📋 REFERENCE GUIDE
Terraform Cheat Sheet
Every command, block type, and function — all in one place.
🚀 CLI Commands
terraform init
Initialize working directory, download providersterraform plan
Preview changes before applyingterraform apply
Apply changes to infrastructureterraform destroy
Destroy all managed resourcesterraform validate
Check configuration syntaxterraform fmt
Format HCL files to canonical styleterraform show
Show current state or planterraform output
Display output valuesterraform state list
List resources in stateterraform state show <resource>
Show details of a resource in stateterraform import <resource> <id>
Import existing resource into stateterraform taint <resource>
Mark resource for recreationterraform untaint <resource>
Remove taint from resourceterraform workspace list
List workspacesterraform workspace new <name>
Create new workspaceterraform workspace select <name>
Switch workspaceterraform graph
Generate dependency graph (DOT format)terraform providers
Show required providersterraform force-unlock <id>
Release a stuck state lockterraform refresh
Update state to match real resources📦 Block Types
resource
Define infrastructure object to managedata
Read external data sourcevariable
Declare input variableoutput
Declare output valuelocals
Define local values / expressionsmodule
Call a reusable moduleprovider
Configure a providerterraform
Backend, required_providers, experimentsmoved
Refactor without destroying resourcesimport
Import block (Terraform 1.5+)check
Continuous validation assertions🔧 Meta-Arguments
count
Create multiple instances by countfor_each
Create instances from map/setdepends_on
Explicit dependency orderingprovider
Select non-default providerlifecycle
create_before_destroy, prevent_destroy, ignore_changes⚡ Built-in Functions
length(list)
Return length of list/map/stringjoin(sep, list)
Join list elements with separatorsplit(sep, string)
Split string into listlookup(map, key, default)
Lookup value in mapmerge(map1, map2)
Merge maps (last wins)concat(list1, list2)
Concatenate listsflatten(list)
Flatten nested liststoset(list)
Convert list to settomap(object)
Convert object to maptry(expr, fallback)
Try expression, return fallback on errorcoalesce(vals...)
Return first non-null/empty valueformat(spec, vals...)
Format string (printf-style)replace(str, search, replace)
String replacementregex(pattern, string)
Regex matchcidrsubnet(prefix, bits, num)
Calculate subnet CIDRfile(path)
Read file contents as stringtemplatefile(path, vars)
Render template filejsonencode(value)
Encode value as JSONyamldecode(string)
Decode YAML stringbase64encode(string)
Base64 encodetimestamp()
Current UTC timestamp (RFC 3339)🔄 Expressions
condition ? true : false
Conditional expressionfor v in list : v.attr
For expression (list){for k, v in map : k => v}
For expression (map)dynamic "name" { ... }
Dynamic block generationsplat: resource.*.attr
Splat expressioneach.key / each.value
for_each iteration valuescount.index
Current count indexself
Self-reference in provisionerspath.module
Current module directorypath.root
Root module directoryterraform.workspace
Current workspace name☁️ Common Provider Resources
aws_instance
EC2 instanceaws_s3_bucket
S3 bucketaws_vpc / aws_subnet
VPC networkingaws_security_group
Security group + rulesaws_iam_role / aws_iam_policy
IAM resourcesaws_lambda_function
Lambda functionaws_rds_cluster
RDS Aurora clusterazurerm_resource_group
Azure resource groupazurerm_virtual_network
Azure VNetgoogle_compute_instance
GCP VM instancegoogle_storage_bucket
GCP storage bucketkubernetes_deployment
K8s deployment🗄️ State & Backend
local backend
Default — state stored as local files3 backend
AWS S3 + DynamoDB for lockingazurerm backend
Azure Blob Storagegcs backend
Google Cloud Storageremote backend
Terraform Cloud / Enterpriseterraform state mv
Move resource in stateterraform state rm
Remove resource from stateterraform state pull
Download remote stateterraform state push
Upload local state to remoteWant to learn all of this hands-on?
Interactive courses with copy-paste examples