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
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.
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
![](https://hestia.ghost.io/content/images/2023/08/image-167.png)
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
- 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.
![](https://hestia.ghost.io/content/images/2023/08/image-181.png)
From a different perspective, the sequence of events resembles the one provided below:
![](https://hestia.ghost.io/content/images/2023/08/image-182.png)
Step 1: Write TF File.
The TF file is a configuration file and contains the declarations about what needs to be provisioned.
- 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:
![](https://hestia.ghost.io/content/images/2023/08/image-189.png)
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.
Step 2: Initiate Provisioning Process.
TF goes through the following distinct stages during its provisioning flow.
![](https://hestia.ghost.io/content/images/2023/08/image-186.png)
- 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
![](https://hestia.ghost.io/content/images/2023/08/image-187.png)
Plan
![](https://hestia.ghost.io/content/images/2023/08/image-188.png)
Apply
Where the proverbial rubber hits the road. By typing
terraform apply
we can kick start the actual provisioning of resources.
![](https://hestia.ghost.io/content/images/2023/08/image-190.png)
Step 3: Check EC2 instance was provisioned.
We can now confirm, through AWS console, that our EC2 instance has been provisioned:
![](https://hestia.ghost.io/content/images/2023/08/image-191.png)
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.
![](https://hestia.ghost.io/content/images/2023/08/image-192.png)
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.