Skip to main content

Prerequisites

  • Terraform >= 1.11.1 installed (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’s README.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 main.tf file and declare the module:
main.tf
Replace 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:
manage_master_user_password defaults to true, so you do not need to supply a password. RDS generates one and stores it in AWS Secrets Manager. Retrieve it after terraform apply using the db_instance_master_user_secret_arn output.
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 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:
For PostgreSQL, the option group is never created (PostgreSQL does not support option groups), so major_engine_version is only informational.
5

Initialize and apply

The first apply typically takes 10–15 minutes. When it completes, retrieve the connection endpoint:
Do not run terraform destroy on a production database without first setting deletion_protection = false and skip_final_snapshot = false. By default, the module creates a final snapshot and enables deletion protection, which prevents accidental data loss.

Complete MySQL example

This example is taken directly from examples/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 from examples/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

After terraform apply, reference these outputs in other modules or scripts:
outputs.tf
Retrieve the master password from Secrets Manager using the AWS CLI:

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:
Both values are in UTC. If you omit 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.
For production, use both deletion_protection = true and skip_final_snapshot = false. For development environments where you frequently recreate infrastructure, use deletion_protection = false and skip_final_snapshot = true.