Tuesday, December 13, 2016

My Guide to terraform - Part 2

During my past blog post on Terraform we discuss what is Terraform and why it is so popular among platform engineers.In this post, we will have a look on how to install Terraform on ubuntu machine and we will discuss instantiate an ec2 instance with Terraform.


Installing Terraform.
  • Step 1: First, we have to download the correct distribution for the operating system. All available distributions are available in the following location url
  • Step 2:It comes as a zip file, that contains binary version of a Terrafrom application so lets unzip the file to a folder in the file system.
    
    cd /home/amith/Documents/Software
    unzip terraform_0.7.13_linux_amd64.zip
    
    
  • Step 3: Now we have to add this binary file path into path environment variable, otherwise, we have to navigate to the directory which contains the distribution in order to execute it.

    PATH=/usr/local/terraform/bin:/home/amith/Documents/Software:$PATH

  • Step 4: execute the following command for checking the installation.If it return set of commands that means we are good to go

    terraform


Use case: Instantiate a t2.micro aws instance

As we discussed previously it's all about writing a source code. As all other source codes terraform also associated with a file extension and usually it is .tf.

vi ec2_create.tf

Before going forward we need few data.

  1. Valid aws access key and a secret key.
  2. Valid aws ami(amazon machine image)
  3. Instance type.

Here is the sample code.

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0d729a60"
  instance_type = "t2.micro"
}

Before move further try to understand the source.There are two ways of defining a resource with Terraform.

  • Terraform format - we use .tf extention
  • JSON format - we use .tf.json.

Why we have two formats and when to use them?

JSON is more machine friendly language so if you plane to generate terraform script in problematically make sure to use JSON format. But if you need a more human-friendly way of defining infrastructure then go with terraform format. Actually, terraform format is a wrapper for JSON, so there is no harm of using any one of them. It's up to you decide the appropriate format for the scenario.


Planing and applying

If you remember the goals we discussed in previous blog post, Terraform support dry runs, by using that feature we can plan the final outcome before actually doing the change.To run a plan you have to navigate into the file location where you define the terraform code.

cd /home/amith/WorkSpace/sandbox/terraform
terraform plan


this will take some time and generate a report.


Explain plan.
Plan: 1 to add, 0 to change, 0 to destroy.
This section summarizes the final outcome. According to this, it says one resource has to create and no any update or delete.


+ aws_instance.example

This section describes which resource going to create, + symbol denote a new resource creation. If it is - that means item is about to remove and if it is +- that means resource about to update.

For more simplicity in this report they use color codes

  • green - items to be created
  • red - items to be removed
  • orange - items to be update


If you look closer you may have seen there are some sections without values.
    availability_zone:        ""
    ebs_block_device.#:       ""
    ephemeral_block_device.#: ""
    instance_state:           ""

Those values will be generated by the provider since this is a dry run those data are not available at the moment.
Now we have a fair understanding of what will be the out come so let's create the resource.

terraform apptly

this process takes some time to complete. Every 10 seconds it update the report.Once this is completed Terraform will create a new file which contains status(metadata) about the infrastructure and saved on the same location - terraform.tfstate.If you plan to share the code make sure to share this file as well.Without this file terraform will note able to do an update or show a status report next time so it's really important to keep this file safe.




To inspect the status.
terraform show

No comments:

Post a Comment