5 min read

Terraform: What it is and how is it used.

Terraform is a deployment technology for provisioning and management of infrastructure using code (also popularily called Infrastructure-as-Code or IaC).

Infrastructure as code is the process of managing and provisioning infrastructure through machine-readable definition files. We use IaC to automate processes that used to be done manually.

Table of Contents

  1. Definition of Provisioning
  2. Terraform and its purpose
  3. Demo: "Hello World" the TF Way

Definition of Provisioning

The word Provision is defined as:

the action of providing or supplying something for use.

When we talk about provisioning within the context of Infrastructure (like servers, VMs, databases, networks etc), we are referring to the actions taken (either sequentially or in parallel) that will install and launch a web server/database instance/network switch etc.

📢
Provisioning of a thing is different than configuring it. We provision a web server (i.e. set up a real or virtual computer with the web server software installed) and once thats done, we configure the different features that are part of the web server software. For example, we configure the ports over which the web server will listen to HTTP messages, we configure the certificates and service accounts that are needed to make the web server actually do something useful for us.

Traditionally, Terraform has been called an Provisioning technology and is allied with Configuration Management tools like Ansible, Puppet, SaltStack and Chef, to automate infrastructure delivery. However, there are certains aspects of configuration that Terraform is also able to handle, and therefore, in some cases, using Configurtation Management tools with Terraform may be considered redundant.

Terraform and its Purpose

Figure 1: Terraform can deploy infrastructure to any cloud or combination of clouds.

Once upon a time, there was a Server Admin. He was the person you and I, as System Engineers or Application Developers, went to asking for a web server or a database to be made available to us. The Server Admin would have to spend time clicking through various folders and files, downloading installables, and then going through the provisioning of software on actual hardware. If the provisioning failed, he would have to do it again. If it passed, he was still on the hook for maintaining what he provisioned. Not that the Server Admin was incapable of doing this, afterall he was hired for specifically this reason.

Scale out the demands being made on the Server Admins time and it becomes clear fairly quickly that he is turning into a bottleneck for infrastructure provisioning. He ends up hiring more people to go through the fairly standard (and repeatable) steps involved in infrastructure set up but the demand-supply cycle gets constantly skewed towards the former. If only he could find a way to optimize his time by automating some of the standard provisioning tasks, he (and his team) could stop getting all fingers pointed at them.

It is for situations like the one detailed above that tools like Terraform (TF) were created. TF does the exact same work as the Server Admin and his team but because it uses code to mimic human actions, provisioning using TF is definitely faster and less prone to human error (of course the assumption here being that the provisioning process is standardized and properly coded for TF to use).

Demo: "Hello World" the TF Way

📢
Technologies required for this demo:
- Terraform (installation guide is here)
- AWS Account Credentials
- AWS Provider for Terraform

This demo is a classical use case for Terraform: deploying a virtual machine (EC2 instance) onto AWS. Using an AWS provider for Terraform, we will make API calls and deploy an EC2 instance.

Figure 2: The flow behind provisioning an EC2 instance using TF.

From a different perspective, the sequence of events resembles the one provided below:

Figure 3: The flow behind provisioning an EC2 instance using TF.
💡
Make sure you have the AWS CLI installed and have already logged in to your AWS account through the command line.

Step 1: Write TF File.

The TF file is a configuration file and contains the declarations about what needs to be provisioned.

💡
A Terraform configurtion file has 3 main blocks/sections:
- Resource: Declares what type of infrastructure element we want to provisioning e.g. VM, Database, Subnet, etc
- Data: Tells TF where to look for additional information, from external sources, that is needed for provisioning the resource.
- Provider: The Cloud Service Provider e.g AWS, Azure or GCP.

The complete file resembles that shown in Figure 4 below:

Figure 4: A simple TF configuration file.

The Resource block

  • The orange box tells TF we want to create an EC2 instance on AWS and we want to call it helloworld. Additionally, we are also telling TF the OS image we want to use for our EC2 (ami-051f7e7f6c2f40dc1), set its computing power to t2.medium and finally, also assign it a tag with key=value combination of Name="HelloWorld".

The Provider block

The provider block declares the Cloud Provider we want to work with and the region in which the EC2 instance should be provisioned.

📢
Configuration files using Data block will be demo'ed in other articles.

Step 2: Initiate Provisioning Process.

TF goes through the following distinct stages during its provisioning flow.

Figure 5: TF goes from Init to Apply, and then Destroy if asked to.
  • Init: TF will make sure all dependencies like Provider-specific plugin are downloaded.
  • Plan: TF will go through the configuration file to assess the new additions/edits to existing infrastructure that are declared.
  • Apply: TF will start the ACTUAL provisioning of infrastructure.
  • Destory: (Optional) In this stage, the infrastructure that was provisioned is destroyed.

Init

Figure 6:terraform init checks for plugins needed and downloads them.

Plan

Figure 7:terraform plan lists all planned new changes, planned updates and destruction to existing infrastructure.

Apply

Where the proverbial rubber hits the road. By typing

terraform apply

we can kick start the actual provisioning of resources.

Figure 8:terraform apply is where the REAL magic happens.

Step 3: Check EC2 instance was provisioned.

We can now confirm, through AWS console, that our EC2 instance has been provisioned:

Figure 9: EC2 instance has been provisioned.

Step 4: Destroy the EC2 instance.

What can lead to birth, in the case of IaC, can also lead to destruction.

Using

terraform destroy

we can reverse the instructions provided.

Figure 10: EC2 instance was destroyed.

I write to remember and if in the process, I can help someone learn about Containers, Orchestration (Docker Compose, Kubernetes), GitOps, DevSecOps, VR/AR, Architecture, and Data Management, that is just icing on the cake.