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 Hashicorp. It 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.
- terraform init: This command is used to initialize the working directory containing terraform script. The desired specification of an infrastructure is configured here.
- 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
- terraform apply: This command creates a real infrastructure.
- 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.