Monday 11 March 2019

How to run Terraform code in AWS by Raj Gupta




You can get all zip file for all system from
https://www.terraform.io/downloads.html

first downlaod Terraform zip file from HashiCorp
[root@ip-172-31-86-125 ~]# yum install wget -y

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

Put in Path of Linux Command (/usr/local/sbin/)
[root@ip-172-31-86-125 ~]# mv terraform /usr/local/sbin/

Now use it
[root@ip-172-31-84-48 ~]# terraform -version
Terraform v0.11.11

Now create EC2 by using terraform
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"
}

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