Labs · DevOps
Lab: Launch a Private RDS MySQL Database with Terraform: DB Subnet Group, Security Group, and a Master Password That Never Appears in Code
Course: DevOps · Chapter 44: Terraform AWS Guides EC2 RDS S3 Security Groups IAM ALB and Auto Scaling · Lesson: 44.2 How to launch an RDS MySQL database with Terraform
Lesson 44.2 explains every line; this lab runs it and checks the secret and the private setting.
Chala mitrano! Database cha password code madhe? Kadhi nahi. Aaj RDS swataha password banvel ani Secrets Manager madhe thevel.
Suppose we are…
Suppose we are building the backend for an Infosys client's leave-management app and the team needs a managed MySQL database. Running MySQL on our own EC2 means we patch it, back it up and fail it over ourselves. RDS does those jobs, and Terraform makes the database repeatable. The big rule in this guide: the password is never written in a file. RDS creates it and keeps it in AWS Secrets Manager.
Goal of this lab
By the end you will have:
- A private RDS MySQL instance created from code.
- The master password stored only in Secrets Manager.
- Everything destroyed at the end.
What you need
- Terraform 1.6 or newer (
terraform version). - AWS CLI v2 signed in to your own AWS account with an IAM user or SSO role that may create these resources (
aws sts get-caller-identityworks). Never use the root user, and never paste access keys into .tf files. - Region Mumbai (
ap-south-1) is used; changeregionif you prefer another. - 45 minutes (RDS takes 6–10 minutes to create).
Cost and cleanup
RDS bills by the hour while it exists. Small charges are possible even on the free tier. Finish the lab in one sitting and always run the Clean up part at the end.
Part 1: Create the project files
Create the folder:
mkdir rds-guide && cd rds-guideWhat you should see: after the next steps, this layout:
rds-guide/ ├── versions.tf ├── variables.tf ├── main.tf └── outputs.tfCreate
versions.tf:versions.tf
terraform { required_version = ">= 1.6" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = var.region }Create
variables.tf:variables.tf
variable "region" { type = string default = "ap-south-1" } variable "db_name" { type = string default = "appdb" } variable "db_username" { type = string default = "appadmin" }Create
main.tf:main.tf
data "aws_vpc" "default" { default = true } data "aws_subnets" "default" { filter { name = "vpc-id" values = [data.aws_vpc.default.id] } } resource "aws_db_subnet_group" "db" { name = "guide-rds-subnets" subnet_ids = data.aws_subnets.default.ids } resource "aws_security_group" "db" { name = "guide-rds-mysql" description = "MySQL from inside the VPC only" vpc_id = data.aws_vpc.default.id } resource "aws_vpc_security_group_ingress_rule" "mysql" { security_group_id = aws_security_group.db.id cidr_ipv4 = data.aws_vpc.default.cidr_block ip_protocol = "tcp" from_port = 3306 to_port = 3306 } resource "aws_db_parameter_group" "mysql" { name = "guide-mysql8" family = "mysql8.0" parameter { name = "require_secure_transport" value = "1" } } resource "aws_db_instance" "app" { identifier = "guide-mysql" engine = "mysql" engine_version = "8.0" instance_class = "db.t3.micro" allocated_storage = 20 storage_type = "gp3" storage_encrypted = true db_name = var.db_name username = var.db_username manage_master_user_password = true db_subnet_group_name = aws_db_subnet_group.db.name vpc_security_group_ids = [aws_security_group.db.id] parameter_group_name = aws_db_parameter_group.mysql.name publicly_accessible = false backup_retention_period = 7 skip_final_snapshot = true deletion_protection = false }Create
outputs.tf:outputs.tf
output "db_endpoint" { value = aws_db_instance.app.endpoint } output "db_secret_arn" { value = aws_db_instance.app.master_user_secret[0].secret_arn }
Part 2: Init, check and plan
Download the AWS provider:
terraform initWhat you should see:
Terraform has been successfully initialized!Format and validate:
terraform fmt terraform validateWhat you should see:
Success! The configuration is valid.Plan and read the counts:
terraform planWhat you should see:
Plan: 5 to add, 0 to change, 0 to destroy.
Part 3: Apply and test
Apply and type
yes:terraform applyWait 6–10 minutes for the apply to finish:
What you should see: the outputs:
Plan: 5 to add, 0 to change, 0 to destroy. aws_db_instance.app: Still creating... [5m0s elapsed] aws_db_instance.app: Creation complete after 6m12s [id=db-ABCDEFG...] Apply complete! Resources: 5 added, 0 changed, 0 destroyed. Outputs: db_endpoint = "guide-mysql.cxxxxxxx.ap-south-1.rds.amazonaws.com:3306" db_secret_arn = "arn:aws:secretsmanager:ap-south-1:<account-id>:secret:rds!db-..."Check the database is not public:
aws rds describe-db-instances --db-instance-identifier guide-mysql --query "DBInstances[0].[PubliclyAccessible,DBInstanceStatus]"What you should see:
[ false, "available" ].Open Secrets Manager in the console and find the secret whose name starts with
rds!db.What you should see: a secret managed by RDS. Your
.tffiles contain no password.
Clean up: destroy everything
Delete everything this lab created. Type
yeswhen asked:terraform destroyWhat you should see:
Destroy complete! Resources: 5 destroyed.
Ravindra Bagale's Tip
manage_master_user_password = true is the one line that keeps the password out of Git and out of state. Use it on every new RDS instance.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Error: creating RDS DB Instance (guide-mysql): InvalidParameterCombination: | Apply stops | For the version error, run aws rds describe-db-engine-versions --engine mysql --query "DBEngineVersions[].EngineVersion" and use a listed version. For the subnet group error, add a subnet from a second Availability Zone. Creating RDS takes 5 to 10 minutes, so a long wait is normal. |
Setting publicly_accessible = true to "test quickly" | Database reachable from the internet | Keep it private; connect from an app server or a bastion |
Access keys written in .tf files | Keys end up in Git and in state | Use aws configure sso or a profile; roles in CI |
Self-check checklist
0 of 4 done
Try-at-home challenge
On your own machine, read the secret with aws secretsmanager get-secret-value --secret-id <db_secret_arn> --query SecretString, and write down who in a team should have that permission.
Samajla ka? Private database, password in Secrets Manager, destroy after practice. Aata pudhe jaauya S3.