Hello and welcome to a new edition of the Progressive Coder newsletter.
The topic for today - How Terraform Works?
Terraform is one of the most popular infrastructure-as-code tools out there. As more and more organizations move towards IaC, Terraform adoption also increases steadily.
With today’s post, you get a quick peek into the working of Terraform.
How Terraform Works?
Terraform is an open-source IAC tool that lets you build, modify and version infrastructure resources.
The basic principle of Terraform is that you write human-readable configuration code to define your infrastructure. Terraform takes this code and uses it to deploy the infrastructure.
What’s the advantage of this approach?
It makes infrastructure provisioning repeatable and consistent across public, private and hybrid cloud platforms. Repeatability and consistency are extremely important if you value your weekends and don’t want to spend them solving rogue deployment issues.
Here’s an illustration that makes the user journey with Terraform extremely clear.
With Terraform, you can deal with infrastructure code just like your application source code.
You can commit the code to a Github repository with an appropriate branching strategy and establish proper code review processes to handle releases.
Terraform Components
Overall, Terraform is split into two components - Terraform Core and Terraform Plugins.
👉 Terraform Core is a statically-compiled binary written in the Go programming language. This binary is the CLI terraform
and it acts as the entry point for anyone using Terraform.
Here are a bunch of responsibilities carried out by Terraform Core:
processing configuration files
constructing resource graphs
managing infrastructure state
planning execution
communicating with plugins
👉 Terraform Plugins are executable binaries written in Go and are invoked by the Terraform Core over RPC.
Plugins are also of two types - providers and provisioners.
Providers expose the functionality of a specific service such as AWS or Azure. Think of providers as middlemen between the core and third-party services.
On the other hand, Provisioners let us execute scripts or commands on the target machine after it has been created or updated by Terraform. In simple terms, provisioners let us customize the software installed on the infrastructure resource. For example, setting up Nginx on an EC2 instance after it has been provisioned.
A Little Bit of Terraform Code
So, what does Terraform code look like?
Here’s a peek into a typical Terraform configuration file.
provider "aws" {
region = "us-west-2"
profile = "terraform-user"
}
resource "aws_instance" "hello_aws" {
ami = "ami-0ceecbb0f30a902a6"
instance_type = "t2.micro"
tags = {
Name = "HelloAWS"
}
}
Do you find the code pretty intuitive?
This is because Terraform uses a special language known as Hashicorp Configuration Language or HCL.
The language is human-readable (even more than JSON) and forms the core of Terraform.
👉 Can you guess what the above snippet of code actually does?
Let me know in the comments section below.
In case you want to take a deeper dive into Terraform with a project-based learning approach, do check out the Deep Dive on Infrastructure as Code with Terraform & AWS. Link below: