Create RDS Using Simple Terraform Script

Divesh
2 min readMay 10, 2023

--

RDS using Terraform

Below is the simple script which can create latest MySQL8.0 AWS RDS instance

provider "aws" {
region = "us-east-1"
}

resource "aws_db_instance" "example" {
identifier = "example-db"
allocated_storage = 10
storage_type = "gp2"
engine = "mysql"
engine_version = "8.0"
instance_class = "db.t3.micro"
name = "example-db"
username = "admin"
password = "password123"
parameter_group_name = "default.mysql8.0"
backup_retention_period = 7
publicly_accessible = false
vpc_security_group_ids = ["sg-1234567890"]
db_subnet_group_name = "example-subnet-group"
}

Just copy and save it as rds.tf than run terraform init than run terraform apply and you are done.

Let me now explain what is written in above script..This script uses the AWS provider and creates an aws_db_instance resource with the following parameters:

  • identifier:

The name of the RDS instance.

  • allocated_storage:

The amount of storage to allocate for the RDS instance in GB.

  • storage_type:

The storage type to use for the RDS instance.

  • engine:

The database engine to use for the RDS instance.

  • engine_version:

The version of the database engine to use for the RDS instance.

  • instance_class:

The instance type to use for the RDS instance.

  • name:

The name of the RDS instance.

  • username:

The username to use for the RDS instance.

  • password:

The password to use for the RDS instance.

  • parameter_group_name:

The name of the parameter group to use for the RDS instance.

  • backup_retention_period:

The number of days to retain automated backups for the RDS instance.

  • publicly_accessible:

Whether or not the RDS instance should be publicly accessible.

  • vpc_security_group_ids:

The IDs of the security groups to use for the RDS instance.

  • db_subnet_group_name:

The name of the DB subnet group to use for the RDS instance.

You can run this script using the terraform apply command. Terraform will create the RDS instance and output its endpoint address.

In order to make this script working…You need to install and configure aws cli for us-east-1 region. Please follow below link to install awscli and to configure just run aws configure and provide access_key & secret_key along with region you need to specify to make this tf script create the RDS instance for you.

You may add these parameters value in vars file or you can use aws parameter store to store this values. further, you can use this in script I just kept it simple to understand so that you can create it. Once you are done with it you can optimize this script as per your requirement but again in day to day admin activity you just need to create it so you can make the copy of it.

thanks.!

--

--

Divesh
Divesh

Written by Divesh

An Architect, A DevOps Engineer, An Automation master, A Kubernetes Security Specialist and always willing to help because helping others is my favourite task.

No responses yet