DEV Community

Ikoh Sylva
Ikoh Sylva

Posted on

AltSchool Of Engineering Tinyuka’24 Month 10 Week 2

If you missed out on our previous session you can catch-up here. This week, we took a dive into Terraform and what makes it special.

Image of an image in space

A Comprehensive Deep Dive Into Terraform

In today’s cloud-driven world, engineers are moving away from manual provisioning and embracing automation at every layer of infrastructure. At the center of this revolution stands Terraform, one of the most powerful Infrastructure-as-Code (IaC) tools ever created. Terraform enables engineers to design, build, and manage infrastructure using code ensuring environments are consistent, repeatable, scalable, and automated.

This article provides a complete, beginner-friendly yet deep dive into Terraform:

  • What Terraform is

  • How the CLI works

  • Understanding Terraform State

  • Terraform’s core concepts and language

  • Real examples

  • A deeper dive into its architecture and workflow

If you want to step into DevOps, Cloud Engineering, or SRE Terraform is a skill you should master.

1. What Exactly Is Terraform?

Terraform is an open-source IaC tool created by HashiCorp. It lets you define your entire infrastructure using a simple, declarative language called HCL (HashiCorp Configuration Language).

Instead of clicking buttons on AWS, Azure, or GCP dashboards, you write code that describes the infrastructure, then Terraform creates it for you.

Example

Instead of manually creating an AWS EC2 instance through the console, you can write:

resource "aws_instance" "web" {
  ami           = "ami-12345"
  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode

Then run:

terraform apply

Terraform reads the code → talks to AWS → creates the server.

Key Benefits

✔️ Infrastructure consistency
✔️ Faster deployments
✔️ Version-controlled infrastructure
✔️ Automated changes
✔️ Works across multiple clouds
✔️ Predictable workflows

Terraform doesn’t just build infrastructure it models complex architectures in a clean, human-readable way.

2. Terraform CLI (Command-Line Interface)

The Terraform CLI is how engineers interact with Terraform. It’s simple, consistent, and predictable.

Here are the most essential commands:

terraform init

Initializes your project, downloads providers, sets up plugins.

Think of it as: “Preparing the environment.”

terraform plan

Shows what Terraform wants to create, modify, or destroy.

Think of this as a preview or dry run.

terraform apply

Actually builds the infrastructure.

Once you confirm, Terraform makes everything real.

terraform destroy

Tears everything down.

Useful when testing, learning, or cleaning up environments.

terraform fmt

Formats your Terraform code automatically.

terraform validate

Checks your configuration for errors.

These commands form the backbone of Terraform usage every engineer uses them daily.

3. Terraform State (One of the Most Important Concepts)

Terraform manages infrastructure using a state file (terraform.tfstate).

This file is the single source of truth about what Terraform has created.

Why Is State Important?

Terraform compares:

  • What you want (your code)

  • What exists (state file)

Then it decides what changes to make.

Example

If your code says:
"Create 1 server,"

and your state file already has that server,
Terraform will do nothing.

If your code says:
"Create 3 servers,"

Terraform checks the state and adds 2 more.
Types of State Storage

  • Local State – default, saved on your machine

  • Remote State – stored in S3, GCS, Azure Blob, Terraform Cloud
    (recommended for teams)

Real-World Example

In an AWS project, storing state in S3 ensures:

  • Multiple engineers can collaborate

  • The state is backed up

  • Changes are tracked securely

  • You avoid conflicting infrastructure changes

State files are the backbone of Terraform automation. Without state, Terraform wouldn’t know what to update.

Image of the galaxy

4. Terraform Language & Core Terms

Terraform uses HCL, a clean, easy-to-read declarative language.

Below are the most important building blocks.

4.1 Providers

Providers allow Terraform to talk to cloud platforms.

Examples:

  • aws

  • google

  • azure

  • digitalocean

  • docker

A provider acts like a plugin that knows how to create resources in that platform.

provider "aws" {
  region = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

4.2 Resources

Resources are the actual things Terraform creates:

  • servers

  • buckets

  • networks

  • containers

  • firewalls

Example:

resource "aws_s3_bucket" "logs" {
  bucket = "my-log-bucket"
}
Enter fullscreen mode Exit fullscreen mode

4.3 Variables

Variables make your Terraform reusable.

variable "region" {
  default = "us-east-1"
}
Enter fullscreen mode Exit fullscreen mode

4.4 Outputs

Outputs display results after deployment.

output "public_ip" {
  value = aws_instance.web.public_ip
}
Enter fullscreen mode Exit fullscreen mode

4.5 Modules

Modules are reusable Terraform packages.

Example: A module that sets up:

  • VPC

  • Subnets

  • Routing

  • Security groups

Modules allow you to reuse architecture patterns across projects essential for scaling infrastructure.

5. Deep Dive Into Terraform (How Terraform Actually Works)

Terraform runs in a four-step lifecycle every time you apply changes:

5.1 Initialization

Terraform downloads providers and ensures your workspace is ready.

5.2 Dependency Graph

Terraform automatically builds a graph of resources.

Example:

If you create:

  • VPC

  • Subnets

  • EC2 inside subnet

Terraform knows:

  1. Create VPC
  2. Then subnets
  3. Then EC2 instance

It figures out the order, you don’t tell it.

5.3 Execution Plan

Terraform compares:

  • your desired infrastructure (your code)

  • your current infrastructure (state file)

Then shows what it will change.

5.4 Apply

Terraform then:

  • creates new resources

  • modifies existing ones

  • deletes unused ones

All changes are recorded in the state file.

Real-World Example: Terraform in a Company

For instance, if a company wants to deploy:

  • 3 AWS EC2 instances

  • 1 RDS database

  • 1 load balancer

  • 1 S3 bucket

In the old days, this could take hours manually.

With Terraform:

  • Write the configuration once

  • Commit it to Git

  • Run terraform apply

In seconds, everything is deployed.

If the team wants to scale EC2 from 3 → 6:

  • Change a number in code

  • Apply again

Terraform automatically adds 3 more servers without breaking anything.

This is why big companies like Uber, Spotify, Coinbase, Airbnb, and Stripe use Terraform heavily.

Image of an advanced satelite technology

Terraform revolutionizes how infrastructure is managed. It shifts teams from manual, error-prone tasks to automated, repeatable, scalable workflows.

Learning Terraform opens doors to roles in DevOps, SRE, Platform Engineering, Cloud Architecture, and Automation.

Understanding the CLI, state, providers, resources, modules, and deeper Terraform processes gives every beginner the power to build production-grade infrastructure with confidence.

Terraform is truly the language of modern cloud engineering.

I’m Ikoh Sylva, a passionate cloud computing enthusiast with hands-on experience in AWS. I’m documenting my cloud journey from a beginner’s perspective, aiming to inspire others along the way.

If you find my content helpful, please like and follow my posts, and consider sharing this article with anyone starting their own cloud journey.

Let’s connect on social media. I’d love to engage and exchange ideas with you!

LinkedIn Facebook X

Top comments (0)