Terraform
๐ง Core Concepts
1๏ธโฃ Infrastructure as Code (IaC)
You define infrastructure in HCL (HashiCorp Configuration Language):
provider "aws" {
region = "ap-southeast-2"
}
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t3.micro"
}
Then run:
terraform init
terraform plan
terraform apply2๏ธโฃ Declarative Model
You describe what you want, not how to build it.
Terraform:
- Builds dependency graph
- Determines order
- Calculates diff
- Applies minimal changes
3๏ธโฃ Providers
Providers connect Terraform to platforms:
| Category | Examples |
|---|---|
| Cloud | AWS, Azure, GCP, OCI |
| Containers | Kubernetes, Docker |
| SaaS | GitHub, Cloudflare |
| On-prem | VMware, Proxmox |
| Databases | PostgreSQL, MongoDB |
๐ Yes โ Terraform works with AWS, Oracle Cloud, Proxmox, etc.
4๏ธโฃ State File (Very Important)
Terraform tracks infrastructure in a state file (terraform.tfstate).
It stores:
- Resource IDs
- Metadata
- Dependency graph
Why state matters:
- Enables diffing
- Prevents recreation
- Tracks drift
Best practice:
Use remote backend:
| Backend | Use case |
|---|---|
| S3 + DynamoDB | AWS teams |
| Azure Blob | Azure |
| GCS | GCP |
| Terraform Cloud | Managed |
๐ Terraform Workflow
Write config โ Init โ Plan โ Apply โ Destroy
Commands Explained
| Command | What it does |
|---|---|
init |
Downloads providers |
plan |
Shows execution plan |
apply |
Executes changes |
destroy |
Deletes infrastructure |
validate |
Syntax check |
fmt |
Format code |
state |
Inspect state |
๐ How Terraform Works Internally
- Parses HCL
- Builds dependency graph
- Compares desired vs actual state
- Calls provider APIs
- Updates state
๐งฉ Modules (Reusability)
Modules are reusable infrastructure packages.
Example:
module "vpc" {
source = "./modules/vpc"
cidr = "10.0.0.0/16"
}
You can publish modules in:
- Terraform Registry
- Private Git repos
๐ฆ Variables & Outputs
Variables
variable "instance_type" {
default = "t3.micro"
}
Outputs
output "public_ip" {
value = aws_instance.web.public_ip
}
๐ Multi-Cloud Architecture
Terraform supports:
| Cloud | Supported |
|---|---|
| AWS | โ |
| Azure | โ |
| GCP | โ |
| OCI | โ |
| Alibaba | โ |
You can define multiple providers in one config.
๐ Security Best Practices
- Never commit
.tfstate - Use remote encrypted backend
- Use IAM roles (not static keys)
- Use workspaces for environments
- Restrict state access
๐งช Workspaces (Environments)
terraform workspace new prod
terraform workspace select devEnables:
- dev
- staging
- production
โ Advanced Features
1๏ธโฃ Data Sources
Read existing infrastructure:
data "aws_vpc" "default" {
default = true
}
2๏ธโฃ Lifecycle Rules
lifecycle {
prevent_destroy = true
}
3๏ธโฃ Count & For_each
Create multiple resources:
resource "aws_instance" "web" {
count = 3
}
4๏ธโฃ Provisioners (Use Carefully)
Run scripts after creation:
provisioner "remote-exec" {}
โ ๏ธ Not recommended for complex config โ use Ansible instead.
๐ Terraform vs Other Tools
| Tool | Type | Declarative? | Language |
|---|---|---|---|
| Terraform | IaC | Yes | HCL |
| Pulumi | IaC | Yes | Python/TS/Go |
| CloudFormation | AWS only | Yes | YAML |
| Ansible | Config mgmt | Mostly procedural | YAML |
| Kubernetes | Container orchestration | Declarative | YAML |
๐ Terraform vs Kubernetes
| Terraform | Kubernetes |
|---|---|
| Provisions infrastructure | Orchestrates containers |
| Creates clusters | Runs workloads |
| Cloud agnostic | Cluster specific |
They often work together.
๐ Common Problems
1๏ธโฃ State Corruption
Fix with:
terraform state rm
terraform import2๏ธโฃ Drift
Use:
terraform plan3๏ธโฃ Large Teams
Use:
- Remote backend
- Locking
- CI/CD
๐ข Terraform Cloud & Enterprise
Terraform Cloud provides:
- Remote runs
- State storage
- Policy as code (Sentinel)
- Team management
๐ฅ Production Best Practices
Structure
infrastructure/
modules/
envs/
dev/
prod/
CI/CD
- Run
planin PR - Require approval
- Auto apply on merge
Version Pinning
required_providers {
aws = {
version = "~> 5.0"
}
}
๐ When NOT To Use Terraform
- Short-lived experiments
- Very dynamic runtime infra
- Fine-grained config management
๐ง Advanced Patterns
- Blue/Green infra
- Multi-region failover
- Immutable infrastructure
- Policy as Code
- GitOps workflows
๐ Example: AWS VPC + EC2 + RDS
Terraform can build:
VPC
โโ Public Subnet
โโ Private Subnet
โโ EC2 Instance
โโ RDS Database
All reproducible with a single apply.
๐ฎ Future of Terraform
- Increased policy automation
- OpenTofu (community fork)
- More cloud-neutral infra design
- AI-generated infrastructure configs
๐งญ If Youโre Running Proxmox
Terraform has:
- Proxmox provider
- VMware provider
- Kubernetes provider
So yes โ you can manage:
- VMs
- Containers
- Networks
๐ Summary
Terraform is best when:
- You want reproducible infrastructure
- You manage multiple environments
- You need version-controlled infra
- You want multi-cloud flexibility
It is less suited for:
- App runtime orchestration
- Fine config management
- One-off manual setups
If you want, I can next give:
- ๐ฅ Real production folder structure example
- ๐ง Terraform for Proxmox homelab guide
- โ Terraform for AWS startup SaaS
- ๐ Terraform security deep dive
- ๐ Terraform vs Pulumi vs OpenTofu
Just tell me what direction you want.