Andrew Mercer
on this page

Current as of Terraform 1.16. Every command works the same with tofu for OpenTofu unless noted.

Global options

terraform -chdir=live/prod/network plan     # run as if in that directory
terraform -help / terraform plan -help
terraform version [-json]

Initialise

terraform init                              # providers, modules, backend
terraform init -upgrade                     # newest versions allowed by constraints
terraform init -backend=false               # skip backend (validate/test in CI)
terraform init -reconfigure                 # ignore existing backend config
terraform init -migrate-state [-force-copy] # move state to a new backend
terraform init -backend-config=env/prod.backend.hcl
terraform init -lockfile=readonly           # fail rather than modify the lock file
terraform get -update                       # refresh modules only

Format and validate

terraform fmt [-recursive] [-check] [-diff]
terraform validate [-json]                  # 1.15+: also validates the backend block

Plan

terraform plan
terraform plan -out=tfplan                  # save for apply
terraform plan -var-file=env/prod.tfvars -var 'env=prod'
terraform plan -detailed-exitcode           # 0 = no changes, 1 = error, 2 = changes
terraform plan -refresh-only                # drift only
terraform plan -refresh=false               # skip refresh
terraform plan -destroy                     # what destroy would do
terraform plan -replace='ADDR'              # force replacement
terraform plan -target='ADDR'               # partial plan (emergencies)
terraform plan -generate-config-out=gen.tf  # draft config for import blocks
terraform plan -lock-timeout=5m -parallelism=20
terraform plan -compact-warnings -no-color -input=false

Apply and destroy

terraform apply tfplan                      # apply a saved plan (no prompt)
terraform apply                             # plan + prompt
terraform apply -auto-approve               # plan + no prompt (avoid outside sandboxes)
terraform apply -refresh-only               # accept drift into state
terraform apply -replace='ADDR'
terraform apply -invoke='action.TYPE.NAME'  # 1.14+: run an action
terraform destroy                           # = apply -destroy

Inspect

terraform show                              # current state, human-readable
terraform show tfplan                       # a saved plan
terraform show -json tfplan > tfplan.json   # for jq, Conftest, Checkov
terraform output [-json] [-raw NAME]
terraform console [-scope=module.x]         # evaluate expressions (-scope: 1.16+)
terraform graph [-type=plan] | dot -Tsvg > graph.svg
terraform graph -format=mermaid             # 1.16+
terraform providers                         # provider requirements tree
terraform providers schema -json            # every resource schema (great for tooling)
terraform metadata functions -json          # function signatures

State

terraform state list [ADDR_PREFIX]
terraform state show ADDR [-json]           # -json: 1.16+
terraform state mv FROM TO                  # prefer moved blocks
terraform state rm ADDR                     # prefer removed blocks
terraform state pull > backup.tfstate
terraform state push backup.tfstate
terraform state replace-provider FROM TO
terraform force-unlock LOCK_ID

Import and query

terraform import ADDR ID                    # legacy; prefer import blocks
terraform query                             # 1.14+: run list blocks in *.tfquery.hcl
terraform query -generate-config-out=imported.tf

Workspaces

terraform workspace list [-json]            # -json: 1.16+
terraform workspace show
terraform workspace new NAME
terraform workspace select [-or-create] NAME
terraform workspace delete NAME

Providers and lock file

terraform providers lock -platform=linux_amd64 -platform=darwin_arm64
terraform providers mirror -platform=linux_amd64 /srv/mirror

Test

terraform test
terraform test -filter=tests/unit.tftest.hcl
terraform test -verbose -junit-xml=report.xml
terraform test -var 'x=y' -var-file=test.tfvars

HCP Terraform

terraform login [hostname]
terraform logout
terraform stacks ...                        # 1.13+, HCP Stacks

Exit codes

Command 0 1 2
plan -detailed-exitcode No changes Error Changes present
Everything else Success Error

Environment variables

Variable Effect
TF_LOG=TRACE\|DEBUG\|INFO\|WARN\|ERROR Logging (core + providers); leaks secrets
TF_LOG_CORE, TF_LOG_PROVIDER Log levels for core / providers separately
TF_LOG_PATH Write logs to a file
TF_IN_AUTOMATION CI-friendly output
TF_INPUT=false Never prompt
TF_CLI_ARGS, TF_CLI_ARGS_<cmd> Extra arguments
TF_VAR_<name> Variable values
TF_DATA_DIR Location of .terraform/
TF_PLUGIN_CACHE_DIR Provider cache
TF_WORKSPACE Workspace
TF_TOKEN_<host> Registry/HCP tokens (dots → underscores)
TF_CLI_CONFIG_FILE Alternative ~/.terraformrc

OpenTofu-specific

tofu plan -exclude='ADDR'                   # inverse of -target (OpenTofu 1.9+)
tofu plan -json-into=plan.json              # 1.12+: machine JSON to a file, human output to the terminal

Other differences are on the OpenTofu page.

Terragrunt equivalents

terragrunt plan (= terragrunt run -- plan), terragrunt run --all -- plan, and the rest are in the Terragrunt cheat sheet.