Automating Infrastructure on AWS Using Terraform

Reading Time: 2 minutes

In this blog, I am going to showcase how to create an infrastructure on AWS using Terraform.  Let’s have a brief introduction of Terraform before jumping to the particular use-case.

What is Terraform?

Terraform is an infrastructure provisioning tool created by HashicorpIt allows you to describe your infrastructure as code, creates execution plans that outline exactly what will happen when you run your code, builds a graph of your resources, and automates changes with minimal human interaction.

In order to create infrastructure, we have to run following commands in the given sequence.

  1. terraform init: This command is used to initialize the working directory containing terraform script. The desired specification of an infrastructure is configured here.
  2. terraform plan: This command creates an execution plan.  It confirms that the set of changes matches your expectations without making any changes to real resources or to the state
  3. terraform apply:  This command creates a real infrastructure.
  4. terraform destroy: This command is used to destroy infrastructure.

How Syntax looks like?

What is the use-case which needs to be solved?

“Launch an AWS EC-2 instance and host a web page on the Nginx server.”

main.tf : This is main terraform script.

vars.tf : This file contains all the environment variables used by our main script.

terraform.tfvars : In this file we add values of the variables which will be used by vars.tf. Before running a script.

Run terraform init, terraform plan and terraform apply to deploy on AWS.

output.tf : This is our output file through which we get various resource information.

When apply finishes, the public dns output variable will show the public dns of an instance.

Open this public dns in your web browser. To see a similar web page as below.

image (2)

References:

https://www.terraform.io/docs/index.html

knoldus-advt-sticker

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!