Terraform Cheatsheet

Commands

Use this Terraform reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Core Workflow Commands

CommandWhat it does
terraform initinitialize directory, download providers and modules
terraform validatecheck config syntax and internal consistency
terraform planshow what changes will be made
terraform applyapply the planned changes
terraform destroydestroy all managed infrastructure
terraform init
terraform plan -out=plan.tfplan     # save plan to file
terraform apply plan.tfplan         # apply exact saved plan (no prompt)
terraform apply -auto-approve       # skip confirmation (CI use)
terraform destroy -auto-approve

init Flags

FlagDescription
-upgradeupgrade providers to latest allowed version
-reconfigureignore existing backend config (force re-init)
-migrate-statemigrate state to new backend
-backend=falseskip backend initialization
-backend-config=file.hclload partial backend config from file
-backend-config="key=val"supply individual backend config values

plan Flags

FlagDescription
-out=plan.tfplansave plan to file for apply
-var="key=val"set a single variable
-var-file=file.tfvarsload variables from file
-target=resource.addrplan only this resource (and deps)
-refresh=falseskip reading real infra state
-refresh-onlyplan to sync state with real infra only
-destroyplan a destroy operation
-compact-warningscondense warning output
-jsonmachine-readable JSON output
-generate-config-out=file.tfgenerate HCL for import blocks

apply Flags

FlagDescription
-auto-approveskip interactive confirmation
-var="key=val"set a variable
-var-file=file.tfvarsload variables from file
-target=resource.addrapply only this resource (and deps)
-refresh=falsedon't refresh state before apply
-replace=resource.addrforce replacement of a specific resource
-parallelism=Nconcurrent operations (default: 10)
-jsonJSON output (for CI parsing)
# Force replace a specific resource
terraform apply -replace="aws_instance.web"

# Apply with variables
terraform apply -var="environment=prod" -var-file="prod.tfvars"

# Target a specific resource and its deps
terraform apply -target="module.vpc"

destroy Flags

terraform destroy                         # destroy all (prompts)
terraform destroy -auto-approve           # no prompt
terraform destroy -target=aws_instance.web  # destroy one resource

State Commands

CommandDescription
terraform showhuman-readable current state
terraform show -jsonJSON state dump
terraform state listlist all resource addresses
terraform state show <addr>show attributes of one resource
terraform state mv <src> <dst>rename resource in state
terraform state rm <addr>remove resource from state
terraform state pullprint remote state to stdout
terraform state push <file>push local state to remote
terraform refreshsync state with real infra
terraform force-unlock <id>release a stuck state lock
terraform state list
terraform state show aws_instance.web
terraform state mv aws_instance.old aws_instance.new
terraform state rm aws_s3_bucket.legacy
terraform state pull > backup-$(date +%Y%m%d).tfstate

Import Commands

# CLI import (classic)
terraform import aws_instance.web i-0abc1234def56789

# Generate config for imported resources (1.5+)
terraform plan -generate-config-out=imported.tf

Workspace Commands

terraform workspace list
terraform workspace new <name>
terraform workspace select <name>
terraform workspace show
terraform workspace delete <name>

Format and Validate

terraform fmt                    # format all .tf files in current dir
terraform fmt -recursive         # format recursively
terraform fmt -check             # exit 1 if formatting needed (CI)
terraform fmt -diff              # show diff instead of writing
terraform validate               # syntax + config validation

Providers and Modules

terraform providers              # list providers in current config
terraform providers schema -json # dump full provider schema
terraform providers lock \
  -platform=linux_amd64 \
  -platform=darwin_arm64         # add multi-platform lock hashes
terraform get                    # download modules only
terraform get -update            # re-download modules

Output Commands

terraform output                          # all outputs
terraform output <name>                   # one output
terraform output -json                    # JSON (all outputs)
terraform output -raw <name>              # raw string value

Graph

terraform graph                           # emit dependency graph (DOT format)
terraform graph | dot -Tsvg > graph.svg   # render to SVG
terraform graph -type=plan                # plan dependency graph

Console (REPL)

terraform console                # interactive expression evaluator
> cidrsubnet("10.0.0.0/16", 8, 2)
"10.0.2.0/24"
> length(["a", "b", "c"])
3
> jsondecode("{\"key\":\"val\"}")
{ "key" = "val" }
# Ctrl-C or Ctrl-D to exit

Environment Variables

VariableEffect
TF_VAR_<name>set input variable
TF_LOGlog level: TRACE, DEBUG, INFO, WARN, ERROR
TF_LOG_PATHwrite logs to file
TF_CLI_ARGSdefault args for all commands
TF_CLI_ARGS_plandefault args for plan only
TF_CLI_ARGS_applydefault args for apply only
TF_DATA_DIRoverride .terraform directory
TF_WORKSPACEset workspace at startup
TF_IN_AUTOMATIONsuppress interactive prompts/hints
TF_INPUTset to 0 to disable interactive prompts
TF_PLUGIN_CACHE_DIRshare provider cache across projects
export TF_LOG=DEBUG
export TF_LOG_PATH=/tmp/terraform.log
export TF_IN_AUTOMATION=true
export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"

CI/CD Checklist

terraform init -input=false
terraform validate
terraform fmt -check -recursive
terraform plan -input=false -out=plan.tfplan
terraform apply -input=false plan.tfplan