AltSchool Of Engineering Tinyuka’24 Month 10 Week 2
Source: Dev.to
If you missed our previous session you can catch up here.
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. Terraform enables engineers to design, build, and manage infrastructure using code, ensuring environments are consistent, repeatable, scalable, and automated.
Topics Covered
- 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
resource "aws_instance" "web" {
ami = "ami-12345"
instance_type = "t2.micro"
}
Run the command:
terraform apply
Terraform reads the code → talks to AWS → creates the server.
Key Benefits
- ✅ Infrastructure consistency
- ✅ Faster deployments
- ✅ Version‑controlled infrastructure
- ✅ Automated changes
- ✅ Multi‑cloud support
- ✅ Predictable workflows
Terraform models complex architectures in a clean, human‑readable way.
2. Terraform CLI (Command‑Line Interface)
The Terraform CLI is how engineers interact with Terraform. Below are the most essential commands:
| Command | Description |
|---|---|
terraform init | Initializes the project, downloads providers, sets up plugins. |
terraform plan | Shows what Terraform will create, modify, or destroy (dry run). |
terraform apply | Builds the infrastructure after confirmation. |
terraform destroy | Tears everything down – useful for testing or cleanup. |
terraform fmt | Formats Terraform code automatically. |
terraform validate | Checks configuration for errors. |
These commands form the backbone of daily Terraform usage.
3. Terraform State (One of the Most Important Concepts)
Terraform tracks infrastructure with a state file (terraform.tfstate). This file is the single source of truth about what Terraform has created.
Why State Is Important
Terraform compares:
- Desired state – your HCL code
- Current state – the state file
and decides what changes are needed.
Example
- Code says “Create 1 server” → state already has that server → no action.
- Code says “Create 3 servers” → state has 1 → Terraform 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
Storing state in S3 for an AWS project enables:
- Collaboration among multiple engineers
- Automatic backup of the state file
- Secure change tracking
- Prevention of conflicting infrastructure changes
State files are the backbone of Terraform automation; without them Terraform would not know what to update.
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 enable Terraform to interact with cloud platforms.
provider "aws" {
region = "us-east-1"
}
Common providers: aws, google, azure, digitalocean, docker.
4.2 Resources
Resources are the actual objects Terraform creates (servers, buckets, networks, containers, firewalls).
resource "aws_s3_bucket" "logs" {
bucket = "my-log-bucket"
}
4.3 Variables
Variables make configurations reusable.
variable "region" {
default = "us-east-1"
}
4.4 Outputs
Outputs display values after deployment.
output "public_ip" {
value = aws_instance.web.public_ip
}
4.5 Modules
Modules are reusable Terraform packages that can encapsulate patterns such as VPCs, subnets, routing, and security groups.
5. Deep Dive Into Terraform (How Terraform Actually Works)
Terraform follows a four‑step lifecycle each time you apply changes.
5.1 Initialization
Downloads providers and prepares the workspace.
5.2 Dependency Graph
Terraform builds a graph of resource dependencies automatically.
Example:
If you define a VPC, subnets, and an EC2 instance inside a subnet, Terraform knows to create the VPC first, then subnets, then the EC2 instance.
5.3 Execution Plan
Compares desired infrastructure (code) with current infrastructure (state) and shows the planned changes.
5.4 Apply
Creates, modifies, or destroys resources as needed and records all changes in the state file.
Real‑World Example
A company wants to deploy:
- 3 AWS EC2 instances
- 1 RDS database
- 1 load balancer
- 1 S3 bucket
Traditional manual setup could take hours. With Terraform:
- Write the configuration once.
- Commit to Git.
- Run
terraform apply.
Everything is provisioned in seconds. Scaling from 3 to 6 EC2 instances is as simple as changing a number in code and re‑applying.
Large organizations such as Uber, Spotify, Coinbase, Airbnb, and Stripe rely heavily on Terraform for this level of automation.


