Infrastructure Evolution: Harnessing Terraform to Deploy RDS Instances
Shivant Kumar Pandey
Cloud Engineering Consultant @Deloitte | Ex-Software Engineer @Deltatech Gaming Limited | Back-end Developer | Cloud Enthusiast | Blogger | AWS | AZURE | GCP | GenAI | Devops | Python | Nodejs | Scripting | CI-CD
In today's rapidly evolving world of cloud infrastructure and automation, managing databases efficiently is crucial for seamless application development and deployment. Amazon RDS (Relational Database Service) offers a managed database service that simplifies database administration tasks. In this guide, we'll walk you through the process of creating a MySQL RDS database instance using Terraform and then connecting to it using MySQL SQLyog.
Table of Contents
Prerequisites:-
Before you begin, ensure you have the following:
Step 1: Set Up Terraform
If you haven't already, download and install Terraform from the official website.
What is Terraform?
Let’s get started!
Objectives:-
1.?Create infrastructure for this project and create a directory name: terraform-project
2.?Change to the directory -?terraform-project?and run?terraform init
3.?Generate the action plans
4.?Create all the resources declared in?main.tf?configuration file.
5.?Check the infrastructures created, from AWS Console
6.?Testing RDS Connection using the SQLyog
7.?Delete AWS Resources
Pre-requisites:
Resources Used:
Here's a list of some common Terraform commands you might use during your infrastructure management journey:
1.Initialization:
??- `terraform init`: Initializes a Terraform working directory by downloading necessary plugins and modules.
2.Configuration:
??- `terraform validate`: Validates the syntax and structure of your Terraform configuration files.
??- `terraform fmt`: Formats your configuration files according to Terraform's style conventions.
3.Planning and Applying:
??- `terraform plan`: Generates an execution plan showing what changes will occur when you apply your configuration.
??- `terraform apply`: Applies your configuration, creating or updating resources as needed.
4.Managing State:
??- `terraform state list`: Lists all resources in the Terraform state.
??- `terraform state show`: Displays details about a specific resource in the state.
??- `terraform state mv`: Moves a resource from one state to another.
??- `terraform state rm`: Removes a resource from the state.
5.Applying Changes:
??- `terraform apply`: Applies your configuration, creating or updating resources as needed.
??- `terraform refresh`: Updates the state file to match the current real-world resources.
6.Destroying Resources:
??- `terraform destroy`: Destroys the resources defined in your configuration.
7.Workspace Management:
??- `terraform workspace new`: Creates a new workspace.
??- `terraform workspace select`: Switches to a different workspace.
??- `terraform workspace list`: Lists all available workspaces.
8.Input and Output:
??- `terraform input`: Displays all configured variables and their values.
??- `terraform output`: Displays the values of any defined outputs.
9.Terraform Modules:
??- `terraform get`: Downloads any necessary modules for your configuration.
??- `terraform init -upgrade`: Updates modules to the latest versions if possible.
10.Miscellaneous:
??- `terraform providers`: Lists the providers used in the current configuration.
Steps for implementation of this project:
1. Create infrastructure for this project
#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"
}
Next Steps:-
#terraform.tfvars
region = "us-east-1"
access_key = "<YOUR AWS CONSOLE ACCESS ID>"
secret_key = "<YOUR AWS CONSOLE SECRET KEY>"
#main.tf
#defining the provider as aws
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 = "myrdsuser"
password = "myrdspassword"
parameter_group_name = "default.mysql5.7"
vpc_security_group_ids = ["${aws_security_group.rds_sg.id}"]
skip_final_snapshot = true
publicly_accessible = true
}
#outputs.tf
output "security_group_id" {
value = aws_security_group.rds_sg.id
}
output "db_instance_endpoint" {
value = aws_db_instance.myinstance.endpoint
}
2. Change to the?terraform-project?and run?terraform init?to initialize Terraform.
cd ../terraform-project
terraform init
3. Generate the action plans
terraform plan
4. Create all the resources declared in?main.tf?configuration file.
terraform apply
5. Check the infrastructures created, from AWS Console
6. Testing RDS Connection using the SQLyog
1.?To connect to a database on a DB instance using MySQL monitor, find the endpoint (DNS name) and port number for your DB Instance.
Endpoint:?db_instance_endpoint = "myrdsinstance.cffqkia4zue7.ap-south-1.rds.amazonaws.com:3306"
2.?Open SQLyog. Click on the plus icon.
Host Name:?Enter the endpoint?"myrdsinstance.cffqkia4zue7.ap-south-1.rds.amazonaws.com:3306"
3.?Click on it to open the database. Enter the database password if prompted.
Note: Do not forget to Delete Resources to get charges.
7. Delete AWS Resources
terraform destroy
Now You can Test whether your RDS Database was created or not using Boto3 and verify it.
import boto
# Set your AWS credentials (or use other methods like IAM roles)
region = "ap-south-1"
access_key = "################"
secret_key = "################"
db_instance_name = 'myrdsinstance' ?# Replace with the RDS instance name
# Initialize Boto3 RDS client
rds_client = boto3.client('rds', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region)
try:
? ? # Describe the RDS instance
? ? response = rds_client.describe_db_instances(DBInstanceIdentifier=db_instance_name)
? ? print(response,"response")
? ? # Check if the instance exists
? ? if len(response['DBInstances']) > 0:
? ? ? ? instance = response['DBInstances'][0]
? ? ? ? status = instance['DBInstanceStatus']
? ? ? ? print(f"Instance {db_instance_name} exists and is in '{status}' state.")
? ? else:
? ? ? ? print(f"Instance {db_instance_name} does not exist.")
except Exception as e:
? ? print("An error occurred:", str(e))
3
Start your journey today and unlock the true potential of terraform learning with Amazon Web Services. Let AWS be your partner in driving innovation, enhancing customer experiences, and shaping the future of your business. Together, let's create a world where automation fuels limitless possibilities.
Wish you great success!
Regards,
Shivant Kumar Pandey