How To Install Terraform In AWS EC2

How to install Terraform in AWS EC2

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In this series, we will explore one of the most popular IAC tool Terraform. In this demo we will install Terraform in AWS EC2 instance and create our 1st AWS resource using Terraform.

What is Terraform?

HashiCorp Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.

How to install Terraform in AWS EC2:

#Create an EC2 AWS Linux 2 instance and an IAM user with admin access
#and programettic access (Get the Access key and Secret Key)
#Connect to the AWS instance

##terraform installation
#get terraform package

#unzip the package

#move the package to /usr/local/bin
sudo mv terraform /usr/local/bin/ && rm

#check if terraform working properly
terraform --version

#get help
terraform <command> --help #for example: terraform plan --help

##create aws resources using terraform
#create a directory and get inside it
mkdir terraform && cd terraform

#create your 1st terraform script
#Create a VPC with cidr block
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "ap-south-1"
resource "aws_vpc" "myvpc" {
cidr_block = ""
tags {
Name = "myvpc"

#auto format the terraform files
terraform fmt

#Initialized terraform (download provider [AWS] packages)
terraform init

#create the terraform plan
terraform plan

#create the resources, type yes when prompted (creates a VPC named myvpc in AWS)
terraform apply

Login to your AWS console and check if VPC ‘myvpc’ created

How To Install Terraform In AWS EC2

#check files (due to above apply, terraform.tfstate file created)

#show details from terraform.tfstate file
terraform show

#show details of a particular resource from terraform.tfstate file
terraform state show <resource_name> #for example: terraform state show aws_vpc.myvpc

#delete the resources, type Yes when prompted
terraform destroy

#check files (due to above destroy, terraform.tfstate file emptied and terraform.tfstate.backup created as backup)
cat terraform.tfstate
cat terraform.tfstate.backup

cd .. && rm -rf terraform

Hope you have enjoyed this article. In the next blog post, we will discuss terraform providers.

To install terraform in a different platform, please refer below terraform documentation

To get more details on terraform, please refer below terraform documentation


Leave a Reply

Close Menu