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.

Prerequisite:

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
or

[js]
$ cd /opt
$ wget
$ unzip terraform_0.3.5_linux_amd64.zip
$ export PATH=$PATH:/opt/terraForm/
[/js]

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

[js] $ terraform [/js]

terra1

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

variables.tf

( we’ll define variables here )

main.tf

( will contained details about infrastructure)

output.tf

(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 variables.tf file

variables.tf

[js]
variable "access_key"{}
variable "secret_key"{}
variable "key_name"{}
variable "key_path"{}

variable "aws_region"{
description="Region to Launch Server’s"
default="us-east-1"
}

variable "aws_amis"{
default = {
us-east-1="ami-9eaa1cf6"
}
}
[/js]

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

 

terraform.tfvars

[js]
access_key=AKIAI****************
secret_key=wJalrXUtnFEMI/********/bPxRfiCYzEXAMPLEKE
[/js]

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

We’ll define provider/resources inside main.tf file.

main.tf

[js]
provider "aws"{
region="${var.aws_region}"
access_key="${var.access_key}"
secret_key="${var.secret_key}"
}

resource "aws_security_group" "default"{
name="terraform-sg"
description="Created by terraform"

ingress{
from_port = 22
to_port = 22
protocol= "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress{
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

}
resource "aws_instance" "web"{

connection={
user="ubuntu"
key_file="${var.key_path}"
}
instance_type="t2.micro"
ami="${lookup(var.aws_amis, var.aws_region)}"
key_name = "${var.key_name}"
security_groups= ["${aws_security_group.default.name}"]
tags{
Name="Terraform-EC2"
}
provisioner "remote-exec"{

inline=[
"sudo apt-get -y update",
"sudo apt-get -y install nginx",
"sudo apt-get -y install php5-fpm",
"sudo service nginx restart"
]
}
}
[/js]

You can see in main.tf 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]

terraform2

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

 

output.tf

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

[js]
output "address"{
value="${aws_instance.web.public_dns}"
}
[/js]

Step 4) Launch Infrastructure

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

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

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

You will get this output if everything goes well.
terrraf3

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

terraf4

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.

FOUND THIS USEFUL? SHARE IT

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.

    Reply

Leave a Reply

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