Infrastructure Evolution: Harnessing Terraform to Deploy RDS Instances

Infrastructure Evolution: Harnessing Terraform to Deploy RDS Instances

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
  • Set Up Terraform
  • Write the Terraform Configuration
  • Apply the Terraform Configuration
  • Connect to the RDS Instance with SQLyog
  • Conclusion

Prerequisites:-

Before you begin, ensure you have the following:

  • An AWS account
  • MySQL Workbench/ SQLYOG installed on your local machine
  • Terraform installed on your local machine

Step 1: Set Up Terraform

If you haven't already, download and install Terraform from the official website.

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.

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:

  • AWS user account with admin access, not a root account.
  • Cloud9 IDE with AWS CLI and Terraform.
  • Download the MySql GUI Tool. Based on your OS, select the respective option under Generally Available (GA) Releases, Download, and Install.

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

  • Let’s create the following organizational structure as shown below.
  • Create a directory -?terraform-project
  • Create 4 files -?variables.tf,?terraform.tfvars,?main.tf, and?outputs.tf.


  • Create a variables.tf ?file.

#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:-

  • Create a?terraform.tfvars?file.
  • You are defining the dynamic values for the variables declared in variables.tf file.

#terraform.tfvars

region = "us-east-1"
access_key = "<YOUR AWS CONSOLE ACCESS ID>"
secret_key = "<YOUR AWS CONSOLE SECRET KEY>"        

  • Create a?main.tf?file.

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


  • Create an?outputs.tf?file.
  • Outputs the security group id and RDS Database Instance endpoint to confirm that they are created.

#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        


  • Wait for a few minutes till all the resources have been created.


5. Check the infrastructures created, from AWS Console

  • Go to RDS Console, Click on the Databases on the left navigation panel, You can see RDS Database Instance created successfully


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.

  • Go to databases and click on?myrdsinstance.
  • Under the Connectivity & security section, copy and note the endpoint and port.

Endpoint:?db_instance_endpoint = "myrdsinstance.cffqkia4zue7.ap-south-1.rds.amazonaws.com:3306"

  • Port:?3306

2.?Open SQLyog. Click on the plus icon.

  • Connection Name:?MyDatabseConnection

Host Name:?Enter the endpoint?"myrdsinstance.cffqkia4zue7.ap-south-1.rds.amazonaws.com:3306"

  • Port:?3306
  • Username:?myrdsuser
  • Password:?Click on Store in Vault and enter password?myrdspassword. Click on ok.
  • Click on Test Connection to make sure that you are able to connect to the database properly.
  • Click on ok and ok again to save the connection.

No alt text provided for this image


3.?Click on it to open the database. Enter the database password if prompted.

  • After successfully connecting and opening the database, you can create tables and perform various queries over the connected database.
  • Navigate to the Schemas tab to see databases available to start doing database operations. More details on database operations are available?here.


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

MyBlog?Portfolio?Github?Medium


要查看或添加评论,请登录

社区洞察

其他会员也浏览了