Terraform Cheat Sheet
Declaratively provision infrastructure with a plan/apply workflow — state, providers, modules, and the destroy loop.
Core Lifecycle
12terraform initInstall providers and modules
terraform init -upgradeRefresh provider/module versions
terraform validateSyntax and consistency check
terraform fmt -recursiveFormat all files canonically
terraform planPreview the changes
terraform plan -out=tfplanSave the plan
terraform applyApply the plan (interactive)
terraform apply -auto-approveApply without prompting
terraform apply tfplanApply a saved plan
terraform destroyRemove all managed resources
terraform apply -target=RESOURCEApply one target (debug)
terraform plan -refresh=falseSkip state refresh
State
7terraform state listResources tracked in state
terraform state show RESOURCEOne resource's attributes
terraform state mv SRC DSTRename/move in state
terraform state rm RESOURCEDrop from state (no destroy)
terraform state pullPrint raw state to stdout
terraform state push FILEForce an uploaded state
terraform refreshRe-read real resources into state
Outputs & Variables
6terraform outputPrint all outputs
terraform output -jsonOutputs as JSON
terraform apply -var 'image_id=ami-xyz'Override a variable
terraform apply -var-file=vars.tfvarsLoad variables from a file
TF_VAR_name=value terraform planVariables via environment
terraform plan -json | jqMachine-readable plan
Workspaces & Backends
5terraform workspace listEnvironments in the current state
terraform workspace new devCreate an environment
terraform workspace select devSwitch environments
terraform init -backend-config=backend.tfvarsPoint at a remote backend
terraform force-unlock LOCK_IDBreak a stale state lock