Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

Implementing Automated RDS Backup and Restore Strategy with Terraform

3 min read

In today’s fast-paced digital world, data protection and business continuity are of paramount importance. For organizations leveraging Amazon RDS (Relational Database Service), implementing an automated backup and restore strategy is critical. In this highly technical blog post, we will explore how to achieve this using Terraform, an infrastructure-as-code tool, from start to finish.

Prerequisites

Before diving into the technical details, ensure you have the following prerequisites in place:

Install Terraform

Install Terraform on your local machine and configure it with valid AWS credentials.

Installing Terraform on MacOS

Homebrew is a free and open-source package management system for Mac OS X. Install the Terraform formula from the terminal.

$ brew install terraform

NOTE: Homebrew and the Terraform formula are NOT directly maintained by HashiCorp. The latest version of Terraform is always available by manual installation.

Installing Terraform on Windows

A binary distribution is avaialble for all environments. Let’s grab the latest version of it for windows.

Open up a powershell on your windows machine, cd to a directroy to D drive and create an Terraform directory,

PS C:\Users\user>D:

Get an exe from the below url,

PS D:\> curl.exe -O https://releases.hashicorp.com/terraform/0.12.26/terraform_0.12.26_windows_amd64.zip

Then unzip this archieve, rename a directory to terraform and we will see a single binary file name terraform and add it’s path into environment variables.

PS D:\Terraform> Expand-Archive terraform_0.12.26_windows_amd64.zip
PS D:\> Rename-Item -path .\terraform_0.12.26_windows_amd64\ .\terraform

Regarding setting up an environment variable, you can add terraform path in Path variable as shown in below screenshot,

And, your are done. Now open up a terminal and run a command terrform and enter

PS D:\terraform> terraform

Verify the installation

Verify that the installation worked by opening a new powershell or cmd session and listing Terraform’s available subcommands.

PS D:\terraform> terraform -help
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you’re just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Add any subcommand to terraform -help to learn more about what it does and available options.

PS D:\terraform> terraform -help plan

Installing Terraform on Linux

A binary distribution is avaialble for all environments. Let’s grab the latest version of it for linux.

$ wget https://releases.hashicorp.com/terraform/0.12.26/terraform_0.12.26_linux_amd64.zip

Then unzip the archieve,

$ unzip terraform_0.12.26_linux_amd64.zip

Check the executable permission on the binary, if it’s not executable, make it executable using the below commmand,

$ chmod +x terraform

Finally make sure that terrform is avaiable in PATH. So, let’s move the binary into /usr/local/bin directroy,

$ sudo mv terraform /usr/local/bin

Now you are ready to run terraform commands. Open up a new termnal and run a command terraform and enter,

$ terraform

Verify the installation

Verify that the installation worked by opening a new terminal session and listing Terraform’s available subcommands.

$ terraform -help

Set up AWS Account

Set up an AWS account with appropriate permissions to create and manage RDS instances.

Step 1: Provisioning RDS Instance

The first step is to define the Terraform configuration for provisioning the RDS instance. Create a new directory for your Terraform project and navigate to it.

1.1. Initialize Terraform:

Run the following command to initialize the project:

terraform init

1.2. Create the Terraform Configuration:

Create a new file called main.tf and add the following code:

# Define AWS provider and region
provider "aws" {
  region = "us-west-2" # Update with your desired region
}

# Create RDS instance
resource "aws_db_instance" "example" {
  engine               = "mysql"
  instance_class       = "db.t3.micro"
  allocated_storage    = 20
  storage_type         = "gp2"
  identifier           = "my-rds-instance"
  username             = "admin"
  password             = "password"
  publicly_accessible = false

  # Other RDS configuration settings...
}

Step 2: Enabling Automated Backups

Now, let’s configure the RDS instance to enable automated backups.

2.1. Update the Terraform Configuration:

Add the following code to the main.tf file:

# Enable automated backups
resource "aws_db_instance" "example" {
  # Previous RDS configuration...

  # Enable automated backups
  backup_retention_period = 7
  backup_window           = "03:00-04:00"
  maintenance_window      = "sun:05:00-sun:06:00"
}

Step 3: Implementing the Restore Strategy

To complete the end-to-end process, let’s define the restore configuration using Terraform.

3.1. Create a New Configuration File:

Create a new file called restore.tf and add the following code:

# Define the restored RDS instance
resource "aws_db_instance" "restored_example" {
  engine               = "mysql"
  instance_class       = "db.t3.micro"
  allocated_storage    = 20
  storage_type         = "gp2"
  identifier           = "restored-rds-instance"
  username             = "admin"
  password             = "password"
  publicly_accessible = false

  # Other RDS configuration settings...

  # Specify the snapshot ID to restore from
  snapshot_identifier = "<snapshot-id>"
}

Remember to replace with the actual snapshot identifier you want to restore from.

Please Note:

To restore an RDS instance from a specific snapshot, you will need to specify the actual snapshot identifier associated with the desired snapshot in your AWS account. The snapshot identifier is a unique identifier assigned to each snapshot created for your RDS instances.

To obtain the actual snapshot identifier, you can follow these steps:

  • Log in to the AWS Management Console.
  • Open the Amazon RDS service.
  • Navigate to the “Snapshots” section.
  • Locate the snapshot you want to restore from and note down its snapshot identifier.

The snapshot identifier typically follows a naming convention like rds:-. For example, if your RDS instance identifier is mydatabase and the snapshot was taken on January 1, 2023, the snapshot identifier might be rds:mydatabase-2023-01-01-12-34-56.

Once you have the snapshot identifier, you can specify it in your Terraform configuration’s snapshot_identifier parameter to restore the RDS instance from that particular snapshot.

By specifying the snapshot identifier, you ensure that Terraform restores the RDS instance from the specific snapshot you desire, enabling you to recover your database to a specific point in time.

Step 4: Apply the Terraform Configuration

Now it’s time to apply the Terraform configuration and create the RDS instance with automated backups and the restore strategy.

4.1. Apply the Configuration:

Run the following command to apply the Terraform configuration:

terraform apply

4.2. Confirm and Proceed:

Review the changes shown by Terraform, and when prompted, enter yes to proceed with the provisioning.

Conclusion

We explored how to implement an automated RDS backup and restore strategy using Terraform. We started by provisioning the RDS instance, enabling automated backups, and configuring the restore strategy. By leveraging Terraform’s infrastructure-as-code capabilities, we can easily automate the backup and restore processes, ensuring data protection and business continuity.

Remember to regularly monitor your backups and periodically test the restore functionality to validate the effectiveness of your strategy. With Terraform, you can easily adjust and modify the configurations as your requirements evolve.

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server
Index