Prerequisites
- Terraform
>= 1.11.1installed (install guide) - AWS credentials configured in your environment (
aws configure, environment variables, or an IAM role) - An existing VPC with at least two subnets in different Availability Zones
- A security group that allows inbound traffic on the database port from your application
The module does not create a VPC or security group. Pass existing resource IDs via
vpc_security_group_ids and either db_subnet_group_name (existing subnet group) or create_db_subnet_group = true + subnet_ids (create a new one).Deploy a MySQL instance
The following example is taken directly from the module’sREADME.md. It creates a MySQL 8.0 instance with Enhanced Monitoring, IAM database authentication, and a custom parameter group.
1
Add the module source
Create a Replace
main.tf file and declare the module:main.tf
sg-12345678 and the subnet_ids values with real IDs from your AWS account.2
Configure required variables
Every module call requires
identifier. The following variables are needed for a functional instance:3
Configure networking
You must attach the instance to a VPC subnet group and one or more security groups:Provide at least two subnets in different Availability Zones. This is required by RDS regardless of whether you enable Multi-AZ.
4
Configure the parameter group
Set For PostgreSQL, the option group is never created (PostgreSQL does not support option groups), so
family to the parameter group family that matches your engine and version:For MySQL and Oracle, also set
major_engine_version so the module can create a matching option group:major_engine_version is only informational.5
Initialize and apply
apply typically takes 10–15 minutes. When it completes, retrieve the connection endpoint:Complete MySQL example
This example is taken directly fromexamples/complete-mysql/. It creates a Multi-AZ MySQL 8.0 instance with Performance Insights, Enhanced Monitoring, CloudWatch log exports, and storage autoscaling. Supporting VPC and security group resources are created using community modules.
main.tf
Complete PostgreSQL example
This example is taken directly fromexamples/complete-postgres/. It creates a Multi-AZ PostgreSQL 17 instance with managed password rotation, Performance Insights, Enhanced Monitoring, and CloudWatch log exports. It also demonstrates automated backups replication to a second region.
main.tf
PostgreSQL does not support option groups. When
engine = "postgres" is set, the module automatically skips creating a db_option_group resource regardless of the create_db_option_group setting.Common outputs
Afterterraform apply, reference these outputs in other modules or scripts:
outputs.tf
Important operational notes
deletion_protection
Set deletion_protection = true on any instance you do not want accidentally deleted. Terraform will fail with an error if you attempt to destroy an instance with deletion protection enabled, which prevents mistakes. To delete the instance, first set deletion_protection = false and re-apply.
backup_window and maintenance_window
These two windows must not overlap. A common pattern taken from the examples above is:
backup_window, AWS assigns a random window.
skip_final_snapshot
skip_final_snapshot defaults to false, meaning a final DB snapshot is created when you destroy the instance. Set it to true only in non-production environments where you do not need a recovery point.