Getting started with Terraform and Infrastructure as Code

Getting started with Terraform and Infrastructure as Code

What is Infrastructure as Code

Infrastructure as Code (Iac) is a process and practice to provision infrastructure using codes and pushing the same to a versioned repo like git. This helps us to achieve a state where we can provide the same infrastructure in an environment with no or very little changes to the code. This state is known as automation.  As the code is in git repo rollback and tracking comes as an addon.

So how can I start with IaC the answer is Terraform.

What is Terraform

Terraform is a cloud provisioning tool which supports multiple cloud providers like  AWS, GCP, and Azure. Terraform is backed by HashiCorp with active development to keep up with the latest changes and options of the cloud provider so there is a very rare chance it will be out of fashion.

It uses a very simple declarative syntax to provision the architecture.

resource "aws_instance" "my-aws-instance" {
ami           = "ami-03221428e6676db69"
instance_type = "t2.nano"
key_name      = "my-key"
subnet_id     = "subnet-id"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 8
}
vpc_security_group_ids = ["my-sg-id"]
}

The above code will spin a t2.nano server instance on AWS in minutes without any manual intervention. See how simple it is.

Why use Terraform

You may ask when there are tools like AWS CloudFormation and OpenStack Heat are already available why use Terraform. Also when chef and puppet also can provision infrastructure why learn one more tool. It is because :

  • Terraform is integrated with multiple vendors and a with a single tool you can provision multiple things like you can use AWS for computing and Cloudflare for DNS whereas with tools like AWS CloudFormation and OpenStack Heat will not help you.
  • Terraform works on state file which let’s terraform keep track of the provisioned infrastructure.  Suppose you launched 5 servers using Terraform but after some time you need two more. In this case, you only need to change the count, terraform will launch only 2 more instances and update the state file. This is not possible using chef and puppet.

Getting started with Terraform

Although getting started with Terraform is very easy, still you must fulfill below requirements:

  • Knowledge of AWS and various resources that we are going to spin up
  • AWS CLI configured – Although CLI is not required, we will be using the access keys from the AWS CLI

Install Terraform

Go to https://www.terraform.io/downloads.html and download binary for your OS and make sure the binary is added to PATH variable. Once done you will get below output in the terminal.

 

 

 

Install AWS CLI

Follow instructions in Installing AWS CLI . Once completed configure your CLI using

aws configure

Overview of Terraform code

Below is the generic terraform code.

resource "resource-type" "name" {
      "property" = "value"
      "property" = "value" 
       ....
       ....
}

Let me explain what the code means:

  1. resource-type :
    Here we mention the vendor and resource type.
  2. name :
    This is the name of the resource is only used within the code for references.
  3. property :
    These are the resource specific properties and vary from resource to resource for e.g CloudFront does not any ami property as it does not makes any sense.

Launch EC2 with Terraform

Now we are all set to spin up an instance using terraform.

provider "aws" {
  region  = "ap-southeast-1"
}

resource "aws_instance" "web" {
ami           = "ami-03221428e6676db69"
instance_type = "t2.nano"
key_name      = "my-key"
subnet_id     = "subnet-id"
associate_public_ip_address = true
root_block_device {
volume_type = "gp2"
volume_size = 8
}
vpc_security_group_ids = ["my-sg-id"]
}

save this file as webserver.tf and replace the properties as per your requirement. Then in the same directory run

terraform init

This will install all the required plugins for AWS. Post that run

 terraform plan

You will see output like

This will show the changes terraform is going to make on your behalf. Once satisfied run

 terraform apply

Here terraform will prompt for a yes or no. If you see everything is fine, enter yes and within no span of time instance will be launched. The beauty of terraform comes to life post apply. If you run apply again also it will make no changes because of the state files. If you check the directory where apply was executed, you will see two extra files

terraform.tfstate
terraform.tfstate.backup

These files keep all the aspects of the resource which were provisioned using terraform so that when you run apply again it can compare the clouds and local systems version. Always keep in mind any changes made manually to infra provisioned using terraform which be overwritten by terraform apply. This could also mean replacing the existing resource by termination.

Hope you were able to understand the basic terraform flow. We will cover more advanced topics like variables and interpolation in upcoming blogs.

Leave a Reply

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