DEV Community

Cover image for Deploying AWS RDS Instances with Terraform: MySQL and PostgreSQL
Dmitry Romanoff
Dmitry Romanoff

Posted on

Deploying AWS RDS Instances with Terraform: MySQL and PostgreSQL

In this article, we'll walk through how to provision Amazon Web Services (AWS) Relational Database Service (RDS) instances for both MySQL and PostgreSQL using Terraform. By using Infrastructure as Code (IaC), we can automate the process of creating these databases, which will allow for consistent and reproducible infrastructure deployments.

Prerequisites

Before we dive into the Terraform configuration, make sure you have the following prerequisites:

  1. Terraform: Install Terraform on your machine. You can follow the official installation guide here.
  2. AWS Account: Ensure you have an active AWS account.
  3. AWS CLI: Set up AWS CLI and configure your credentials. You can configure it using aws configure.
  4. IAM Permissions: Ensure your AWS credentials have sufficient permissions to manage RDS instances.

What We'll Build

In this tutorial, we’ll define two AWS RDS instances:

  • MySQL: A basic MySQL instance using the mysql8.0 engine.
  • PostgreSQL: A PostgreSQL instance using the latest postgres16 engine.

We’ll configure both instances with 20 GB of storage, a db.t3.micro instance type, and encryption enabled for data at rest. The instances will also be set to not be publicly accessible and will have a backup retention period of 7 days.

Writing the Terraform Configuration

Create a file named main.tf. This is where we’ll define our Terraform resources for provisioning the RDS instances.

provider "aws" {
  region = "us-east-1"  # Change to your preferred region
}

# MySQL RDS Instance
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 = false
  backup_retention_period = 7
  storage_type      = "gp2"
  storage_encrypted = true
}

# PostgreSQL RDS Instance
resource "aws_db_instance" "postgres" {
  identifier        = "postgres-db-instance"
  engine            = "postgres"
  instance_class    = "db.t3.micro"
  allocated_storage = 20
  engine_version    = "16"
  db_name           = "mydb_postgres"
  username          = "my_admin"
  password          = "mysecretpassword"
  multi_az          = false
  publicly_accessible = false
  parameter_group_name = "default.postgres16"
  backup_retention_period = 7
  storage_type      = "gp3"
  storage_encrypted = true
}

# Outputs: RDS Endpoints
output "mysql_endpoint" {
  value = aws_db_instance.mysql.endpoint
}

output "postgres_endpoint" {
  value = aws_db_instance.postgres.endpoint
}
Enter fullscreen mode Exit fullscreen mode

Breaking Down the Configuration

  1. Provider Block:
   provider "aws" {
     region = "us-east-1"  # Change to your preferred region
   }
Enter fullscreen mode Exit fullscreen mode

This block configures the AWS provider for Terraform, specifying the region in which your resources will be created. You can change the region as per your requirements.

  1. MySQL RDS Instance:
   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 = false
     backup_retention_period = 7
     storage_type      = "gp2"
     storage_encrypted = true
   }
Enter fullscreen mode Exit fullscreen mode

This resource block creates an RDS instance for MySQL. Here’s a breakdown of some key properties:

  • engine: Specifies the database engine (MySQL).
  • allocated_storage: Sets the storage size in GB (20 GB here).
  • instance_class: Defines the type of instance (db.t3.micro in this case, which is a low-cost instance).
  • storage_encrypted: Ensures data is encrypted at rest.
  • backup_retention_period: Retains backups for 7 days.
  1. PostgreSQL RDS Instance:
   resource "aws_db_instance" "postgres" {
     identifier        = "postgres-db-instance"
     engine            = "postgres"
     instance_class    = "db.t3.micro"
     allocated_storage = 20
     engine_version    = "16"
     db_name           = "mydb_postgres"
     username          = "my_admin"
     password          = "mysecretpassword"
     multi_az          = false
     publicly_accessible = false
     parameter_group_name = "default.postgres16"
     backup_retention_period = 7
     storage_type      = "gp3"
     storage_encrypted = true
   }
Enter fullscreen mode Exit fullscreen mode

This block is similar to the MySQL one but for PostgreSQL. The engine_version property is set to 16, indicating the latest version of PostgreSQL.

  1. Outputs:
   output "mysql_endpoint" {
     value = aws_db_instance.mysql.endpoint
   }

   output "postgres_endpoint" {
     value = aws_db_instance.postgres.endpoint
   }
Enter fullscreen mode Exit fullscreen mode

These output blocks display the endpoint URLs for the MySQL and PostgreSQL RDS instances once they are created. You can use these endpoints to connect to your databases.

Applying the Configuration

  1. Initialize Terraform: To begin, initialize your Terraform working directory with the following command:
   terraform init
Enter fullscreen mode Exit fullscreen mode
  1. Plan the Deployment: Run the following command to preview the resources Terraform will create:
   terraform plan
Enter fullscreen mode Exit fullscreen mode
  1. Apply the Configuration: Apply the configuration to create the RDS instances by running:
   terraform apply
Enter fullscreen mode Exit fullscreen mode

Terraform will prompt for confirmation before proceeding. Type yes to create the resources.

Verifying the Deployment

Once the deployment is complete, Terraform will output the endpoints for the MySQL and PostgreSQL RDS instances. You can connect to these instances using the endpoint URLs along with the username and password specified in the configuration.

Conclusion

In this tutorial, we’ve successfully automated the deployment of MySQL and PostgreSQL RDS instances in AWS using Terraform. By leveraging Terraform's Infrastructure as Code capabilities, we can quickly and efficiently create reproducible environments for our databases.

Feel free to expand upon this configuration by adding more customization options like VPC, security groups, and IAM roles to further secure and optimize your RDS instances.

Top comments (0)