LAUNCHING INFRASTRUCTURE USING TERRAFORM

Harsh Upparwal
Analytics Vidhya
Published in
5 min readJan 15, 2021

--

This is my first article on Terraform, in this article, I’ll talk about overview on Terraform & Creation of EC2 Instance using Terraform.

So, first question that comes in mind! What is Terraform?

Well, terraform is an IAC(Infrastructure as a Code) tool used for building, changing, and versioning infrastructure, which supports many popular cloud providers including Amazon AWS.

This guide will walk you through the basics of configuring a single instance using a simple configuration file and the Terraform provider.

Before we begin, there are few perquisites:

· Configure your AWS credentials.

· Familiarity with command line (PowerShell).

Head over to the Terraform download page and get the latest version for your operating system.

To check if terraform is installed or not, open CMD and run the following command:

As you can see, terraform version gives us which version is installed in your system thereby confirming that terraform was successfully configured.

To begin with, create a terraform directory and use this directory to store all your terraform templates. Remember that the name of your terraform file will always end with .tf extentsion.

Use command, notepad [file-name].tf

This will create a new file which we will the use for creating terraform template. For this demo I am using notepad as a text editor for creating a terraform template but you can choose a text editor of your choice.

The first step to using Terraform is typically to configure the provider(s) you want to use. Create a file called ec2.tf and put the following code in it:

provider “aws” {

region = “us-west-1”

}

This tells Terraform that you are going to be using the AWS provider and that you wish to deploy your infrastructure in the us-east-1 region (AWS has data centers all over the world, grouped into regions and availability zones, and us-east-1 is the name for data centers in N. Virginia)

If you are using a federated account(like educate) then you are good to go with above code just update .aws/credentials file on your system, if you are a IAM user then you will have to follow

provider “aws” {

region = “us-west-1”

access_key = “YOUR_ACCESS_KEY”

secret_key = “YOUR_SECRET_KEY”

}

this template to configure IAM credentials in-order to use with AWS.

Next, you will specify which resource you want to use. The general syntax for a Terraform resource is:

resource “<PROVIDER>_<TYPE>” “<NAME>” {
[CONFIG …]
}

Where PROVIDER is the name of a provider (e.g., aws), TYPE is the type of resources to create in that provider (e.g., instance), NAME is an identifier you can use throughout the Terraform code to refer to this resource (e.g., terra-ec2), and CONFIG consists of one or more arguments that are specific to that resource.

For this demo we are simply launching an ec2 instance and therefore the template is set accordingly.

For the aws_instance resource, there are many different arguments, you can check them out on terraform documentation.

when first starting to use Terraform, you need to run terraform init to tell Terraform to scan the code, figure out what providers you’re using, and download the code for them.

By default, the provider code will be downloaded into a .terraform folder, which is Terraform’s scratch directory. You need to run init any time you start with new Terraform code, and that it’s safe to run init multiple times (the command is idempotent).

Now that you have the provider code downloaded, run the terraform plan command.

Terraform generates an execution plan, describing what it will do to reach the desired state, then executes it to build the described infrastructure.

resources with a plus sign (+) are going to be created, resources with a minus sign (-) are going to be deleted, and resources with a tilde sign (~) are going to be modified in-place. In the output above, you can see that Terraform is planning on creating a single EC2 Instance and nothing else, which is exactly what we want.

Enter yes when prompted.

Use command, terraform apply to begin deploying the configuration to your infrastructure.

After the configuration is loaded, you will be asked to confirm your planned actions. Enter yes when prompted.

Now you deployed an EC2 Instance in your AWS account using Terraform. Let’s check whether it’s created or not. Open your AWS Console & go to EC2. Note the instance ID displayed on your terminal and the one shown on EC2 dashboard.

EC2 Dashboard

Congratulations, you have just deployed infrastructure from code!

For deleting the instance, use terraform destroy command.

Terraform will ask to confirm delete. Type yes and hit enter.

Go to your EC2 dashboard to and check instance state.

This is for basic introduction of Terraform & We have deployed our first EC2 Instance and deleted it using Terraform.

Clap if you found this article helpful.

THANK YOU!!

--

--