How Meesho launched infrastructure as code through Terraform, and its many advantages
By: Amrita Bose
Tags: AWS, Tech, Terraform, DevOps, Software Development

Guest blog by Rahul Arya, Senior DevOps Engineer

As Meesho is growing at scale, so is the infrastructure (infra) for handling requests and launching of new services for our Meesho entrepreneurs (resellers). Our infra provides them with new features frequently to help them earn more on the platform and become even more successful (Making Resellers Successful is a core Meesho value).

Launching the infra from a console is an easy enough task, but sometimes if you miss certain parameters it might cost you in terms of time spent and other factors. The DevOps team at Meesho wanted to do it in a robust way so that it met our standards of achieving our infra security.

Whenever we release a new feature, bug fixers or introduce the latest services, we have to go through multiple testing (such as functionality and performance), which recurs in an infra similar to production.

Keeping the above pointers in mind, the DevOps team ( Jegadesh Samiraj, Senior Manager — DevOps and I ) at Meesho decided to introduce Terraform, an open source infrastructure as a code software tool, towards the launching of a clean and perfect infra for every requirement at Meesho and modifying or scaling up the existing infra.

But before choosing Terraform, we also evaluated other open source tools, before arriving at this solution. Let’s begin by examining Terraform and its many advantages first.

Terraform is a simple and powerful, open-source tool that allows you to create, manage and improve IT infrastructures. It can be shared among members of a team and treated exactly as code — so it can be easily changed, reviewed, and versioned.

What are the advantages of using Terraform?

Terraform is a cross-platform tool and it can interact with AWS, GCE, VMware, OpenStack and Azure. It can also be packaged as modules and reused, which avoids rewriting the code.

Below are the steps for getting started and is targeted at AWS and Terraform.

Pre-Requisites required:

Create an AWS account or use an existing one .

Generate AWS Access Key and Secret Key.

Note: We can use the AWS credential file or IAM role ARN and mention it in provider.tf block in the code itself.

How to install the Terraform

Installing Terraform is pretty straightforward. Download it from the Terraform download page and select the appropriate package based on your operating system.

To verify that Terraform is installed correctly, apply the following:

Checking Terraform version

Caption: Checking Terraform version

As mentioned above Terraform supports many providers, and for my use case I am using AWS.

Creating EC2 instance with Terraform.

provider “aws” {

region = “ap-northeast-1”

}

resource “aws_key_pair” “example” {

key_name = “example-key”

public_key = “XXXX”

}

resource “aws_security_group” “examplesg” {

ingress {

from_port = 22

to_port = 22

protocol = “tcp”

cidr_blocks = [“192.x.x.x/0”]

}

}

resource “aws_instance” “ec2_instance” {

ami = “ami-34st7099”

instance_type = “t2.micro”

vpc_security_group_ids = [“${aws_security_group.examplesg.id}”]

key_name = “${aws_key_pair.example.id}”

tags {

Name = “my-ec2-instance”

}

}

Before running the Terraform command to spin our first EC2 instance, run Terraform fmt command which will rewrite Terraform configuration files to a canonical format and style.

Formatting the Terraform code

The first command we are going to run to setup our instance is terraform init, and what this will do is that it will download code for a provider (AWS) that we are going to use.

Initializing the Terraform code

Terraform gives us the possibility of planning any modification and control change previews before applying.

After the terraform plan, we raise the PR to verify what we are going to launch with the terraform.

Dry running of the Terraform code to check the infra it is planning to create

Once, PR is approved and merged our infra will get launched. To apply these changes, run Terraform apply.

Reusable infrastructure with Terraform modules

Terraform modules can be used to avoid code duplication for the same application deployed in multiple environments.

We can maintain the templates of the resources in a particular folder that may be used more than once for the deployment of environments. All we need to do is specify the path of the folder which will fetch the resource.

Terraform module structure

For creating our infra for performance testing, releasing a new service or bug fixes, we at Meesho now use Terraform to launch these infras. Because of Terraform, we can spin up our infra within a few minutes and it can provide a clean, perfect infra for every requirement, save cost and time as well.

Want to join the DevOps team and help Meesho create 20 million entrepreneurs by 2020? Then apply here. We are hiring!