📋 REFERENCE GUIDE

Terraform Cheat Sheet

Every command, block type, and function — all in one place.

🚀 CLI Commands

terraform init Initialize working directory, download providers
terraform plan Preview changes before applying
terraform apply Apply changes to infrastructure
terraform destroy Destroy all managed resources
terraform validate Check configuration syntax
terraform fmt Format HCL files to canonical style
terraform show Show current state or plan
terraform output Display output values
terraform state list List resources in state
terraform state show <resource> Show details of a resource in state
terraform import <resource> <id> Import existing resource into state
terraform taint <resource> Mark resource for recreation
terraform untaint <resource> Remove taint from resource
terraform workspace list List workspaces
terraform workspace new <name> Create new workspace
terraform workspace select <name> Switch workspace
terraform graph Generate dependency graph (DOT format)
terraform providers Show required providers
terraform force-unlock <id> Release a stuck state lock
terraform refresh Update state to match real resources

📦 Block Types

resource Define infrastructure object to manage
data Read external data source
variable Declare input variable
output Declare output value
locals Define local values / expressions
module Call a reusable module
provider Configure a provider
terraform Backend, required_providers, experiments
moved Refactor without destroying resources
import Import block (Terraform 1.5+)
check Continuous validation assertions

🔧 Meta-Arguments

count Create multiple instances by count
for_each Create instances from map/set
depends_on Explicit dependency ordering
provider Select non-default provider
lifecycle create_before_destroy, prevent_destroy, ignore_changes

⚡ Built-in Functions

length(list) Return length of list/map/string
join(sep, list) Join list elements with separator
split(sep, string) Split string into list
lookup(map, key, default) Lookup value in map
merge(map1, map2) Merge maps (last wins)
concat(list1, list2) Concatenate lists
flatten(list) Flatten nested lists
toset(list) Convert list to set
tomap(object) Convert object to map
try(expr, fallback) Try expression, return fallback on error
coalesce(vals...) Return first non-null/empty value
format(spec, vals...) Format string (printf-style)
replace(str, search, replace) String replacement
regex(pattern, string) Regex match
cidrsubnet(prefix, bits, num) Calculate subnet CIDR
file(path) Read file contents as string
templatefile(path, vars) Render template file
jsonencode(value) Encode value as JSON
yamldecode(string) Decode YAML string
base64encode(string) Base64 encode
timestamp() Current UTC timestamp (RFC 3339)

🔄 Expressions

condition ? true : false Conditional expression
for v in list : v.attr For expression (list)
{for k, v in map : k => v} For expression (map)
dynamic "name" { ... } Dynamic block generation
splat: resource.*.attr Splat expression
each.key / each.value for_each iteration values
count.index Current count index
self Self-reference in provisioners
path.module Current module directory
path.root Root module directory
terraform.workspace Current workspace name

☁️ Common Provider Resources

aws_instance EC2 instance
aws_s3_bucket S3 bucket
aws_vpc / aws_subnet VPC networking
aws_security_group Security group + rules
aws_iam_role / aws_iam_policy IAM resources
aws_lambda_function Lambda function
aws_rds_cluster RDS Aurora cluster
azurerm_resource_group Azure resource group
azurerm_virtual_network Azure VNet
google_compute_instance GCP VM instance
google_storage_bucket GCP storage bucket
kubernetes_deployment K8s deployment

🗄️ State & Backend

local backend Default — state stored as local file
s3 backend AWS S3 + DynamoDB for locking
azurerm backend Azure Blob Storage
gcs backend Google Cloud Storage
remote backend Terraform Cloud / Enterprise
terraform state mv Move resource in state
terraform state rm Remove resource from state
terraform state pull Download remote state
terraform state push Upload local state to remote

Want to learn all of this hands-on?

Interactive courses with copy-paste examples