How to Create MySQL RDS in AWS using Terraform [2 Steps]

In this article, We are going to cover How to Create MySQL RDS in AWS using Terraform | Creation of RDS with existing VPC using Terraform.

Create a MySQL RDS Database Instance with Terraform 2

What is Terraform?

1280px Terraform Logo.svg removebg preview 1
  • It is an open-source IaaC (Infrastructure as a code) software tool where you define and create resources using providers in the declarative configuration language example JSON.
  • With Terraform, You can package and reuse the code in the form of modules.
  • It supports a number of cloud infrastructure providers such as AWS, Azure, GCP, IBM Cloud, OCI, etc.

What is Amazon Relational Database Service (Amazon RDS)?

Amazon RDS
  • RDS stands for Relational Database Service
  • It’s a managed DB service for DB use SQL as a query language.
  • It allows you to create databases in the cloud that are managed by AWS
    • Postgres
    • MySQL
    • MariaDB
    • Oracle
    • Microsoft SQL Server
    • Aurora (AWS Proprietary database)
  • 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.

Advantage over using RDS versus deploying DB on EC2

  • Automated provisioning, OS patching
  • Continuous backups and restore to specific timestamp (Point in Time Restore)!
  • Monitoring dashboards
  • Read replicas for improved read performance
  • Multi AZ setup for DR (Disaster Recovery)
  • Maintenance windows for upgrades
  • Scaling capability (vertical and horizontal)
  • Storage backed by EBS (gp2 or io1)

RDS Architecture

image 13

Amazon RDS for MySQL

  • Amazon RDS (Relational Database Service) for MySQL is a fully managed database service that simplifies the process of setting up, operating, and scaling a MySQL database in the cloud.
  • MySQL is widely used in web development, business applications, and various other domains due to its reliability, performance, and ease of use. Its open-source nature and strong community support contribute to its continued popularity as a relational database management system.

Prerequisites

d3c7b91a b285 4d1e 8429 5de1acc5f61e removebg preview

Before we begin, make sure you have the following Editor installed on your system:

Visual Studio Code And HashiCorp Terraform Extension

Screenshot 2023 11 21 134609

How to Create MySQL RDS in AWS using Terraform

Create infrastructure for this project in Visual Studio Code

  • Let’s create the following organizational structure as shown below.
  • Create a Folder – terraform-rds-project
  • Create 4 files – variables.tf, terraform.tfvars, main.tf, and outputs.tf.
  • variables.tf-This file is used to declare variables in Terraform. Variables allow you to parameterize your configurations, making them more flexible and reusable.
  • terraform.tfvars-This file is used to set values for the variables declared in variables.tf. It allows you to provide specific values for your variables without modifying the main Terraform configuration files.
  • main.tf-This is the main Terraform configuration file where you define the infrastructure resources and their configurations. It specifies what resources to create, how to configure them, and any other necessary settings.
  • outputs.tf-This file is used to define output values that you want to be shown when Terraform applies your configuration. Outputs can be useful for obtaining information about the infrastructure that you can use in subsequent steps or for display purposes.
Screenshot 2023 11 21 135034

Create a variables.tf file in terraform-rds-project folder

  • Enter the below code
variables.tf
#variables.tf
variable "access_key" {
    description = "Access key to AWS console"
}
variable "secret_key" {
    description = "Secret key to AWS console"
}
variable "region" {
    description = "AWS region"
}
Screenshot 2023 11 21 135633

Create a terraform.tfvars file in terraform-rds-project folder

terraform.tfvars
region = "ap-south-1"
access_key = "<YOUR AWS CONSOLE ACCESS ID>"
secret_key = "<YOUR AWS CONSOLE SECRET KEY>"
Screenshot 2023 11 21 140508

Create a main.tf file in terraform-rds-project folder

  • Enter the below code
main.tf
provider "aws" {
    region     = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}

#create a security group for RDS Database Instance
resource "aws_security_group" "rds_sg" {
  name = "rds_sg"
  ingress {
    from_port       = 3306
    to_port         = 3306
    protocol        = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
#create a RDS Database Instance
resource "aws_db_instance" "myinstance" {
  engine               = "mysql"
  identifier           = "myrdsinstance"
  allocated_storage    =  20
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  username             = "admin"
  password             = "admin12345"
  parameter_group_name = "default.mysql5.7"
  vpc_security_group_ids = ["${aws_security_group.rds_sg.id}"]
  skip_final_snapshot  = true
  publicly_accessible =  true
}
Screenshot 2023 11 21 141527

Create a output.tf file and Enter the below code

  • Outputs the security group id and RDS Database Instance endpoint to confirm that they are created.
output.tf
output "security_group_id" {
  value       = aws_security_group.rds_sg.id
}
output "db_instance_endpoint" {
  value       = aws_db_instance.myinstance.endpoint
}
Screenshot 2023 11 21 142042

Initialize and Apply Terraform Configuration

  • Open a terminal in the directory containing your main.tf file and run the following commands:
terraform init
Screenshot 2023 11 21 143217
  • To show the changes that will be applied to the infrastructure, show the additions and modifications, or any deletions of resources based on the current configuration for this Terrraform plan is used.
terraform plan
Screenshot 2023 11 21 143928
Screenshot 2023 11 21 143953 1
  • Terraform apply is a Terraform command that executes the planned changes, creating, modifying, or deleting resources to align the actual infrastructure with the desired state defined in the configuration.
terraform apply

Terraform will prompt you to confirm the creation of the resources. Type yes and press Enter.

Screenshot 2023 11 21 145029

If we want monitor the progress:-

Terraform will display the execution plan and ask for confirmation before applying the changes. Once confirmed, it will create the MySQL RDS instance. You can monitor the progress in your AWS Management Console or by running:

terraform show
Screenshot 2023 11 21 145150
Screenshot 2023 11 21 145203
  • To check the syntax and to validate the configuration files, ensuring they are adhere to the HashiCorp Configuration Language (HCL) and are structurally correct we use Terraform Validate command
terraform validate
Screenshot 2023 11 21 145456
  • If we want to automatically format the Terraform configuration files and ensure consistent and readable code it is done by applying a standard style and for that purpose we use Terraform fmt command.
terraform fmt
Screenshot 2023 11 21 145750

End Result

  • To see the end result login to AWS console and search for RDS service and click on Databases.
Screenshot 2023 11 21 150032
Screenshot 2023 11 21 150057

Conclusion

In this article, we’ve demonstrated How to Create MySQL RDS in AWS using Terraform, Terraform’s declarative syntax and the ability to manage infrastructure as code make it a powerful tool for automating the deployment of database instances and other cloud resources.

Reference:-

For reference visit the official website TerraformRegistry.

Any queries pls contact us @Devopshint.

Related Articles:

Terraform State File Management | Terraform Local State File | Terraform Remote State

About Akash Bhujbal

Hey, I am Akash Bhujbal, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link