AWS Database Blog
Deploy an Amazon RDS for PostgreSQL and MySQL Multi-AZ DB cluster using Terraform modules
Selecting the right cloud management tool is extremely important for many enterprises. In the post 5 Tips for Keeping Pace with AWS’s Innovation, we briefly discussed the high-level strategies to overcome the challenges of continuous improvement and drive innovations in your organization. In this post, we show how infrastructure as code (IaC) solutions built with one of our partner products Hashicorp’s Terraform can help with this goal.
We show how you can use a modular approach to build an IaC solution to deploy Amazon Relational Database Service (Amazon RDS) for PostgreSQL and Amazon RDS for MySQL. The goal is to create a reusable infrastructure with Terraform modules to implement a Multi-AZ DB cluster. For more information about the benefits of this architectural change, refer to New Amazon RDS for MySQL & PostgreSQL Multi-AZ Deployment Option: Improved Write Performance & Faster Failover.
Solution overview
The Multi-AZ DB cluster module provisions and manages a Multi-AZ DB cluster across multiple Availability Zones within an AWS Region in a repeatable and predictable manner.
If you would like to manage an existing Multi-AZ DB Cluster resource with Terraform, then we show how to transition self-provisioned Multi-AZ DB cluster into Terraform module by restoring from a snapshot.
The snapshot restore method works well for the following options:
- A snapshot of a Single-AZ deployment
- A snapshot of a Multi-AZ DB instance deployment with a single DB instance
- A snapshot of a Multi-AZ DB cluster
The following screenshot illustrates this new feature on the Amazon RDS console.
The following diagram illustrates the High level architecture of both Multi-AZ DB Instance and Multi-AZ DB Cluster.
Developing Terraform modules
A Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more .tf files is a module. These modules are developed with end-users in mind. This may include a three-tier application stack for the application team or a Multi-AZ RDS database cluster for DBAs. With this approach, developers and DBAs can benefit from a consistent, repeatable environment for staging, development, and production. A module developer can be creative with a focus on granularity or high-level requirements, and each module can vary based on the need of your end-users.
To achieve our defined goals, let’s start with planning our Terraform modules structure. We develop modules with a focus on DBA requirements. Therefore, we have infrastructure modules covering Amazon Virtual Private Cloud (VPC) networking and security modules and database modules covering the Multi-AZ DB cluster.
The following diagram illustrates the components of these three modules.
Prerequisites
This post assumes that you’re familiar with Terraform, GitHub, and Git commands.
For this walkthrough, you need the following:
- An AWS account. If you don’t have an account, sign up for one.
- A Terraform Cloud account. If you don’t have an account, sign up for a free tier.
One-time setup
Complete the following steps to set up your resources:
- Install git on your workstation.
- Clone the GitHub repo multi-az-db-cluster-sample-for-amazon-rds to your workstation by running the following command in your terminal window:
git clone https://github.com/aws-samples/multi-az-db-cluster-sample-for-amazon-rds
This command creates a directory named multi-az-db-cluster-sample-for-amazon-rds
under your current directory, referred to as <TF_DIR>
in this post.
You can modify the variables as per your use case to deploy the modules.
Workflow for new deployment
Let’s get started by pulling the GitHub Terraform modules for deploying the Multi-AZ DB cluster.
- Create a VPC and deploy the Terraform RDS Multi-AZ DB cluster module by running the following commands in your terminal window:
cd TF_DIR/multi-az-db-cluster-sample-for-amazon-rds/rds_multiaz_cluster/
terraform init
- Once initialized, update the
variables.tf
file with the following values/parameters to deploy either PostgreSQL or MySQL.
-
var.database_name
var.engine [Either postgres or mysql]
var.master_username
var.name
var.rds_secret_name
var.region
- Save the changes of
variables.tf
- Run the command to deploy
terraform apply
The following screenshot illustrates an example of an Amazon RDS PostgreSQL Multi-AZ DB cluster deployment using Terraform Module.
On average, the above deployment takes 20 to 30 minutes. Depending on the configuration settings, it varies.
- On the Amazon RDS console, choose Databases in the navigation pane.
- Verify that an RDS for PostgreSQL Multi-AZ DB cluster is running with one writer instance and two reader instances in different Availability Zones, as shown in the following screenshot.
Workflow for existing resource
As we discussed earlier, you can restore the following types of snapshot to a Multi-AZ DB cluster using Terraform:
- A snapshot of a Single-AZ deployment
- A snapshot of a Multi-AZ DB instance deployment with a single DB instance
- A snapshot of a Multi-AZ DB Cluster
In this example, we create a new Multi-AZ DB cluster from an existing snapshot of a Multi-AZ DB cluster. Complete the following steps:
- Create a VPC and deploy the Terraform RDS Multi-AZ DB cluster using the existing snapshot by running the following commands in your terminal window:
cd TF_DIR/multi-az-db-cluster-sample-for-amazon-rds/rds_multiaz_to_multi-az_cluster/
terraform init
- Once initialized, update the
variables.tf
file with the following to deploy either PostgreSQL or MySQL using snapshot identifier.
-
var.database_name
var.master_username
var.name
var.rds_secret_name
var.region
var.snapshot_db_cluster_identifier
- Save the changes of
variables.tf
. - Run the command to deploy.
terraform apply
The following screenshot illustrates an example for deploying an Amazon RDS PostgreSQL Multi-AZ DB cluster using Snapshot Identifier.
On average, the above deployment takes 20 to 30 minutes. Depending on the configuration settings, it varies.
- On the Amazon RDS console, choose Databases in the navigation pane.
- Verify that a restored RDS for PostgreSQL Multi-AZ DB cluster is running with one writer instance and two reader instances in different Availability Zones.
Clean up
Some of the AWS resources created by the Terraform RDS Multi-AZ instance and cluster modules incur costs as long as they are in use. When you no longer need the resources, clean them up by deleting the Multi-AZ cluster with the VPCs as follows. Run the following commands in your terminal window:
cd TF_DIR/multi-az-db-cluster-sample-for-amazon-rds/rds_multiaz_cluster/
terraform destroy
The following screenshot illustrates an example for cleaning up the Terraform resources.
Conclusion
In this post, we showed you how you can implement deployment strategies using Terraform modules to drive consistency with repeatable deployments and ensure best practices are implemented at every stage of continuous delivery and deployment pipelines. A modular approach with Terraform deployments can help with governance, lifecycle management, and automation for managed database services such Amazon RDS across multiple application environments.
We recommend you test the modules in your development environment and make necessary changes to the Terraform modules to meet your requirements in production setup.
About the Authors
Sudhir Amin is a Database Specialist Solutions Architect at Amazon Web Services. In his role based out of New York, he provides architectural guidance and technical assistance to enterprise customers across different industry verticals, accelerating their cloud adoption. He is a big fan of snooker, combat sports such as boxing and UFC, and loves traveling to countries with rich wildlife reserves where he gets to see world’s most majestic animals up close.
Sharath Chandra Kampili is a Database Specialist Solutions Architect with Amazon Web Services. He works with AWS RDS team, focusing on commercial database engines like Oracle. Sharath works directly with AWS customers to provide guidance and technical assistance on the database projects , helping them improve the value of their solutions when using AWS.