In this post, we will see how to restore a RDS using the latest snapshot in AWS

We will use 3 configuration files in here.

1. Generic-varibles.tf

#Input Variables
#AWS Region
variable "aws_region" {
    description = "Region in which AWS Resources will be created"
    type = string
    default = "us-east-1"
}

 

2. Versions.tf

# Terraform Block
terraform {
    required_version = "~> 1.0.11"
    required_providers {
        aws = {
            source = "hashicorp/aws"
            version = "~> 3.0"
        }
    } 
}

#Provider Block
provider "aws" {
    region = var.aws_region
    }

 

3. Main.tf

This is the main file where the details will be provided.
Here infradb is the DB identifier, whose latest snapshot will be taken to restore infradb-uat RDS instance

#Get latest snapshot from Production DB
data "aws_db_snapshot" "db_snapshot" {
    most_recent = true
    db_instance_identifier = "infradb"
}

#Create New Staging DB

resource "aws_db_instance" "infradb_uat" {
    instance_class    = "db.t2.micro"
    identifier = "infradb-uat"
    username = "infra"
    password = "infra#123"
    snapshot_identifier = "${data.aws_db_snapshot.db_snapshot.id}"
    vpc_security_group_ids = ["sg-037cf1f30d24dcb00"]
    skip_final_snapshot = true
    parameter_group_name = "infradb-20220104145649842300000003"
}