Master Terraform Fast - Guide + Cheat Sheet
by Dan.C
Terraform From zero to Hero + Complete Commands Cheat Sheet
If you’ve ever manually clicked through a cloud console to create servers, databases, or networks, you know it can get messy fast. What if you need to replicate the same environment tomorrow—or share it with your team? That’s where Terraform comes in.
Table of Contents
- Terraform From Zero to Hero + Complete Commands Cheat Sheet
- What is Terraform (and why should you care)?
- How Terraform Works (The 4-Step Workflow)
- Getting Started with Terraform
- Going Deeper: Key Terraform Concepts
- Terraform Commands Cheat Sheet
- Final Thoughts
- You may also like
- Conclusion
This post is your from zero to hero guide:
- I’ll explain what Terraform is, why it’s important, and how it works.
- Walk step by step through writing and applying your first configuration.
- Cover intermediate concepts like variables, outputs, state, and modules.
- Wrap up with a complete cheat sheet you can use every day.
🌍 What is Terraform (and why should you care)?
Terraform is an Infrastructure as Code (IaC) tool. Instead of manually creating resources in AWS, Azure, GCP, or other providers, you describe your infrastructure in declarative configuration files.
- Declarative means: you describe what you want, not the step-by-step instructions to get there.
- Terraform then compares your desired state with the current state and figures out the actions needed (create, update, delete).
Example Analogy
Imagine you’re telling a builder:
- “I want a house with 3 rooms, 2 bathrooms, and a garden.”
- You don’t explain how to lay bricks or install pipes.
- Terraform is that builder—it knows the steps and makes sure reality matches your blueprint.
🏗️ How Terraform Works (The 4-Step Workflow)
Terraform’s workflow always revolves around four core commands:
- Write – You write configuration files (
.tf
) describing resources. - Init – Terraform downloads the right provider plugins.
- Plan – Terraform shows you what will change.
- Apply – Terraform makes those changes.
At the end, you can destroy everything with a single command.
🚀 Getting Started with Terraform
Step 1. Install Terraform
Download Terraform from HashiCorp’s official site.
Check installation:
terraform -v
Step 2. Set Up Your First Project
Create a new folder and a file called main.tf
.
Inside main.tf
, write:
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "demo" {
bucket = "my-terraform-demo-bucket-12345"
acl = "private"
}
What this does:
- Provider block: tells Terraform which cloud provider you’re using (here AWS).
- Resource block: describes the thing you want (an S3 bucket).
- Each resource has a type (
aws_s3_bucket
) and a name (demo
).
Step 3. Initialize Terraform
terraform init
This downloads the provider plugin (AWS in this case) into a hidden folder .terraform/
.
Without init
, Terraform doesn’t know how to talk to AWS.
Step 4. Validate Configuration
terraform validate
Checks for typos and syntax errors. Think of it as a compiler for your infra.
Step 5. Preview the Execution Plan
terraform plan
Shows what Terraform would do:
- Add, change, or delete resources.
- No changes happen yet—this is a dry run.
Step 6. Apply the Plan
terraform apply
Terraform now creates your resources. You’ll be asked to confirm with yes.
Behind the scenes, Terraform also updates its state file (terraform.tfstate
). This file keeps track of all resources Terraform manages.
Step 7. Destroy Infrastructure
When you’re done testing:
terraform destroy
Terraform removes everything it created. This is super useful for keeping cloud costs down!
🧠 Going Deeper: Key Terraform Concepts
1. Variables
Instead of hardcoding values, make them dynamic:
variable "region" {
default = "us-east-1"
}
provider "aws" {
region = var.region
}
Run with custom variables:
terraform apply -var="region=eu-west-1"
2. Outputs
Show useful information after deployment:
output "bucket_name" {
value = aws_s3_bucket.demo.bucket
}
View outputs:
terraform output
3. State Management
Terraform keeps track of what it manages in terraform.tfstate
.
- For teams, store it remotely (e.g., AWS S3 + DynamoDB for locking).
- Never edit it by hand unless you really know what you’re doing.
4. Modules
Think of modules like functions in programming. They package resources into reusable blocks.
Example:
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
}
This lets you structure large projects cleanly.
5. Workspaces
Workspaces let you separate environments (dev, staging, prod) while reusing the same configs.
terraform workspace new dev
terraform workspace select dev
terraform apply
🛠️ Terraform Commands Cheat Sheet
Here’s your quick reference. Bookmark this!
Initialization & Setup
terraform init # Initialize directory (downloads providers)
terraform validate # Validate configuration files
terraform fmt # Auto-format .tf files
terraform providers # Show required providers
Core Workflow
terraform plan # Preview changes (dry run)
terraform apply # Apply changes
terraform destroy # Destroy all managed infrastructure
State Management
terraform state list # List resources in state
terraform state show <resource> # Show details of a resource
terraform state rm <resource> # Remove a resource from state
terraform refresh # Refresh local state from provider
Variables & Outputs
terraform output # Show outputs
terraform apply -var="key=value" # Pass variable at runtime
Workspaces
terraform workspace list # List workspaces
terraform workspace new staging # Create new workspace
terraform workspace select staging # Switch to workspace
Debugging & Logs
TF_LOG=DEBUG terraform plan # Debug logs
TF_LOG=TRACE terraform apply # Very detailed logs
🎯 Final Thoughts
Terraform is a powerful tool for managing infrastructure consistently and safely. The learning curve may feel steep at first, but once you understand the workflow (init → plan → apply → destroy) and how state files work, it becomes second nature.
Start small (like creating a single bucket or VM), then explore variables, modules, and remote state as you grow more confident. And whenever you forget a command—come back to the cheat sheet above.
You may also like:
Keep learning, stay inovative. — Dan.C
tags: terraform - infrastructure-as-code - iac - devops - cloud - aws - automation - infrastructure - cloud-computing - cheat-sheet - tutorial - beginners-guide