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
wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip

#unzip the package
unzip terraform_0.11.13_linux_amd64.zip

#move the package to /usr/local/bin
sudo mv terraform /usr/local/bin/ && rm terraform_0.11.13_linux_amd64.zip

#check if terraform working properly
terraform --version

#get help
terraform
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
vi main.tf
---------------
#Create a VPC with cidr block 10.0.0.0/16
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "ap-south-1"
}
resource "aws_vpc" "myvpc" {
cidr_block = "10.0.0.0/16"
tags {
Name = "myvpc"
}}
---------------
:wq

#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)
ls

#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)
ls
cat terraform.tfstate
cat terraform.tfstate.backup

#cleanup
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

https://learn.hashicorp.com/terraform/getting-started/install.html

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

https://www.terraform.io/docs/index.html

 

Leave a Reply

Close Menu