Generate a random password, store it in Secrets Manager and pass it to RDS without it appearing in code.
AdvancedHands-onWrite-Terraform tasks
Answer
resource "aws_db_instance" "app" {
identifier = "app-db"
engine = "postgres"
engine_version = "16"
instance_class = "db.t4g.medium"
allocated_storage = 50
username = "app_admin"
manage_master_user_password = true # RDS creates and rotates it in Secrets Manager
storage_encrypted = true
skip_final_snapshot = false
final_snapshot_identifier = "app-db-final"
}
output "db_secret_arn" {
value = aws_db_instance.app.master_user_secret[0].secret_arn
}
Key trick → manage_master_user_password keeps the password out of Terraform state entirely. A random_password resource would still be stored in plain text in state. Apps read the secret ARN at runtime.
More on Write-Terraform tasks
Write Terraform for an S3 bucket with versioning, encryption and public access blocked.BeginnerWrite the remote backend configuration with S3 and state locking.BeginnerWrite a VPC with public and private subnets across 3 AZs using `count` or `for_each`.IntermediateWrite a reusable module for an EC2 instance with inputs, outputs and validation.AdvancedWrite a security group that allows HTTPS from anywhere and SSH only from an office CIDR.BeginnerCreate multiple IAM users from a list with `for_each`, and output their ARNs as a map.Beginner