Terraform for the new guys in DevOps...

Terraform is a tool that allows us to automate and manage infrastructure, platforms, and services that run on the platform. It is open-source and was written in Declarative Language.

Declarative means, you don’t have to define the steps to be taken but rather the end results you want. While Imperative is the alternative to Declarative, it will be explained in broader terms later in the tutorial. Terraform is a tool for infrastructure provisioning, for example, while starting a project, setting up infrastructure from the scratch is its responsibility.

Its tasks range from provisioning infrastructure to deploying applications in the correct order. You can list out a terraform task verbally as:

  • Create VPC

  • Spin up servers

  • AWS users and permissions

  • Installing Docker. Etc.

A lot of time, there are several comparisons between Ansible and Terraform, in reality, they are closely related, we can’t take that fact away, but they differ slightly either in strength or what they specialized in particular.

The similarities are

  1. They are both infrastructures as a code.

  2. They are both used for automating the provisioning, configuring, and management of infrastructure.

Differences are

  1. Terraform is mainly for infrastructure provisioning, while Ansible is mainly a configuration tool.

  2. Ansible is a much older tool than Terraform.

  3. Terraform is more advanced in orchestration.

  4. Terraform is better at provisioning infrastructure.

  5. Ansible is better for configuring infrastructure.

  6. Ansible can use both imperative and declarative scripting, while terraform uses declarative scripting.

For utmost performance, DevOps engineers mostly combine the two tools.

Terraform Architecture:

The architecture of terraform consists of two components.

  1. Core

  2. Provider

The core takes the input and figures out plans of what needs to be done.

It has two input sources;

I - Tf-config(terraform configuration): Where you define creation or configuration.

II - State: Keep the up-to-date state of all the current setup of how infrastructure looks like.

The provider provides for;

  1. specific technologies, e.g. AWS/Azure/GCP (Infrastructure as a Service)

  2. Platforms, e.g.Kubernetes (Platform as a Service)

  3. Web, e.g. Fastly (Software as a Service)

Declarative Vs. Imperative Scripts

Declarative

Instead of defining steps, you define the end state in your configuration file: e.g:

  • 7 servers with the following network configuration.

  • AWS user with the following permissions.

Imperative:

Defining what to do one step at a time and how to get it done.

During the initial setup, the two make not much difference, but while updating, declarative becomes very useful.

Let’s get a hang of how it works.

s/n

Imperative

Declarative

1.

Remove 2 servers.

7 servers.

2.

Add firewall configuration.

This firewall config.

3.

Add permissions to AWS users.

User with the following permissions.

Terraform Commands

  1. Refresh: Query infrastructure provider to get the current state.

  2. Plan: Create an execution plan, it is what the CORE use to carry out its operations. Necessary actions to achieve the desired state. Core constructs plan.

  3. Apply: It executes the plan.

  4. Destroy: Destroy the resources or infrastructure.