Browse the Repo

file-type-icon.circleci
file-type-icon.github
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconaurora
file-type-iconbackup-plan
file-type-iconbackup-vault
file-type-icondms
file-type-iconefs
file-type-iconlambda-cleanup-snapshots
file-type-iconlambda-copy-shared-snapshot
file-type-iconlambda-create-snapshot
file-type-iconlambda-share-snapshot
file-type-iconorg-backup-policy
file-type-iconrds-proxy
file-type-iconrds-replicas
file-type-iconrds
file-type-iconCHANGELOG.md
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconredshift
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconterraform-cloud-enterprise-private-module-...

Browse the Repo

file-type-icon.circleci
file-type-icon.github
file-type-icon_docs
file-type-iconexamples
file-type-iconmodules
file-type-iconaurora
file-type-iconbackup-plan
file-type-iconbackup-vault
file-type-icondms
file-type-iconefs
file-type-iconlambda-cleanup-snapshots
file-type-iconlambda-copy-shared-snapshot
file-type-iconlambda-create-snapshot
file-type-iconlambda-share-snapshot
file-type-iconorg-backup-policy
file-type-iconrds-proxy
file-type-iconrds-replicas
file-type-iconrds
file-type-iconCHANGELOG.md
file-type-iconREADME.md
file-type-iconmain.tf
file-type-iconoutputs.tf
file-type-iconvariables.tf
file-type-iconredshift
file-type-icontest
file-type-icon.gitignore
file-type-icon.pre-commit-config.yaml
file-type-iconCODEOWNERS
file-type-iconLICENSE.txt
file-type-iconREADME.adoc
file-type-iconterraform-cloud-enterprise-private-module-...
RDS

RDS

Terraform code and scripts for deploying data-storage resources (e.g. databases, cache) in AWS

Code Preview

Preview the Code

mobile file icon

README.md

down

RDS Module

This module creates an Amazon Relational Database Service (RDS) cluster that can run MySQL, Postgres, MariaDB, Oracle, or SQL Server. The cluster is managed by AWS and automatically handles standby failover, read replicas, backups, patching, and encryption.

img

About RDS

Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the AWS Cloud. It provides cost-efficient, resizable capacity for an industry-standard relational database and manages common database administration tasks. Refer to the What is Amazon RDS page for more information.

Common Gotcha's

  • All RDS upgrades (version upgrades, instance type upgrades, etc.) require a few minutes of scheduled downtime.
  • If an RDS instance that uses Multi-AZ fails, Amazon will automatically kick off a fail-over, but you will still experience about 3 - 5 minutes of downtime.
  • Based on the above, make sure you've written your app to gracefully handle database downtime.
  • An RDS instance that runs out of disk space will stop working, so be sure to monitor and set an alert on the FreeStorageSpace CloudWatch Metric. Consider monitoring other RDS CloudWatch Metrics as well.

How do you scale this database?

  • Storage: Use the allocated_storage variable.
  • Vertical scaling: To scale vertically (i.e. bigger DB instances with more CPU and RAM), use the instance_type, storage_type, and iops input variables. For a list of AWS RDS server types, see DB Instance Class
  • Horizontal scaling: To scale horizontally, you can add more replicas using the num_read_replicas input variable, and RDS will automatically deploy the new instances, begin asynchronous replication, and make them available as read replicas. For more info, see Working with PostgreSQL, MySQL, and MariaDB Read Replicas.
  • Storage performance: N.B: only available when var.storage_type is set to gp3. When you are using gp3, you can optionally fine-tune storage performance characteristics via the storage_throughput variable. See the RDS User Guide for more information.

How do you connect to the database?

This module provides the connection details as Terraform output variables:

  1. Primary endpoint: The endpoint for the primary DB. You should always use this URL for writes, as it points to the primary.
  2. Read replica endpoints: A comma-separated list of read replica URLs.
  3. Port: The port to use to connect to the endpoints above.

You can programmatically extract these variables in your Terraform templates and pass them to other resources (e.g. pass them to User Data in your EC2 instances). You'll also see the variables at the end of each terraform apply call or if you run terraform output.

Note that the database is likely behind a Bastion Host, so you may need to first connect to the Bastion Host (or use SSH Tunneling) before you can connect to the database.

How do you use this module?

Deployment

Before making any deployment for the RDS database, start by backing up the database and taking a snapshot of the infrastructure state. Review the release notes for any breaking changes and new features. Update the infrastructure code by modifying the Terraform configurations and testing them in a non-production environment. Conduct post-upgrade testing to ensure application functionality and performance, while monitoring the database health. Communicate the potential downtime to relevant stakeholders and involve them in the process.

Minor version upgrades

RDS supports automatically installing minor version upgrades. For example, it can automatically update a MySQL database from version 5.7.10 to 5.7.11. To enable this functionality, follow these steps:

  1. Set the auto_minor_version_upgrade parameter to true.
  2. Set the engine_version parameter to MAJOR.MINOR and omit the PATCH number.

Major Version Upgrade

RDS supports automatically installing major version upgrades. To enable this functionality, follow these steps:

  1. Set the allow_major_version_upgrade parameter to true.
  2. Set the engine_version parameter to MAJOR.MINOR and omit the PATCH number.

Note: consider temporarily setting parameter and option group variables to engine defaults during the major version upgrade process. This step is important to prevent upgrade failures that might occur due to custom configurations not being compatible with the new version. By reverting these configurations to default settings temporarily, you minimize the risk of incompatibility issues during the upgrade process. After the upgrade is successfully completed, these configurations can be reverted back to their custom values, ensuring that your database operates with the desired settings while being compatible with the upgraded version.

Note: A minimal downtime is expected during a major version upgrade. Make sure to communicate the potential downtime to relevant stakeholders in advance.

Blue/Green Deployment for Low-Downtime Updates

By default, RDS updates DB Instances in-place, which can cause service interruptions. Low-downtime updates minimize interruptions by using an RDS Blue/Green deployment. To enable this, set the enable_blue_green_update variable to true.

Note that low-downtime updates are only supported for MySQL, MariaDB, and Postgresql, and backups must be enabled. When using terraform, the Blue/Green Deployment won't finish until the Green instances become the new instance and the Blue instance is deleted. Therefore, Blue/Green Deployment cannot be used for scenarios outside of terraform's resource update, such as manual testing of the Green deployment or reverting back to the Blue deployment.

Standby Deployment

Set multi_az=true. When setting up a multi-AZ (Availability Zone) RDS deployment in AWS, both the primary and standby RDS instances are created in different Availability Zones for high availability. However, this doesn't mean they will have different endpoints. Both instances will have the same DNS endpoint, and AWS's internal infrastructure will handle the failover process transparently for you. AWS RDS provides automatic failover support for DB instances using Multi-AZ deployments for the supported database engines. Failover is automatically handled by RDS without any manual intervention.

Questions? Ask away.

We're here to talk about our services, answer any questions, give advice, or just to chat.

Ready to hand off the Gruntwork?