Terraform Cheat Sheet - Quick Reference

 

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:

  1. Providers – Plugins that allow Terraform to interact with cloud services. Examples: awsazuregoogle.

  2. Resources – The infrastructure components you want to create. Examples: aws_instanceaws_s3_bucket.

  3. State Files – Terraform keeps a record of all managed resources in terraform.tfstate.

  4. Modules – Reusable sets of Terraform code that simplify complex infrastructure management.


2. Terraform Workflow

Terraform has a simple but powerful workflow:

  1. Write Configuration – Create .tf files with resources, variables, and outputs.

  2. Initialize Directory

terraform init
  • Downloads providers and initializes your working directory.

  1. Plan Changes

terraform plan
  • Preview the changes Terraform will make before applying them.

  1. Apply Changes

terraform apply
  • Deploys the infrastructure as per your configuration.

  1. Destroy Resources

terraform destroy
  • 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

provider "aws" { region = var.region } resource "aws_instance" "web_server" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" tags = { Name = "MyWebServer" } } output "instance_id" { value = aws_instance.web_server.id }

4. Variables and Outputs

Variables make your Terraform code dynamic and reusable.

Defining Variables

variable "region" { description = "AWS region to deploy resources" default = "us-east-1" }

Using Variables

provider "aws" { region = var.region }

Defining Outputs

Outputs let you expose useful values after deployment.

output "instance_ip" { value = aws_instance.web_server.public_ip }

5. Workspaces

Workspaces allow you to manage multiple environments using the same code:

  • List Workspaces:

terraform workspace list
  • Create a Workspace:

terraform workspace new dev
  • Switch Workspaces:

terraform workspace select dev

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

module "s3_bucket" { source = "terraform-aws-modules/s3-bucket/aws" bucket = "my-terraform-bucket" acl = "private" }

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

terraform state list # List all resources terraform state show <res> # Show resource details terraform state mv <old> <new> # Rename/move resources

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.

data "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] } }

9. Lifecycle and Dependencies

  • Ignore Changes: Useful if some attributes are managed outside Terraform.

lifecycle { ignore_changes = [tags] }
  • Explicit Dependencies: Sometimes you need to control the order of creation.

depends_on = [aws_vpc.main]

10. Common Commands Cheat Sheet

CommandPurpose
terraform initInitialize a working directory
terraform planPreview changes
terraform applyApply changes
terraform destroyDelete resources
terraform validateValidate configuration
terraform fmtFormat code
terraform taint <res>Mark a resource for recreation
terraform import <res> <id>Import an existing resource
terraform graphShow resource dependency graph

11. Best Practices

  1. Version Control: Keep all .tf files in Git.

  2. Secure State Files: Especially if stored remotely in S3 or Terraform Cloud.

  3. Use Workspaces or Directories: Separate environments clearly.

  4. Write Reusable Modules: Avoid duplicating code.

  5. Plan Before Apply: Always double-check what Terraform will do.

  6. Experiment Fearlessly: Break and rebuild resources in a test environment to learn faster.



Comments