12.6 C
Jaipur
Saturday, January 22, 2022

How to create an RDS instance on AWS using Terraform

Must read

In this article, we will see how to create an RDS MySql Instance. Before proceeding, I assume that you are familiar with the basics of Terraform and AWS RDS Service. If you want to learn to create an RDS MySql instance from the AWS console then search for “How to setup an RDS MySql (Relation Database MySql ) instance on AWS”

Pre-requisites

  1. Basic understanding of Terraform.
  2. Terraform installed on your system.
  3. AWS Account (Create if you don’t have one).
  4. ‘access_key’ & ‘secret_key’ of an AWS IAM User. (Click here to learn to create an IAM user with ‘access_key’ & ‘secret_key’ on AWS, )

 What we will do

  1. Write Terraform configuration files for RDS MySql Instance.
  2. Create an RDS MySql Instance using the Terraform configuration files.
  3. Delete the created RDS MySql Instance using Terraform.

 Write Terraform configuration files for RDS MySql Instance.

Create a dedicated directory where you can create terraform configuration files.

Use the following command to create a directory and change your present working directory to it.

mkdir terraform
cd terraform/

I am using “vim” as an editor to write in files, you can use an editor of your choice and copy-paste the following configurations to create variables.tf, terraform.tfvars and main.tf.

 Create ‘main.tf’ which is responsible to create an RDS MySql on the AWS. This main.tf will read values of variables from variables.tf and terraform.tfvars.

vim main.tf

provider "aws" {
      region     = "${var.region}"
      access_key = "${var.access_key}"
      secret_key = "${var.secret_key}"
}
resource "aws_db_instance" "default" {
  depends_on             = ["aws_security_group.default"]
  identifier             = "${var.identifier}"
  allocated_storage      = "${var.storage}"
  engine                 = "${var.engine}"
  engine_version         = "${lookup(var.engine_version, var.engine)}"
  instance_class         = "${var.instance_class}"
  name                   = "${var.db_name}"
  username               = "${var.username}"
  password               = "${var.password}"
  vpc_security_group_ids = ["${aws_security_group.default.id}"]
  db_subnet_group_name   = "${aws_db_subnet_group.default.id}"
  skip_final_snapshot = "true"
}

resource "aws_db_subnet_group" "default" {
  name        = "main_subnet_group"
  description = "Our main group of subnets"
  subnet_ids  = ["${aws_subnet.subnet_1.id}", "${aws_subnet.subnet_2.id}"]
}
resource "aws_subnet" "subnet_1" {
  vpc_id            = "${var.vpc_id}"
  cidr_block        = "${var.subnet_1_cidr}"
  availability_zone = "${var.az_1}"

  tags = {
    Name = "main_subnet1"
  }
}

resource "aws_subnet" "subnet_2" {
  vpc_id            = "${var.vpc_id}"
  cidr_block        = "${var.subnet_2_cidr}"
  availability_zone = "${var.az_2}"

  tags = {
    Name = "main_subnet2"
  }
}
resource "aws_security_group" "default" {
  name        = "main_rds_sg"
  description = "Allow all inbound traffic"
  vpc_id      = "${var.vpc_id}"

  ingress {
    from_port   = 0
    to_port     = 65535
    protocol    = "TCP"
    cidr_blocks = ["${var.cidr_blocks}"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "${var.sg_name}"
  }
}

Create ‘variables.tf’ which contains the declaration and definition of the variables. 

vim variables.tf

variable "access_key" {
     description = "Access key to AWS console"
}
variable "secret_key" {
     description = "Secret key to AWS console"
}
variable "region" {
     description = "Region of AWS VPC"
}
variable "identifier" {
  default     = "mydb-rds"
  description = "Identifier for your DB"
}

variable "storage" {
  default     = "10"
  description = "Storage size in GB"
}

variable "engine" {
  default     = "mysql"
  description = "Engine type, here it is mysql"
}

variable "engine_version" {
  description = "Engine version"

  default = {
    mysql    = "5.7.21"
  }
}

variable "instance_class" {
  default     = "db.t2.micro"
  description = "Instance class"
}

variable "db_name" {
  default     = "myfirstdb"
  description = "db name"
}

variable "username" {
  default     = "rahul"
  description = "User name"
}

variable "password" {
  description = "password, provide through your ENV variables"
  default = "rahul1234"
}
variable "subnet_1_cidr" {
  default     = "172.31.48.0/20"
  description = "Your AZ"
}

variable "subnet_2_cidr" {
  default     = "172.31.64.0/20"
  description = "Your AZ"
}

variable "az_1" {
  default     = "eu-west-3c"
  description = "Your Az1, use AWS CLI to find your account specific"
}

variable "az_2" {
  default     = "eu-west-3a"
  description = "Your Az2, use AWS CLI to find your account specific"
}

variable "vpc_id" {
  description = "Your VPC ID"
  default = "vpc-be1010d7"
}
variable "cidr_blocks" {
  default     = "0.0.0.0/0"
  description = "CIDR for sg"
}

variable "sg_name" {
  default     = "my-rds-sg"
  description = "Tag Name for sg"
}

Once you have created ‘variables.tf’, do not forget to change the values assigned to variables. You must change the values highlighted as these are specific to my environment. You can keep the rest variables as is.

Create ‘terraform.tfvars’ which contains the definition of access_key and secret_key variables defined in the above file. We have kept the declaration of these 2 variables along with ‘region’ in ‘terraform.tfvars’ file. Change the value of “region” if you want to create the instance in a region other than what I have specified.

The following keys need to be changed with the keys of your IAM user.

vim terraform.tfvars

region = "eu-west-3"
access_key = "AKIAQ6GAIA5XFLXF6HOV"
secret_key = "/lJ3tFDkIYqr0rNX7aJqaXyJR8uCeFMiwuEW6aA/"

Now, you should have 3 files, viz, variables.tf, terraform.tfvars and  main.tf

 Create an RDS MySql Instance using the Terraform configuration files

Before you execute the following commands make sure you have configured the valid access_key and secret_key.

The first command to be used is ‘terraform init’. This command downloads and installs plugins for providers used within the configuration. In our case it is AWS.

 terraform init

The second command to be used is ‘terraform plan’. This command is used to see the changes that will take place on the infrastructure.

 terraform plan

‘terraform apply’ command will create the resources on the AWS mentioned in the main.tf file. You will be prompted to provide your confirmation input to create the resources.

terraform apply

When you execute the above command, upon successful completion, you can see that new resources have been added and 0 has been destroyed.

You can go to the AWS RDS console to verify if the RDS MySql instance is created or not.

Delete the created RDS MySql Instance using Terraform

If you no longer require resources you created using the configuration mentioned in the main.tf file, You can use the “terraform destroy” command to delete all those resources.

terraform destroy

Conclusion

In this article, we saw the steps to create an RDS MySql instance in the region of our choice. We also saw how the instance we created can be deleted in just one command.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article