What is Terraform? What is the use of it?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.
The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

Terraform is an open-source infrastructure as a code software tool created by HashiCorp. It enables users to define and provision a data center infrastructure using a high-level configuration language known as Hashicorp Configuration Language (HCL).

For Example, Suppose you need 2 EC2 Instance, 2 EIP attached with that EC2 Instances, 1 Security Group, 1 Load Balancer etc, So you’ll manually create it in AWS Console/CLI/SDK. Now in case, you need same thing, many times in your requirements, so this will be hectic work for you. So here comes Terraform, Write code once, use it(You can modify) according to you many times.


Prerequisites

  1. Make sure you have an AWS account
  2. Make sure you have access and secret key for a user with ec2 access.

Install terraform on windows

  • Download the terraform executable file from here
  • Extract the downloaded .zip folder containing the terraform executable file and copy the file.
  • Create a new Terraform directory and paste the file, this directory can be anywhere on your machine. I would recommend pasting in “C:\Program Files (x86)\Terraform”.
  • Copy the path of the executable file, in my case, it is “C:\Program Files (x86)\Terraform”
  • Now, we need to set an environment variable that points to this path. Search environment in the windows search bar and select “Edit the system environment variables“.
  • Click on “Environment Variables…“, select “Path” in the pop-up and click on “Edit“.
  • Paste the copied path by clicking on New and then click OK.
  • Done!! Now to check if it is installed open the command prompt and type terraform -v and you will get the current version as the output.

INSTALL TERRAFORM ON Linux

  • Download the terraform Zip File by visiting here and select 32-bit or 64-bit according to your machine.
  • Unzip the folder using the command (make sure you are in the correct directory): $ unzip terraform_0.12.26_linux_amd64.zip (file name may change according to the newer versions).
  • Move the extracted terraform file to /usr/local/bin/ using the command: $ sudo mv terraform /usr/local/bin /
  • Confirm the file has been moved and terraform has been installed using the command: $ terraform -v (if it gives the current version as output then the installation is successful).

Creation of EC2 Instance Using Terraform

The first step to using Terraform is typically to configure the providers (We’re using AWS here) you want to use. Create a file called main.tf & provide the provider information.

provider "aws" {
access_key = "access_key_here"
secret_key = "secret_key here"
region = "ap-south-1"
}

Provider basically shows terraform that you are going to be using AWS and you are going to create infrastructure in this particular region i.e ‘ap-south-1‘ and access and the secret key are provided for authentication.

Note:- providing access and secret key in code is not a recommended way to do this for this we need to create a variable and call it here.

For each type of provider, there are different types of resources you want to create e.g: EC2-Instance, VPC , Security_Groups etc.

resource "aws_instance" "example" {
ami = "ami-0e306788ff2473ccb"
instance_type = "t2.micro"
key_name = "testing"
security_groups = ["${aws_security_group.allow_ssh.name}"]
tags = {
Name = "terraform-instance"
}
}
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow ssh traffic"
ingress {
from_port = 22
 to_port = 22 protocol = "tcp"
 cidr_blocks = ["0.0.0.0/0"]
}
}

Now we will go to terminal, go to that path where you created main.tf and run the terraform init command

terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times

Now run the Command terraform plan

terraform plan command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or to the state. For example, a terraform plan might be run before committing a change to version control, to create confidence that it will behave as expected.

Now to create an Instance run terraform apply command

Type yes and hit Enter to deploy the EC2 Instance

Finally, you deployed an EC2 Instance using terraform. Navigate to your AWS Management Console and you will find your Instance is in running stage

Congratulations… you follow all the steps and created an EC2 instance using terraform

Your email address will not be published. Required fields are marked *