Amazon Web Services, AWS, Infrastructure as a Code, open source, Terraform

Getting started with Terraform

September 9, 2017

IT has been changes fast. We are moving from owning the resources to renting the resources in cloud. With this roles in companies are also changing rapidly. Administrators are learning to code now. It looks like in future Infrastructure is also going to be code. Write a program for new server.

I started my career as an administrator, later I got interested in PowerShell scripting and now it looks like I am going to be full time developer.

I am interested in a new tool recently which has captured a lot of market, the tool is “Terraform”. I am not sure whether to call it tool or a new language. Terraform is a simple tool which can just plug into your infrastructure and start playing. It is just an executable binary file which you can use to integrate different platform(on premises or cloud) and manage from single point. I like this tool from auditing point also as all the information about your infrastructure is there in single file.

Terraform is an open source tool written in Go programming language. The code compiles down to single binary called terraform.

You can use this binary to deploy infrastructure from your laptop or a build server or just about any other computer, and you don’t need to run any extra infrastructure to make that happen. That’s because under the hood, the terraform binary makes API calls on your behalf to one or more providers, such as Amazon Web Services (AWS), Azure, Google Cloud, DigitalOcean, OpenStack, etc. That means Terraform gets to leverage the infrastructure those providers are already running for their API servers, as well as the authentication mechanisms you’re already using with those providers (e.g., the API keys you already have for AWS).

How does Terraform know what API calls to make? The answer is that you create Terraform configurations, which are text files that specify what infrastructure you wish to create. These configurations are the “code” in “infrastructure as code.”

Let us just get our hands dirty.

You can download Terraform from here. After downloading you need to add this path to your path variable.

I am going to use AWS free account here.

Create IAM user in AWS and assign Administrator role to it. You will need access key and secret key of this user to proceed further.

The first file we will create is “instance.tf”. This file will have all the information which we will use to create an EC2 instance in AWS.


provider “aws” {
access_key = “ACCESS_KEY”
secret_key = “SECRET_KEY”
region = “eu-central-1”
}

resource “aws_instance” “VirtualSingh” {
ami = “ami-958128fa”
instance_type = “t2.micro”
}

Replace “ACCESS_KEY” and “SECRET_KEY” with your keys. If you are in different region change it. You will need to search ami id for your region also. The image I am using here is Ubuntu image. I used this link to locate the AMI.

Open command prompt and type below command

terraform.exe init

You output would be similar to:
article1

Now you are ready to create your first EC2 instance using terraform.

When ever you execute the apply command a new file is created in same directory called terraform.tfstate. This file all the information about infrastructure. If you delete this file, you cannot manage your infrastructure using terraform. You can say this is brain of terraform.

There is one more file which is created, which is terraform.tfstate.backup. This contains information of state prior to commitment. So that you can role back if you want to.

Now you need to execute below command
terraform.exe apply

You will get summary of resources which would be created as below.
article2

article3

If you are using AWS free account then you should delete the machine.

To delete the machine just execute below command.
terraform.exe destroy

This will destroy all the resources or changes which were committed with instance.tf file.
article4

Happy Coding!

You Might Also Like

No Comments

Leave a Reply