Introduction to Terraform – EC2 Instance Creation using Terraform

What is Terraform? What is use of it?

Terraform is open-source infrastructure as a code (IAC) software tool designed by HashiCorp. Terraform is used for building, changing, and versioning infrastructure safely and efficiently. It can manage existing and popular cloud service providers such as AWS, AZURE, GCP, Alibaba as well as custom on premises solutions. It is one of the famous DevOps tools in the market. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language (HCL). Here we will give introduction to Terraform and will do EC2 instance Creation using Terraform.

For instance, Suppose you need 10 EC2 Instance, 10 EIP attached with that EC2 Instances, 1 Security Group, 1 Load Balancer etc, So you need to create it manually using AWS Console/CLI/SDK. Now in case, you require same thing many times in your requirements so this will be a hectic job for you. So here comes terraform, write code once and use it as many times as you want. However you can modify it as per your requirement.

Here we’ll use AWS Cloud Provider for this practical. We’ll install the Terraform on EC2 Instance. I hope you one AWS account and have basic knowledge on AWS EC2 Instance. I have already created one EC2 Instance to install Terraform on it. I used Amazon Linux 2 AMI to create EC2 Instance. I have connected this EC2 instance using putty as i am using windows OS platform.

Terraform Thecodecloud.in

Terraform Installation on EC2 Instance

First let’s install Terraform on ec2 instance using below commands. Then extract the tar file and move to /usr/bin directory.

# wget https://releases.hashicorp.com/terraform/0.15.2/terraform_0.15.2_linux_amd64.zip

# unzip terraform_0.12.16_linux_amd64.zip -d /usr/bin/

Now let’s check Terraform Version using below command.

# terraform -v

Terraform v0.15.2
on linux_amd64

So we have successfully installed the terraform on EC2 Instance. Now we’ll create one EC2 Instance using Terraform.  Once Terraform installed, we’ll configure AWS IAM credentials in-order to use terraform with AWS.

credentials terraform thecodecloud.in

Note: It is recommended to use AWS IAM Roles for EC2 instances rather than providing security credentials on EC2 instance. It not best practice for security purpose.

Creating EC2 Instance Using Terraform

The first step to using Terraform is typically to configure the providers (Here we’re using AWS) you would like to use. Create a file called providers.tf & put the provider information.

provider "aws" {
    region = "us-east-1"
}

Basically it tells Terraform that you are going to use AWS as your provider and you want to deploy your infrastructure into us-east-1 region. For each type of provider, there are many different kinds of resources that you can create such as instances, databases, security group, VPC and load balancers etc.

We’ll create one more file called main.tf. So for creating the EC2 instance, basically we need AMI, instance type & tags. Additionally, here we need to specify key_pair name in order to login into EC2 instacnce.

resource "aws_instance" "my_first_server" {
    ami = "ami-0d5eff06f840b45e9"
    instance_type = "t2.micro"
    key_name = "Devops_project"
    tags = {
        Name = "Devops_Practice"
    }
}

terraform main.tf

Now let’s go to the terminal and find that path where you created main.tf and run the terraform init command.

Terraform init

The terraform binary contains the basic functionality for Terraform, but it does not come with the code for any of the cloud providers, so when you’re first starting to use Terraform, you need to run terraform init to tell Terraform to scan the code, figure out which providers you’re using, and download the code for them.

By default, the provider code will be downloaded into a .terraform folder, it is terraform’s scratch directory. You need to run init any time you start with new terraform code and 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 plan

Terraform plan command lets you see what terraform will actually do before actually making any changes in real. This is a great way to check sanity of your code before unleashing it onto the real world. Anything in output with a plus sign (+) will be created, with a minus sign (–) will be deleted and anything with a tilde sign (~) will be modified in place. In the preceding output, you can see that Terraform is planning on creating a single EC2 Instance and nothing else, which is exactly what you want.

So to actually create the Instance, run the terraform apply command.

# terraform apply

Terraform apply

You’ll get to know that the terraform apply command shows you the same plan output what terraform plan showed you and it asks you to confirm whether you actually want to proceed with this plan or not. Hit yes and press enter to deploy the EC2 Instance.

Now you just deployed an ec2 instance in your account using terraform. Let’s check whether it’s created or not. Open your AWS Console & go to ec2 section.

Terraform ec2 instance

This is the basic introduction of Terraform and we deployed our first EC2 Instance using Terraform. Hence we have given introduction to terraform and done EC2 Instance Creation using Terraform

Read More : Top 20 Linux Interview Questions For Experienced SysAdmins

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

2 thoughts on “Introduction to Terraform – EC2 Instance Creation using Terraform”

Leave a Comment