Friday, May 7, 2021

How to Install Terraform in Centos and Create EC2 instance using Terraform

Description: Here I have explained, How to install Terraform in Centos and How to create an EC2 instance using Terraform

Install Terraform in Centos

  • Download Terraform from the URL and upload to Centos machine or download using wget command 
#wgt https://releases.hashicorp.com/terraform/0.15.3/terraform_0.15.3_linux_amd64.zip

  • Once you download unzip and extract it


  • After extract terraform binary from zipping, We need to add it on bin path or add the binary path to the source
 # cp terraform /usr/bin/

        OR

Add current pwd path to bash_profile using below command

# echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile

# source ~/.bash_profile

  • After adding into path verify terraform version using below command
# terraform --version


Create EC2 instance using Terraform

  • Create IAM user with EC2 access in AWS console 


  • Download CSV for access_key and secret_key for user
  • Create Project Directory for Terraform
# mkdir Terraform-Project

# cd Terraform-Project

  • Create terraform project as follow and paste 
# vi ec2.tf provider "aws" { access_key = "XXXXXXXXXXXX" secret_key = "XXXXXXXXXXXXXXX" region = "us-east-1" } resource "aws_instance" "Test" { ami = "ami-09d19e919d57453f8" instance_type = "t2.micro"
key_name = "Terraform_Key"

tags = { Name = "Terraform" }
}

  • Initialize terraform project by run below command, you will get output as follow
# terraform init


  • After initialize apply to terraform project using the below command and you will get output as follow. We need to type 'yes' to confirm
# terraform apply



  • Once the process will be completed, you will get output as follow


  • Also, find a new EC2 instance in the list



No comments:

Post a Comment