Quick Introduction to Terraform

Introduction

There are many tools and frameworks available for provisioning infrastructure on the cloud. One of the most popular cloud agnostic tool is Terraform. It is a vital piece of Infrastructure as a Code Service and could be used to tear up or tear down infrastructure.
We will learn about some basic nuances of Terraform in this post. 

Installing Terraform:

Terraform could be installed by finding the appropriate package and following instructions. (Refer https://www.terraform.io/intro/getting-started/install.html). 
In this post, I will be using Terraform on CentOS.

Getting Started:

Let us try to get our hands dirty. Create a directory, 'scripts' and cd into that.

Run 'terraform --version' to confirm if terraform is successfully installed.

Let us create our first terraform file. Terraform files have an extension of .tf. 
So, create a file 'first.tf'.
Let us create a variable called 'name' and try to print it out.


Note the syntax, the variables should be referred with a prefix 'var' and variables can have default values.
Before we can start running this file, we need to initialize terraform. Terraform init  is used to initialize a new or existing Terraform configuration.
The command to initialize is 'terraform init'


Creating a Terraform Plan:

Now, to run this file, we need to create a plan first. A plan generates and shows an execution plan. It is always advisable to create a plan as it gives an opportunity to review what resources are being created.

We will save the plan to a file and then apply changes using the plan.
Commands:

terraform plan -out first.terraform

terraform apply first.terraform


When you run the 'apply' command, we will get a result as below:
"Apply complete! Resources: 0 added, 0 changed, 0 destroyed."

Terraform Providers:

Providers are responsible in Terraform for managing the lifecycle of a resource. i.e creating a resource or updating it etc. There are a lot of cloud providers which are implemented by Terraform. It supports AWS, Google Cloud to name a few.

Sample provider configuration(AWS):

Get the AWS Secret key and access key and set it into environment variables:

export AWS_ACCESS_KEY_ID=<YOUR_KEY>
export AWS_SECRET_ACCESS_KEY=<YOUR_KEY>

Create a new file 'second.tf' with the following content:




Note that we are trying to create a EC2 instance of type t2.micro and tagged as 'my-vm-automated'.
Note that before creating a plan file, we need to run 'terraform init' again. When we ran it first time, the provider was not there and when we change or add a provider, we will need to initialize again.
Create a plan file and apply the plan file.

terraform plan -out second.terraform

terraform apply second.terraform

You will see that terraform will show what resources are being created.

Finally you would see:
"Apply complete! Resources: 1 added, 0 changed, 0 destroyed."


Terraform State:


When you apply changes, you would notice that a 'terraform.tfstate' file is created.
This file is used by terraform to save the current state of the infrastructure. This is used when we run subsequent files and hence terraform will know what changes to apply.

Now let us try to rerun the same terraform plan again:
terraform apply

You will get a message indicating, the plan is older than the current state and cannot be run. Terraform gets this insights through the terraform.tfstate file and verifies if the plan is latest. If not, it will not run the plan.
Now let us try to simply apply the terraform files. 
terraform apply

We will see that the no resources are added or deleted, since we have not made any changes to terraform file.
"Apply complete! Resources: 0 added, 0 changed, 0 destroyed."

Now login to AWS console and verify that the EC2 instance is created.

Destroy resources:

Run the command 'terraform destroy'.
It will check the state file and notify you that it is going to destroy those resources as below:
Once you confirm, it will delete the EC2 instance created.

Creating an EC2 instance with a key pair:

Create a key pair using puttygen. (Say a file myec2keypair.pem)
Update the above configuration to refer to that key pair.


Next time you want to create an infrastructure on AWS, resist the temptation of using AWS CLI or GUI....Happy Terraforming!!

Comments

Popular posts from this blog

Pivotal Cloud Foundry (PCF) Integration with Elastic Cloud Storage (ECS)

Restful code example using Spring MVC

Spring Integration - Bulk processing Example