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.
Table of Contents
What is Terraform?
- 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)?

- 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

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

Before we begin, make sure you have the following Editor installed on your system:
Visual Studio Code And HashiCorp Terraform Extension

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 invariables.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.

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" }

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>"

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 }

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 }

Initialize and Apply Terraform Configuration
- Open a terminal in the directory containing your
main.tf
file and run the following commands:
terraform init

- 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


- 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.


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


- 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

- 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

End Result
- To see the end result login to AWS console and search for RDS service and click on Databases.


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