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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
##----------------------------- ## Terraform: Installation ## ##----------------------------- ## 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 ########################################## ## Create aws resources using terraform ## ########################################## ## Create a directory and get inside it mkdir terraform && cd terraform ## Create your 1st terraform script vi main.tf --------------- #Creates a VPC with cidr block 10.0.0.0/16 provider "aws" { access_key = " 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 ## Initialize terraform (download's 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
## 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 ## 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