In this post, we will create Multi AZ RDS using Terraform

RDS Engine/Version : MySQL 5.7

We will also see how to provide an existing VPC security group using Terraform while creating the RDS.

We will just post the configuration files over here. For more details, please visit our YouTube Channel

1. generic-variables.tf

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

variable "multi_az" {
  description = "Specifies if the RDS instance is multi-AZ"
  type        = bool
  default     = true
}

 

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

#Create RDS Instance
module "rds" {
  source  = "terraform-aws-modules/rds/aws"
  version = "3.4.1"
  # insert the 30 required variables here
  identifier = "infradb"
  engine            = "mysql"
  engine_version    = "5.7.19"
  instance_class    = "db.t2.micro"
  allocated_storage = 5
  name                                = "INFRA"
  username                            = "infra"
  password                            = "Infra123"
  port                                = 3306
  skip_final_snapshot              = true ##If you dont want backup before deletion, keep this true. Or else, the option group wont be deleted as snapshot is using it
  backup_window               = "03:00-06:00"
  maintenance_window          = "Mon:00:00-Mon:03:00"
  ##vpc_security_group_ids = ["sg-0b340cbfced198974"] # You can provide this if you want to create your RDS in a Non Default VPC
  #DB Subnet Group
  subnet_ids      = ["subnet-009f74608e791eb62","subnet-04a752c1a6a0acb17"]
  
  #Multi AZ
  multi_az = var.multi_az

  #DB Parameter Group
  family          = "mysql5.7"

  #DB Option Group
  option_group_name      = "mysql5-7-option-group"

  major_engine_version = "5.7"

  parameters = [
      {
          name = "character_set_client"
          value = "utf8mb4"
      },
      {
          name = "character_set_server"
          value = "utf8mb4"
      }
  ]


}

 

4. outputs.tf

output "db_instance_address" {

  description = "The address of the RDS instance"

  value       = module.rds.db_instance_address

}

output "db_instance_availability_zone" {

description = "The availability zone of the RDS instance"

value = module.rds.db_instance_availability_zone

}

output "db_instance_endpoint" {

description = "The connection endpoint"

value = module.rds.db_instance_endpoint

}

output "db_instance_hosted_zone_id" {

description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"

value = module.rds.db_instance_hosted_zone_id

}

output "db_instance_id" {

description = "The RDS instance ID"

value = module.rds.db_instance_id

}

output "db_instance_name" {

description = "The database name"

value = module.rds.db_instance_name

}

output "db_instance_username" {

description = "The master username for the database"

value = module.rds.db_instance_username

sensitive = true

}

output "db_instance_port" {

description = "The database port"

value = module.rds.db_instance_port

}

output "db_parameter_group_id" {

description = "The db parameter group id"

value = module.rds.db_parameter_group_id

}

output "db_master_password" {

description = "The master password"

value = module.rds.db_master_password

sensitive = true

}