Infrastructure as Code : Terraform

Infrastructure as Code : Terraform

What is Infrastructure as Code?

Infrastructure as code is a concept in the operations side of application development and delivery. It borders round provisioning supporting infrastructure or resources for the smooth running of application with code or configuration files. The goal is to make the provisioning and management of this resources easier. Helps improve making changes to the configuration of resources.

So, we can define infrastructure as code as

The act of provisioning hardware, network or security resources to support the adequate running of applications in the cloud using tools that helps configure them in files.

The popular tools for infrastructure as code are:

  1. Terraform
  2. CloudFormation

Terraform as an Infrastructure as Code(IaC) tool

As rightly mentioned above Terraform is one of the tools that help define your infrastructure components and their dependencies on each other and eventually apply the definitions to physical infrastructures via the cloud platform AWS, GCP, Azure. This allows to build, change and manage infrastructure in way that is repeatable and reusable. We can in-turn share the journey of infrastructure changes with teams mates. All these forms the reason for the shift towards IaC.

Terraform is HashiCorp's infrastructure as code tool. It lets you define resources and infrastructure in human-readable, declarative configuration files, and manages your infrastructure's lifecycle.

i. One of the very prominent edge terraform has over a tool like CloudFormation is that it works across multiple cloud platforms while the other is a tool by AWS which works for only AWS.

ii. It provides an easy-to-read configuration language that is used to define infrastructures. Therefore, it is easy to learn. Configurations can also be written in yaml format.

iii. Terraform automatically creates a local state file that keeps track of changes made to configuration during deployment, hence you have access to an history of changes which can be shared with others in the team.

iv. Configuration files can be committed to version control systems to allow for collaboration.

With terraform you can manage resources on cloud platforms and other platforms with their Application Programming Interface. Terraform does this with Providers. It is in record that there are over 1000 providers written so far and can be found in terraform registry. The icing on the cake is if there is a provider you need that does not exist, especially for developers into open source, you can write your own. Some of the existing providers are : AWS, GCP, Azure, Vim, Github, Alibaba Cloud, Datadog etc.

Using Terraform

You need to download terraform for your specific OS by visiting terraform.io/downloads.html

Install on Mac OS using Homebrew

First install hashicorp tap

 $ brew tap hashicorp/tap

The next step is to install terraform with hashicorp/tap/terraform

$ brew install hashicorp/tap/terraform

Now to ensure you have the latest version of terraform, update brew first with:

$ brew update

You can now run an upgrade command for terraform, to make sure you are using the current version.

$ brew upgrade hashicorp/tap/terraform

Install Terraform on Windows with Chocolatey

Chocolotey most often times comes with your windows OS, so to verify open up windows powershell and type 'choco' they press enter. If you get the version of choclatey back, then chocolatey is installed

Now install terraform by running

choco install terraform

Installing on Linux

Update the system first

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl

Add hashicorp GPG key. Get it here apt.releases.hashicorp.com/gpg

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

Add the official hashicorp respository

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Update to add the respository, then intall terraform CLI

sudo apt-get update && sudo apt-get install terraform

Verify Installation by running

terraform --help

If installed, you should see the commands you can use with terraform and their description else go through the steps above more carefully.

To provision infrastructure in the cloud, you need the following set:

i.) An AWS account and credentials you will get from the IAM dashboard on the console (docs.aws.amazon.com/general/latest/gr/aws-s..) ii.) Terraform CLI (1.2.0+) installed iii.) AWS CLI installed

Example Infrastructure Building with Terraform

Create a directory to define your terraform infrastructure

mkdir terraform-example

Go into this base directory

cd terraform-example

Create a file usually name main.tf which would be used to describe the infrastructure

touch main.tf

main.tf file contains the following

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region  = "us-west-1"
}

resource "aws_instance" "app_server" {
  ami           = "ami-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "MyAppServerInstance"
  }
}

In the code above:

The Terraform block defines the terraform settings e.g. version and the providers to be used to build our infrastructure. By default, Terraform pulls and installs providers from the Terraform Registry . In above example configuration, the aws provider's source is defined as hashicorp/aws, which is shorthand for registry.terraform.io/hashicorp/aws.

The providers use the resource block to define the components of your infrastructure. It is possible to use multiple provider blocks in your Terraform configuration to manage resources from different providers. Also, it is very possible to use different providers together. For example, you could pass the IP address of your web server on AWS to a monitoring resource from Prometheus or DataDog.

The resources block use resource blocks to define components of your infrastructure. A resource might be a physical or virtual component such as an EC2 instance, or it can be a logical resource such as a Heroku application.

In our example the resource block has two strings before the curly braces, the first string "aws_instance" is the resource type while the second string is the resource name "app_server" i.e. a name you a giving that instance.

Initialize the directory

Now when our terraform file is well written, we can initialize the repository- this will be by navigating to the root of the folder where the configuration file is and running terraform init.

Initializing a configuration directory downloads and installs the providers defined in the configuration, which in this case is the aws provider.

Initialize the directory.

The above operation helps terraform to download "aws" provider and installs it in a hidden subdirectory of your current working directory, named .terraform. The "terraform init" command prints out which version of the provider was installed. Terraform also creates a lock file named .terraform.lock.hcl which specifies the exact provider versions used, so that you can control when you want to update the providers used for your project.

Run terraform plan to see how what infrastructure will be created

Create the infrastructure

Run terraform apply and the infrastructures get created.

Run terraform show to display the current state of the infrastructure

The terraform state file is sensitive file that should be kept local and only accessible by trusted users, in production, it should be stored remotely in Terraform Cloud or Terraform Enterprise.

I belie this will get you started with terraform. For more context to the possibilities of terraform visit the official document to learn more learn.hashicorp.com/collections/terraform/a..