Terraform Cheat Sheet: A Complete Refresher for Beginners and Practitioners
Terraform is one of the most popular Infrastructure as Code (IaC) tools in the cloud world today. It allows you to define, provision, and manage cloud infrastructure efficiently and consistently across providers like AWS, Azure, and GCP.
I cleared my Terraform certification in January 2025, and since then, I’ve realized that even experienced users often forget the little commands, workflow steps, or best practices. That’s why I created this comprehensive Terraform cheat sheet — a resource you can use as a refresher, for exam prep, or even for day-to-day cloud work.
Whether you’re just starting or returning after a break, this guide will help you refresh your concepts, commands, and workflow in one place.
1. Terraform Basics: What You Need to Know
Before diving into commands, it’s essential to recall the core concepts of Terraform.
What is Terraform?
Terraform is a tool for managing cloud infrastructure as code. Instead of manually creating resources via the cloud console, you write configuration files that describe your infrastructure. Terraform then automates provisioning, updating, and deletion of those resources.
Key points:
Terraform is declarative: you describe what you want, not how to do it.
It can manage multi-cloud environments with a single tool.
State files keep track of your infrastructure to ensure safe updates.
Terraform Architecture
Understanding Terraform’s architecture is crucial:
Providers – Plugins that allow Terraform to interact with cloud services. Examples:
aws,azure,google.Resources – The infrastructure components you want to create. Examples:
aws_instance,aws_s3_bucket.State Files – Terraform keeps a record of all managed resources in
terraform.tfstate.Modules – Reusable sets of Terraform code that simplify complex infrastructure management.
2. Terraform Workflow
Terraform has a simple but powerful workflow:
Write Configuration – Create
.tffiles with resources, variables, and outputs.Initialize Directory
Downloads providers and initializes your working directory.
Plan Changes
Preview the changes Terraform will make before applying them.
Apply Changes
Deploys the infrastructure as per your configuration.
Destroy Resources
Deletes all resources managed by your Terraform configuration.
Pro Tip: Always run terraform plan before apply. It’s like a safety net — you see what’s going to change without risking unintended modifications.
3. Configuration Files
Terraform uses files with .tf extensions for configuration.
Main configuration file (
main.tf) – Contains resource definitions.Variables (
variables.tf) – Store dynamic input values.Outputs (
outputs.tf) – Define values that Terraform should return after applying resources.State (
terraform.tfstate) – Automatically managed by Terraform, tracks resources.
Example: EC2 Instance
4. Variables and Outputs
Variables make your Terraform code dynamic and reusable.
Defining Variables
Using Variables
Defining Outputs
Outputs let you expose useful values after deployment.
5. Workspaces
Workspaces allow you to manage multiple environments using the same code:
List Workspaces:
Create a Workspace:
Switch Workspaces:
Using workspaces is ideal for dev, test, and production environments.
6. Modules and Reusability
Modules are reusable blocks of Terraform code. They simplify complex infrastructure management.
Example: Using a Module
Best Practices with Modules:
Break your code into small, reusable modules.
Use inputs and outputs to pass values in and out.
Leverage Terraform Registry modules for commonly used resources.
7. State Management
Terraform’s state file is essential for tracking resources.
Local vs Remote State: Local stores on your machine; remote can be in S3, Terraform Cloud, or GCS.
State Locking: Prevents multiple users from modifying infrastructure simultaneously.
Useful Commands
Tip: Never manually edit state files unless you really know what you’re doing. Mistakes can break your infrastructure.
8. Data Sources
Data sources allow you to pull existing information from providers.
9. Lifecycle and Dependencies
Ignore Changes: Useful if some attributes are managed outside Terraform.
Explicit Dependencies: Sometimes you need to control the order of creation.
10. Common Commands Cheat Sheet
| Command | Purpose |
|---|---|
terraform init | Initialize a working directory |
terraform plan | Preview changes |
terraform apply | Apply changes |
terraform destroy | Delete resources |
terraform validate | Validate configuration |
terraform fmt | Format code |
terraform taint <res> | Mark a resource for recreation |
terraform import <res> <id> | Import an existing resource |
terraform graph | Show resource dependency graph |
11. Best Practices
Version Control: Keep all
.tffiles in Git.Secure State Files: Especially if stored remotely in S3 or Terraform Cloud.
Use Workspaces or Directories: Separate environments clearly.
Write Reusable Modules: Avoid duplicating code.
Plan Before Apply: Always double-check what Terraform will do.
Experiment Fearlessly: Break and rebuild resources in a test environment to learn faster.
Comments
Post a Comment