How to Create an RDS Instance with Terraform
To create an RDS instance with
Terraform, define an aws_db_instance resource specifying engine, instance class, and credentials. Then run terraform init, terraform plan, and terraform apply to deploy the database.Syntax
The aws_db_instance resource creates an RDS instance. Key parts include:
- identifier: a unique name for your database instance.
- engine: type of database (e.g., mysql, postgres).
- instance_class: size and power of the instance.
- username and password: credentials for the database.
- allocated_storage: storage size in GB.
terraform
resource "aws_db_instance" "example" { identifier = "mydb-instance" engine = "mysql" instance_class = "db.t3.micro" allocated_storage = 20 username = "admin" password = "mypassword" skip_final_snapshot = true }
Example
This example creates a MySQL RDS instance with basic settings. It uses a small instance class and 20 GB storage. The password is set directly for simplicity but should be stored securely in real projects.
terraform
provider "aws" { region = "us-east-1" } resource "aws_db_instance" "example" { identifier = "mydb-instance" engine = "mysql" instance_class = "db.t3.micro" allocated_storage = 20 username = "admin" password = "mypassword" skip_final_snapshot = true publicly_accessible = true }
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
aws_db_instance.example: Creation complete after 5m30s [id=mydb-instance]
Common Pitfalls
Common mistakes when creating RDS instances with Terraform include:
- Not setting
skip_final_snapshottotrueduring testing, which causes apply to fail if a snapshot is required. - Hardcoding passwords in code instead of using secure methods like Terraform variables or AWS Secrets Manager.
- Forgetting to specify
engineor using unsupported engine names. - Choosing an instance class not supported in your AWS region.
terraform
resource "aws_db_instance" "wrong" { identifier = "bad-instance" instance_class = "db.t3.micro" allocated_storage = 20 username = "admin" password = "password" # Missing engine causes error } # Correct way: resource "aws_db_instance" "right" { identifier = "good-instance" engine = "mysql" instance_class = "db.t3.micro" allocated_storage = 20 username = "admin" password = "password" skip_final_snapshot = true }
Quick Reference
| Property | Description | Example |
|---|---|---|
| identifier | Unique name for the DB instance | "mydb-instance" |
| engine | Database engine type | "mysql", "postgres" |
| instance_class | Size/type of instance | "db.t3.micro" |
| allocated_storage | Storage size in GB | 20 |
| username | Master username | "admin" |
| password | Master password | "mypassword" |
| skip_final_snapshot | Skip snapshot on deletion | true |
Key Takeaways
Use the aws_db_instance resource with required properties like engine and instance_class.
Always set skip_final_snapshot=true during testing to avoid apply errors.
Store sensitive data like passwords securely, not hardcoded in code.
Check AWS region support for your chosen instance class and engine.
Run terraform init, plan, and apply to deploy your RDS instance.