Monday, 18 March 2019

How to install Terraform tool and run the Terraform code in AWS by Raj Gupta


You can get all zip file for all the system from

https://www.terraform.io/downloads.html




First download Terraform zip file from HashiCorp (https://www.terraform.io/downloads.html)  as per your operating system.

In my case i am using linux 

[root@ip-172-31-86-125 ~]# yum install wget -y



Now unzip Terraform


[root@ip-172-31-86-125 ~]# yum install unzip –y
[root@ip-172-31-86-125 ~]# unzip terraform_0.11.11_linux_amd64.zip

Put in Path of Linux Command (/usr/local/sbin/)


[root@ip-172-31-86-125 ~]# mv terraform /usr/local/sbin/


Now check it

[root@ip-172-31-84-48 ~]# terraform -version

Terraform v0.11.11


Now run the simple Terraform code to create EC2 server

make a new directory (can be named anything) and go inside the directory

[root@ip-172-31-84-48 ~]# mkdir terraform-july && cd terraform-july
[root@ip-172-31-84-48 terraform-july]#

Paste this following code to a file called raj.tf (can be anything.tf)
The syntax of Terraform configurations is called HashiCorp Configuration Language (HCL)

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-2757f631"
  instance_type = "t2.micro"
}


Here change the Access key and Secret key with your actual value.

Now initialize the working directory for terraform

[root@ip-172-31-84-48 terraform-july]# terraform init

"The terraform init command is used to initialize a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times."

Provision the ec2 with this command
[root@ip-172-31-84-48 terraform-july]# terraform apply

From your terminal/command prompt/ shell , destory the resources
[root@ip-172-31-84-48 terraform-july]# terraform destroy

No comments:

Post a Comment