Understanding Infrastructure as Code (IaC) using Terraform: A QuickStart

Knoldus Blog Audio
Reading Time: 4 minutes

As a DevOps Engineer, there might be a requirement of creating Infrastructure over various Cloud Providers including AWS, GCP or Azure. The ineffective way of doing it is simply going over the Provider console and manually create the infrastructure as per the requirement. Do you feel that its the correct way of doing it? Are you looking for a better solution? If your answer is yes, than this blog is for you.

Herein, I’ll be discussing about the limitations with the tradition way and how Terraform solves those for you. So let’s get started.

Limitations of Manual Configuration

  1. It is difficult for you know what resources you have created over Cloud.
  2. Creating those resource manually is a time consuming task.
  3. There are chances of human errors while configuring those resources.
  4. No versioning of resource configuration.
  5. Replicating resources for creating separate environment would be a painful job

I believe these points are enough to get you on the other side of the court. But, now the question arise that how is Terraform helping us here?

Benefits of using IaC

Here, I have just used a term IaC which stands for Infrastructure as Code. What this means is that instead of setting up the infrastructure manually, you use some coding language to set up the whole infrastructure. A formal definition for IaC states this:
IaC is provisioning infrastructure via software to achieve consistent and predictable environments
So, let’s see what the above definition says by stating some of its benefits:

  • Documented Architecture:
    We can keep track of what resources we are creating as they are defined in code.
  • Automated deployment:
    Creation of these resources are quick and automated.
  • Consistent Environments:
    Since there is no human intervention, the spawned up infra remains consistent.
  • Reusable components:
    Replicating the resources for setting up a different environment can be done very quickly as the scripts are already prepared.
  • Source Controlled:
    The infrastructure code is source controlled and hence can be easily versioned.

Got IaC, But What’s Terraform?

Terraform is a tool developed by HashiCorp which helps in achieving the Infrastructure as Code. It helps in building, changing, and versioning infrastructure safely and efficiently. Terraform can not only create new resources for you, but can also import your existing infrastructure into code. Terraform used HCL (HashiCorp Configuration Language) for managing the configuration.
But, Before jumping to its features, Let me tell you its major benefit, and that is that it supports a huge list of Providers. Providers include the plugins that are required to interact with the remote systems. Here are some of its features:

  1. Declarative
  2. Idempotent
  3. Push Mechanism

Let see these 1 by 1 that what they mean:

  1. Declarative:
    This means that we will tell Terraform what all I want, but not the process. We’ll only pass the resources we want and the configuration we required for our infrastructure. The rest will be taken care by Terraform.
    For example: For setting up a VPC, I will only tell Terraform that I want a VPC, with some minimum configurations required like CIDR block and Terraform will automatically create that for us.
  2. Idempotent:
    This means that Terraform maintains the existing state and whenever we pass new configs to set up infrastructure, it will initially check the passed configuration with the existing state and checks what are new creations, updation and destruction in the current state.
    For example: If you have created an instance. Now to create a new instance, you have to update your configuration by adding a new instance, and not by simply asking Terraform to create a new instance.
  3. Push Mechanism:
    This means that we need to push the configurations explicitly to Terraform to make the necessary changes. It won’t pick any by itself.
    For example: After changing the Terraform configuration file, we need to run terraform apply to push those changes.

Terraform Components

  1. Terraform Executable:
    Written in GO. Simply download it and make sure it exist in your PATH variable.
    Verify this via terraform version
  2. Terraform File:
    The configuration files uses HashiCorp Configuration Language (HCL)
    These files uses the .tf extension.
  3. Terraform Plugin:
    These are the plugins of the provider which are used to interact with the remote systems like AWS, GCP, Azure and many more.
  4. Terraform State:
    Last but not the least, the state file. This file contains the current state of infrastructure which is created by Terraform.
    The file extension is .tfstate and is in JSON format.
    Also, One important thing, NEVER CHANGE this file manually.

Terraform Blocks

Variables

These serves as parameters passed to Terraform Resources. We’ll see what resources are later in this section.

## Syntax
variable "name" {
	type = map(string)
	description = "(optional) describe your variable"
	default = {
		key1 = "val1"
		key2 = "val2"
	}
}

## Example
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "aws_region" {
	default = "us-east-1"
}

Provider

Terraform relies on plugins called “providers” to interact with remote systems.

## Syntax: Tf version 0.13 and later
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

## Syntax: Tf version 0.12 and earlier 
provider "aws" {
  version = "~> 3.0"
  region  = var.aws_region
}

Data Sources

Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration. Use of data sources allows a Terraform configuration to make use of information defined outside of Terraform, or defined by another separate Terraform configuration.

## Example
data "aws_ami" "alx" {
	most_recent = true
	owners = ["amazon"]
	filters {}
}

Resources

Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects.

## Example
resource "aws_instance" "ex" {
	ami = "data.aws_ami.alx.id"
	instance_type = "t2.micro"
}

Output

Outputs are like return values for a Terraform module.

## Example
output "aws_public_ip" {
	value = "aws_instance.ex.public_dns"
}

Conclusion

After reading the blog, you must be familiar with what is Infrastructure as Code is and what are its advantages. Apart from that, you are now familiar with what Terraform is, what are its components and some of the blocks Terraform has.
If you wants to practice some hands-on over Terraform, please head over to my Terraform Quickstart Github Repository.

For now, I believe this would be enough for a Kick Start. For furthur information, you can refer the documentation of Terraform as it is very simple and straight forward.
And for any doubts/suggestion, you can contact me directly over yatharth.sharma@knoldus.com.

Also, I would like to thank you for sticking to the end. If you like this blog, please do show your appreciation by giving thumbs-ups and share this blog and provide suggestions on how can I improve my future posts to suit your needs. Follow me to get updates on different technologies.

Written by 

Yatharth Sharma is a Software Consultant at Knoldus Software LLP. He has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. He has a decent knowledge of Java Language and currently working on DevOps technologies/tools like Ansible, Jenkins, Docker, Kubernetes. Apart from programming, he loves listening to rap music.