Interview Prep/Real-World Interview/Write-Terraform tasks

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.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-Terraform tasks