Managing database users in Amazon Aurora Serverless v2 for PostgreSQL involves provisioning and managing user roles effectively, and can e challenging. In this blog, we’ll explore the best practices for setting up and managing users efficiently using Terraform.
1. Traditional PostgreSQL User Management
2. Terraform-Based User Management Approach
Using a configuration file in Terraform simplifies the process of provisioning and managing users and roles. In our project, we have automated database user management using Terraform with the following steps:
Define users and roles in Terraform local files
Maintain a structured list of users and their corresponding roles.
Create the Aurora Serverless v2 DB cluster
Define the necessary Terraform resources to provision the Aurora database.
Store user credentials in AWS Secrets Manager
Generate and manage secrets for individual users securely.
Create roles and users dynamically
Use stored secrets to create database users and attach appropriate roles.
Implement automatic secret rotation
Deploy an AWS Lambda function to rotate secrets daily and update user passwords in PostgreSQL.
3. Managing Users with AWS Secrets Manager
4. Role-Based Access Control (RBAC) in Aurora Serverless v2
5. Automating User Creation with Terraform
6. Sample Terraform Configuration
locals {
# To attach a specific role in the specific environment
env_roles = {
dev = { dev_users = "ro", devops_users = "rw", admin_users = "admin" }
stg = { dev_users = "ro", devops_users = "rw", admin_users = "admin" }
prd = { dev_users = "ro", devops_users = "ro", admin_users = "admin" }
}
# List of application user identities
app_users = {
dev_users = [
"dev_user_a",
"dev_user_b",
"dev_user_c",
"dev_user_d"
]
devops_users = [
"devops_user_a",
"devops_user_b"
]
admin_users = [
"admin_user_a",
"admin_user_b"
]
}
}
resource "aws_secretsmanager_secret" "db_user_secrets" {
for_each = local.db_users
name = "rds-db-credentials/${module.psql_app_serverless_v2_db.this.cluster_resource_id}/${each.key}"
description = "RDS database ${module.psql_app_serverless_v2_db.this.cluster_id} credentials for ${each.key}"
kms_key_id = data.aws_kms_key.secretsmanager.id
}
resource "aws_secretsmanager_secret_version" "db_user_secrets" {
for_each = aws_secretsmanager_secret.db_user_secrets
secret_id = each.value.id
secret_string = jsonencode({
engine = "postgres"
host = data.aws_rds_cluster.psql_app_serverless_v2_db.endpoint
username = each.key
password = random_password.db_user_secrets[each.key].result
dbname = data.aws_rds_cluster.psql_app_serverless_v2_db.database_name
port = "${data.aws_rds_cluster.psql_app_serverless_v2_db.port}"
})
}
module "create_postgres_user" {
for_each = {
for user, user_info in local.sql_users_map :
user => user_info
if var.env_name != "localstack"
}
source = "digitickets/cli/aws"
version = "7.0.0"
role_session_name = "CreatePostgresUser"
aws_cli_commands = [
"rds-data", "execute-statement",
format("--resource-arn=%s", module.psql_app_serverless_v2_db.this.cluster_arn),
format("--secret-arn=%s", data.aws_secretsmanager_secret.rds_cluster.arn),
format("--database=%s", local.rds_database_name),
format("--sql=\"%s\"", local.sql_create_user[each.key].sql)
]
}
module "lambda_rotate_db_secret" {
source = "../modules/lambda"
function_name = "lbda-rotate-db-secret"
description = "Rotate Aurora Serverless PostgreSQL DB secret"
handler = "lambda_function.lambda_handler"
source_path = "./lambda_function.py"
create_package = true
package_type = "Zip"
runtime = "python3.9"
timeout = 30
memory_size = 128
layers = [aws_lambda_layer_version.psycopg2_layer.arn]
create_role = true
}
Managing users in Aurora Serverless v2 PostgreSQL involves multiple layers, including IAM authentication, role management, Secrets Manager, and automation with Terraform. By following these best practices, and using the right cybersecurity approach, you can ensure a secure and scalable database user management approach.
At PALO IT, we specialize in partnering with clients to implement these kinds of strategies, ensuring seamless integration of AWS services and Terraform in your projects. If you're eager to unlock the full potential of your database management, give us a shout!
Or, if you'd like to learn more, check out our related PALO IT services:
✅ Data Strategy
✅ AI & Gen AI Strategy
✅ Cybersecurity Services