This content originally appeared on DEV Community and was authored by Dmitry Romanoff
In this tutorial, we will walk through how to set up a multi-database environment on AWS using Terraform. We'll cover five different databases: MySQL, PostgreSQL, SQL Server, and two variations of Amazon Aurora (PostgreSQL and MySQL). All of these instances will be created on AWS RDS (Relational Database Service).
The example main.tf
file below demonstrates how to create multiple databases with Terraform and outputs the endpoints for each database instance. This will help you automate the process of provisioning databases on AWS, which is extremely useful for managing and scaling your infrastructure efficiently.
Prerequisites
Before diving into the code, ensure that you have the following:
- An active AWS account with access to RDS services.
- Terraform installed on your local machine.
- AWS credentials configured (either through
aws configure
or environment variables).
You can follow this guide to get started with Terraform on AWS.
The main.tf
Code Explained
Step 1: AWS Provider Configuration
We begin by configuring the AWS provider. This tells Terraform which AWS region to operate in. In the example below, we’re using the us-east-1
region:
provider "aws" {
region = "us-east-1" # Change to your preferred region
}
Make sure to update the region as needed.
Step 2: MySQL Database Instance
Next, we create a MySQL instance using the aws_db_instance
resource. We specify the instance details such as the instance class, storage, database name, username, password, and more:
resource "aws_db_instance" "mysql" {
identifier = "mysql-db-instance"
engine = "mysql"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydb_mysql"
username = "my_admin"
password = "mysecretpassword"
parameter_group_name = "default.mysql8.0"
multi_az = false
publicly_accessible = true
backup_retention_period = 7
storage_type = "gp2"
}
-
instance_class = "db.t3.micro"
: This sets the instance size. -
allocated_storage = 20
: Allocates 20 GB of storage for this instance. -
backup_retention_period = 7
: Retains backups for 7 days.
We use the mysql
engine with the mysql8.0
parameter group for this instance.
Step 3: PostgreSQL Database Instance
Similarly, we create a PostgreSQL instance with its own parameters:
resource "aws_db_instance" "postgres" {
identifier = "postgres-db-instance"
engine = "postgres"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydb_postgres"
username = "my_admin"
password = "mysecretpassword"
parameter_group_name = "default.postgres12"
multi_az = false
publicly_accessible = true
backup_retention_period = 7
storage_type = "gp3"
}
Here, we’ve chosen the postgres
engine and used the postgres12
parameter group. We’ve also set the storage type to gp3
, which offers improved I/O performance compared to gp2
.
Step 4: Amazon Aurora (PostgreSQL)
Aurora is a fully managed relational database compatible with MySQL and PostgreSQL. Below, we create an Aurora PostgreSQL cluster:
resource "aws_rds_cluster" "aurora_postgres" {
cluster_identifier = "aurora-postgres-cluster"
engine = "aurora-postgresql"
master_username = "my_admin"
master_password = "mysecretpassword"
database_name = "mydb_aurora_postgres"
skip_final_snapshot = true
}
-
engine = "aurora-postgresql"
: Specifies the Aurora PostgreSQL engine. -
skip_final_snapshot = true
: We skip the final snapshot when deleting the cluster (be cautious with this setting in production).
Step 5: Amazon Aurora (MySQL)
Next, we create an Aurora MySQL cluster in a similar manner:
resource "aws_rds_cluster" "aurora_mysql" {
cluster_identifier = "aurora-mysql-cluster"
engine = "aurora-mysql"
master_username = "my_admin"
master_password = "mysecretpassword"
database_name = "mydb_aurora_mysql"
skip_final_snapshot = true
}
This is the same as the Aurora PostgreSQL cluster, but using the aurora-mysql
engine.
Step 6: SQL Server Database Instance
We also include a SQL Server instance using the sqlserver-se
engine:
resource "aws_db_instance" "sql_server" {
identifier = "sqlserver-db-instance"
engine = "sqlserver-se"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydb_sql_server"
username = "my_admin"
password = "mysecretpassword"
multi_az = false
publicly_accessible = true
backup_retention_period = 7
storage_type = "gp3"
}
This sets up a SQL Server instance with the sqlserver-se
(SQL Server Standard Edition) engine.
Step 7: Output the Endpoints
Now, we output the database endpoints for each instance and cluster so that we can easily connect to them after Terraform has completed provisioning:
output "mysql_endpoint" {
value = aws_db_instance.mysql.endpoint
}
output "postgres_endpoint" {
value = aws_db_instance.postgres.endpoint
}
output "aurora_postgres_endpoint" {
value = aws_rds_cluster.aurora_postgres.endpoint
}
output "aurora_mysql_endpoint" {
value = aws_rds_cluster.aurora_mysql.endpoint
}
output "sql_server_endpoint" {
value = aws_db_instance.sql_server.endpoint
}
Explanation of Outputs
-
output
blocks: These blocks allow you to print values from the Terraform configuration after the infrastructure is applied. In this case, we're outputting the database endpoints for each of the instances and clusters we created. This is particularly useful because it allows you to access and connect to the newly created databases directly from your application or management tools.
The values provided in the outputs represent the endpoint URLs of each database instance or cluster. You can use these endpoints to configure connections in your applications or database management tools.
For example:
-
MySQL endpoint:
mysql-db-instance.c9akciq32r43.us-east-1.rds.amazonaws.com
-
PostgreSQL endpoint:
postgres-db-instance.c9akciq32r43.us-east-1.rds.amazonaws.com
-
Aurora PostgreSQL endpoint:
aurora-postgres-cluster.cluster-cthvi57f36ds.us-east-1.rds.amazonaws.com
-
Aurora MySQL endpoint:
aurora-mysql-cluster.cluster-cthvi57f36ds.us-east-1.rds.amazonaws.com
-
SQL Server endpoint:
sqlserver-db-instance.c9akciq32r43.us-east-1.rds.amazonaws.com
These output values allow you to easily connect to and manage the databases you just created.
Step 8: Applying the Configuration
Once your main.tf
file is ready, run the following Terraform commands to apply the configuration:
- Initialize Terraform:
terraform init
- Apply the configuration:
terraform apply
Confirm the apply when prompted. Terraform will begin provisioning the resources on AWS.
Complete main.tf
Script
Here is the entire script that we've built for setting up your AWS RDS database instances:
provider "aws" {
region = "us-east-1" # Change to your preferred region
}
resource "aws_db_instance" "mysql" {
identifier = "mysql-db-instance"
engine = "mysql"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydb_mysql"
username = "my_admin"
password = "mysecretpassword"
parameter_group_name = "default.mysql8.0"
multi_az = false
publicly_accessible = true
backup_retention_period = 7
storage_type = "gp2"
}
resource "aws_db_instance" "postgres" {
identifier = "postgres-db-instance"
engine = "postgres"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydb_postgres"
username = "my_admin"
password = "mysecretpassword"
parameter_group_name = "default.postgres12"
multi_az = false
publicly_accessible = true
backup_retention_period = 7
storage_type = "gp3"
}
resource "aws_rds_cluster" "aurora_postgres" {
cluster_identifier = "aurora-postgres-cluster"
engine = "aurora-postgresql"
master_username = "my_admin"
master_password = "mysecretpassword"
database_name = "mydb_aurora_postgres"
skip_final_snapshot = true
}
resource "aws_rds_cluster" "aurora_mysql" {
cluster_identifier = "aurora-mysql-cluster"
engine = "aurora-mysql"
master_username = "my_admin"
master_password = "mysecretpassword"
database_name = "mydb_aurora_mysql"
skip_final_snapshot = true
}
resource "aws_db_instance" "sql_server" {
identifier = "sqlserver-db-instance"
engine = "sqlserver-se"
instance_class = "db.t3.micro"
allocated_storage = 20
db_name = "mydb_sql_server"
username = "my_admin"
password = "mysecretpassword"
multi_az = false
publicly_accessible = true
backup_retention_period = 7
storage_type = "gp3"
}
output "mysql_endpoint" {
value = aws_db_instance.mysql.endpoint
}
output "postgres_endpoint" {
value = aws_db_instance.postgres.endpoint
}
output "aurora_postgres_endpoint" {
value = aws_rds_cluster.aurora_postgres.endpoint
}
output "aurora_mysql_endpoint" {
value = aws_rds_cluster.aurora_mysql.endpoint
}
output "sql_server_endpoint" {
value = aws_db_instance.sql_server.endpoint
}
Conclusion
With this configuration, you’ve successfully created a multi-database environment on AWS, including MySQL, PostgreSQL, Aurora PostgreSQL, Aurora MySQL, and SQL Server. Terraform has provided an efficient way to automate the provisioning and management of these database instances, making it easier to maintain consistent infrastructure for your applications.
By using Terraform to manage your AWS resources, you ensure reproducibility, scalability, and ease of maintenance in your cloud infrastructure.
You can customize this configuration further to suit your specific needs, such as enabling Multi-AZ deployments for high availability, adding monitoring, or customizing storage and backup options.
This content originally appeared on DEV Community and was authored by Dmitry Romanoff

Dmitry Romanoff | Sciencx (2025-03-02T20:21:53+00:00) Setting Up a Multi-Database Environment on AWS Using Terraform. Retrieved from https://www.scien.cx/2025/03/02/setting-up-a-multi-database-environment-on-aws-using-terraform/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.