Tuesday, December 13, 2016

My Guide to terraform - Part 3

By now we know what is Terraform, why it is there and how to create a resource with it. In this post, I'm going to modify resource which I created and lastly destroy the infrastructure we create.

Updating an infrastructure
Let's modify the instance type. Previously we create an instance with t2.micro size so now I need something bigger than that, so I modify my previous script.


resource "aws_instance" "example" {
  ami           = "ami-13be557e"
  instance_type = "t2.medium"
}

First thing first, before do any thing to your infrastructure, execute a dry run.

It clearly specifies there will be an update in resources ,( -/+ ) symbol stands that.

instance_type: "t2.micro" => "t2.medium" (forces new resource)

This is what we expected, whenever an update happens on ec2 instance type AWS destroy existing ec2 and gives us a new instance. Let me apply it and check the status.


As we expected our infrastructure is changed and new resource with t2.medium is available.


Destroying an infrastructure.

Before moving to destroy I' wanted to do an experiment. Usually, If you need to modify or delete an AWS resource you need an ID but in our previous example we didn't specify any identification, so where they come from. Definitely, it should be retrieved from the status file. So I'm going to delete the status file and apply another instance size change.


Then I execute terraform show
It gives me there is no status.
Then I execute terraform apply
This time it gives me shine new instance instead of updating existing instance.

So keep in mind, never ever delete the status file, that why terraform also keeping a backup file whenever a change happens.So the best place to keep this is your repository. Make sure to commit this status file along with other source code.



Removing an infrastructure also soo easy with terraform. Same steps as before, first we change the code, go for a dry run then apply the changes.
This time dry run is little bit different
terraform plan --destroy
to actual deletion
terraform destroy

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

My Guide to terraform.

What is terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. - Terraform start guide.

Well, this briefly explains what it capable of. So let's look at some of the key goals of Terraform as per the author of this nice technology.


  • Unify the view of resources using infrastructure as a code.
  • It's all about writing some code to model your infrastructure. You specify the resource on code snippet and describe it and hand over to Terraform to create it. The beauty of modeling an infrastructure as a code is, it brings all other advantages we got from a source code.Simply we can keep them on a repository, version them, review them, integrate with a CI/CD pipeline, automate the tests on infrastructure.

  • Support the modern data center ( IaaS, PaaS, SaaS )
  • It's capable of handling any of the above, as an example

    • IaaS --> EC2 is an infrastructure as a service by the AWS.
    • PaaS --> AWS OpsWork.
    • SaaS --> RDS.
    Terraform can integrate with any on those services.

  • Expose a way to safely and predictably change the infrastructure.
  • With Terraform you don't need to go and create the infrastructure. You can predict the infrastructure by dry running or here we called it plan. It gives you a report on your infrastructure and how it will be once you execute the script.Then you can review the changes and safely build or upgrade the infrastructure without affecting to any of existing.

  • Provide a workflow that is technology agnostic.
  • You don't have to bound to any specific provider. You can instantiate an ec2 which is AWS and you can use some other platform to create a database.


If you already playing with the infrastructures you should have plenty of questions on Terraform because you have played with other technologies which sound similar to Terraform.

It's not Chef or Puppet, both of them are cool technologies where we use to install and manage software on a hosts in other words they are managing configurations but Terraform is not.But you can use any of those configuration management technologies along with Terraform to configure the infrastructure which created with it.

CloudFormation, yes it has some similarities. Terraform inspired by the problem they solved the problem of modeling infrastructure as a code. But it is limited to a specific provider, you can't create a hybrid infrastructure with CloudFormation.

SDK like python boto empowered developers to access cloud providers in a programmatic way but terraform is not used for pragmatic access to cloud its simple infrastructure modeling in a human friendly way.

I think this is enough for a single blog post will meet you soon with another blog post with my hands on experience with terraform.