Launching Infrastrucure using Terraform (Infrastructure as Code)

26 / Dec / 2014 by Vikash Jha 3 comments

Terraform is a tool for creating, managing and versioning the infrastructure effectively. Terraform provides a common configuration to launch infrastructure of physical servers, DNS, Email Servers. With terraform, we will be launching servers using simple file based configuration ( JSON based ) and maintain versioning for each launch on any version control software.

This tool aims to take the concept of “Infrastructure as Code”

This can be used with various providers, including AWS, Digital Ocean, Heroku etc. In this article,we will be provisioning the servers using AWS.

This section walks you through the step by step guide for launching infrastructure using terraform on AWS.


1) Create an IAM user with full Administrative Access. ( Since we are going to launch and destroy server’s)

2) Create a key pair on AWS Management Console or via CLI which will be used for connecting to the instance during the launch

Step 1) Install Terraform

Step 2) Configure Variables

Step 3) Launch Infrastructure

Step 4) Push the code to git ( Maintain Versions of Insfrastructure )

Let’s break down the steps 🙂

1) Install Terraform

You can download it here

$ cd /opt
$ wget
$ unzip
$ export PATH=$PATH:/opt/terraForm/

Terraform is downloaded and installed. Just run to check whether it is installed or not

[js] $ terraform [/js]


You will get the above output.

Terraform configuration are simple text file in JSON format and has .tf extensions

We’ll create three .tf files

( we’ll define variables here )

( will contained details about infrastructure)

(Expected Output)

We will create one terraform.tfvars files which will contain secret informations (access_key/secret_key) and put this in .gitignore

We’ll define variables inside file

variable "access_key"{}
variable "secret_key"{}
variable "key_name"{}
variable "key_path"{}

variable "aws_region"{
description="Region to Launch Server’s"

variable "aws_amis"{
default = {

As you can see we are using variables name aws_region and aws_amis with given value.

access_key and secret_key is placed in terraform.tfvars files in below format




We have created a key-pair on Management console named “terraform” and downloaded terraform.pem

We’ll define provider/resources inside file.

provider "aws"{

resource "aws_security_group" "default"{
description="Created by terraform"

from_port = 22
to_port = 22
protocol= "tcp"
cidr_blocks = [""]
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [""]

resource "aws_instance" "web"{

ami="${lookup(var.aws_amis, var.aws_region)}"
key_name = "${var.key_name}"
security_groups= ["${}"]
provisioner "remote-exec"{

"sudo apt-get -y update",
"sudo apt-get -y install nginx",
"sudo apt-get -y install php5-fpm",
"sudo service nginx restart"

You can see in we have few sections.

1) provider : defines the provider, In our case its “AWS”
2) resource : block defines a resource that exists within the infrastructure. A resource might be a physical component such as an EC2 instance,
3) provisioner: User data scripts.

Our configuration is complete. Now we’ll check which how terraform builds the resource that we have declared in our configuration files.

for that we have to execute plan command

[js] $ terraform plan [/js]


This Output show’s that, Terraform will launch a t2.micro EC2 Server with ami-9eaa1cf6.

In we will be defining the output variables that’ll be displayed after successful launch.

output "address"{

Step 4) Launch Infrastructure

Since we haven’t received any error message while terraform plan
Now we’ll apply the changes.

$ terraform apply -var ‘key_name=terraform’ -var ‘key_path=~/terraform/terraform.pem’

Note: We are passing pemfile name and path as an argument ( Recommended ), instead of declaring on using path_value inside

You will get this output if everything goes well.

Now on Web Console, You can see a instance is in launching state


Push all your files to git to maintain versions of your infrastructure configuration. Make sure to add terraform.tfvars to your .gitignore if you are placing secret info on this file

Leave a comment if you have any questions regarding this setup.


comments (3)

  1. Nishant Sharma

    Terraform – to launch infrastructure of physical servers, DNS, Email Servers ????
    or providing common configuration to launch infrastructure FROM physical Server TO DNS/Email servers.


Leave a Reply

Your email address will not be published. Required fields are marked *