Provisioners terraform

Posted By :Baldev Pal |30th March 2021

 

What is Terraform? 

i. Infrastructure provisioning-store your cloud infrastructure set up as code.

ii. It’s like CloudFormation but for many cloud providers i.e AWS Google, Azure, Cloudflare.

 

Terraform provisioners:

Created provisioner.tf file is used to the remote or local machine as part of resource creation and destruction.

i. Build your own AMI with the required software/application.

ii. Boot with standard image and install your required software and necessary configuration.

 

1.Local Exec:

The Local and provisioner are used to execute scripts on a local machine that is running the Terraform.

 

2.Remote Exec:

The remote exec provisioner executes tools and scripts on a remote target.

 

How to Install Terraform:

 

Step1: Before you download Terraform, update the repository lists.

$ sudo apt-get update

 

Step2: Run the wget utility to download Terraform:

$ sudo wget https://releases.Hashicorp.com/terraform/0.12.2/terraform_0.12.2_linux_amd64.zip

 

Step3: Extract the files:

$ sudo unzip ./ terraform_0.12.2_linux_amd64.zip –d /usr/local/bin

step4:Next, verify that Terraform accepts commands:

$ terraform –v

 

Launch ec2 instance and configure apache server using terraform script.

Step1:Create a directory.

$ sudo mkdir terrafrom

Step2:Create provisioner.tf file.

$ sudo nano provisioner.tf

provider "aws" {


region = "us-east-1"

access_key = "AKIA52TM6V26IT6ANIRQ"

secret_key = "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"

}


resource "aws_key_pair" "provi" {

key_name = "provi"

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCpqx9qiMbOj8BirJbx/ivLSwOStoiMgb3HK4fv0y0hq

DW3yoiwn4UFNL1Jr2MvgWHmPVivC1Ti3Tu+HKVO6ivncwl5yxwF7pGKK0dTbT9OnhGul4vd5vev6BRZh

gnRSn3LNUZQo0yqrPyj1igpM3A2ivlaNq9E63dy9o0zIEYuRnvGtZP5/iAzYWhd3cNfV5mJJu8QEelmq7iTHV

V4pS+u2LprLlifqJ2oRfiMgcJyj2KqEcw60o2fisulrsfLYSsB5wMflionnoszSCyTYI5oy2lAGygbagkUq0nHuN9rR

2/igtAoWKjjdi+n8Juv71UGKn28tdx50EZ52nrmr97b baldevpal@baldevpal

}


resource "aws_instance" "Ec2" {

ami = "ami-013f17f36f8b1fefb"

instance_type = "t2.micro"

key_name = "provi"

tags = {

Name="provisioner"

}


provisioner "file" {

source = "/home/baldevpal/terraform/templatemo_525_the_town"

destination = "/tmp"

}

provisioner "remote-exec" {

inline = [

"sudo apt-get update",

"sudo apt-get install apache2 -y",

"sudo systemctl start apache2",

"sudo systemctl enable apache2",

"sudo cp /tmp/templatemo_525_the_town /var/www/html",

"sudo systemctl restart apache2"

]

}

connection {

host = self.public_ip

user = "ubuntu"

type = "ssh"

private_key = file("/home/baldevpal/terraform/provi")

}

}

 

Step3:Now after writing the script, the Terraform INIT command has to be run.

$ sudo terraform init

                                                         

Terraform 101 | by Shanika Perera | Towards Data Science

 

Step4:The Terraform plan command has to be run.

 

  
       $ sudo terraform plan

 

Terraform - New way of Infra Orchestration Part 1 - powerupcloud

 

 

Step5:The Terraform apply command has to be run.

 

          $ sudo terraform apply

    Terraform AWS: Up and Running in 25 Minutes - Codemotion Magazine

 


About Author

Baldev Pal

Baldev is a DevOps Engineer. He has Knowledge of Linux, AWS, Docker, Jenkins, Kubernetes, Terraform, and Git.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us