Introduction
Terraform: the Swiss Army knife of Infrastructure as Code (IaC). If you’ve ever wanted to manage cloud infrastructure like a boss without manually clicking through AWS, Terraform is your new best friend.
It automates the entire process, leaving you more time to drink coffee and pretend you’re fixing production issues. ☕🔥
A Brief History of Terraform
Back in the day (pre-2014), cloud provisioning was a wild, wild west of manual configurations and spaghetti scripts.
Then, HashiCorp entered the scene with Terraform, a tool that changed everything by introducing declarative infrastructure as code. Instead of imperative scripts (do X, then Y), Terraform lets you define the desired end state, and it figures out the rest.
Since then, it’s become the de facto standard for managing cloud infrastructure, outshining other tools like AWS CloudFormation and Ansible for provisioning tasks.
Why is Terraform so popular?
✅ Multi-cloud support – Works with AWS, Azure, GCP, Kubernetes, and even random things like Cloudflare DNS.
✅ State management – Keeps track of infrastructure changes with a state file.
✅ Modular – You can reuse Terraform code across projects.
✅ Easy to learn – HCL (HashiCorp Configuration Language) is simpler than YAML, JSON, or writing Bash scripts at 3 AM.
Feature | Terraform | CloudFormation | Pulumi | Ansible |
---|
Multi-cloud | ✅ Yes | ❌ AWS-only | ✅ Yes | ✅ Yes |
Declarative | ✅ Yes | ✅ Yes | ❌ No (Imperative) | ❌ No (Imperative) |
State Management | ✅ Yes | ✅ Yes | ❌ No | ❌ No |
Programming Language | HCL | YAML/JSON | Python/JS/Go | YAML |
Best For | Cloud provisioning | AWS environments | Devs who like real languages | Configuration management |
If you want to provision multi-cloud infrastructure, use Terraform. If you’re AWS-only, CloudFormation is fine. If you prefer Python, try Pulumi. If you just want to install packages, use Ansible.
1. Provision an AWS EC2 Instance
1
2
3
4
5
6
7
| provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "my_vm" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
|
2. Create an S3 Bucket
1
2
3
4
| resource "aws_s3_bucket" "my_bucket" {
bucket = "my-awesome-bucket"
acl = "private"
}
|
3. Deploy a Load Balancer
1
2
3
4
5
| resource "aws_lb" "my_lb" {
name = "my-load-balancer"
internal = false
load_balancer_type = "application"
}
|
4. Spin Up an RDS Database
1
2
3
4
5
| resource "aws_db_instance" "my_db" {
engine = "postgres"
instance_class = "db.t3.micro"
allocated_storage = 20
}
|
5. Provision a Kubernetes Cluster (EKS)
1
2
3
4
| resource "aws_eks_cluster" "my_eks" {
name = "my-cluster"
role_arn = aws_iam_role.eks_role.arn
}
|
6. Manage DNS Records with Cloudflare
1
2
3
4
5
6
| resource "cloudflare_record" "my_dns" {
zone_id = "your-zone-id"
name = "example.com"
type = "A"
value = "192.168.1.1"
}
|
1
2
3
4
| module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket = "my-modular-bucket"
}
|
1
2
3
4
5
6
| variable "instance_type" {
default = "t2.micro"
}
resource "aws_instance" "my_vm" {
instance_type = var.instance_type
}
|
9. Set Up Auto Scaling
1
2
3
4
5
| resource "aws_autoscaling_group" "my_asg" {
desired_capacity = 2
min_size = 1
max_size = 5
}
|
1
2
3
| output "instance_ip" {
value = aws_instance.my_vm.public_ip
}
|
Key Ideas Table
Concept | Explanation |
---|
Terraform | Open-source IaC tool by HashiCorp |
Multi-cloud support | Works with AWS, Azure, GCP, Kubernetes, and more |
State Management | Keeps track of infrastructure changes |
HCL | HashiCorp’s configuration language for Terraform |
Comparison | Competes with CloudFormation, Pulumi, and Ansible |
Example Uses | Provisioning VMs, Load Balancers, Kubernetes, and DNS |
Reference Links