Terraform CLI Cheat Sheet

Core Terraform CLI commands for init, plan, apply, state, output, variables, workspaces, and automation.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Getting Started

Initialize a working directory and inspect version/help.

Show Terraform version

Print the installed Terraform CLI version and provider versions in use.

bashANYterraformversion
bash
terraform version

Print the installed Terraform CLI version and provider versions in use.

Show Terraform help

List Terraform subcommands and global options.

bashANYterraformhelp
bash
terraform -help

List Terraform subcommands and global options.

Initialize working directory

Install required providers and modules and initialize backend configuration.

bashANYterraforminitprovidersmodules
bash
terraform init

Install required providers and modules and initialize backend configuration.

Init and upgrade providers/modules

Upgrade providers and modules to newest allowed versions during initialization.

bashANYterraforminitupgrade
bash
terraform init -upgrade

Upgrade providers and modules to newest allowed versions during initialization.

Init without backend

Initialize modules and providers without configuring remote/backend state.

bashANYterraforminitbackend
bash
terraform init -backend=false

Initialize modules and providers without configuring remote/backend state.

Reconfigure backend

Reinitialize backend configuration without migrating existing state automatically.

bashANYterraforminitbackendreconfigure
bash
terraform init -reconfigure

Reinitialize backend configuration without migrating existing state automatically.

Formatting and Validation

Format and validate configuration before planning.

Format current directory

Rewrite Terraform files to canonical style.

bashANYterraformfmtformat
bash
terraform fmt

Rewrite Terraform files to canonical style.

Format recursively

Format Terraform files in current directory and subdirectories.

bashANYterraformfmtrecursive
bash
terraform fmt -recursive

Format Terraform files in current directory and subdirectories.

Validate configuration

Check whether configuration is syntactically valid and internally consistent.

bashANYterraformvalidate
bash
terraform validate

Check whether configuration is syntactically valid and internally consistent.

Validate and return JSON

Emit machine-readable validation output, useful in CI.

bashANYterraformvalidatejsonci
bash
terraform validate -json

Emit machine-readable validation output, useful in CI.

Open Terraform console

Interactive REPL for testing expressions and inspecting values from state.

bashANYterraformconsoleexpressions
bash
terraform console

Interactive REPL for testing expressions and inspecting values from state.

Run Terraform tests

Execute Terraform test files in the tests directory or specified test directory.

bashANYterraformtesttesting
bash
terraform test

Execute Terraform test files in the tests directory or specified test directory.

Plan and Apply

Core lifecycle workflow commands.

Create execution plan

Show the actions Terraform would take to reach the desired state.

bashANYterraformplan
bash
terraform plan

Show the actions Terraform would take to reach the desired state.

Save plan to file

Write the generated plan to a file for later apply.

bashANYterraformplanout
bash
terraform plan -out=tfplan

Write the generated plan to a file for later apply.

Plan with variable

Set an input variable directly on the command line.

bashANYterraformplanvarvariables
bash
terraform plan -var='environment=prod'

Set an input variable directly on the command line.

Plan with var file

Use a tfvars file during planning.

bashANYterraformplanvar-filetfvars
bash
terraform plan -var-file=prod.tfvars

Use a tfvars file during planning.

Target one resource

Plan changes for a specific resource address.

bashANYterraformplantarget
bash
terraform plan -target=aws_instance.web

Plan changes for a specific resource address.

Refresh-only plan

Compare state with remote objects and update state without proposing config changes.

bashANYterraformplanrefresh-only
bash
terraform plan -refresh-only

Compare state with remote objects and update state without proposing config changes.

Plan destroy

Plan destruction of all managed infrastructure.

bashANYterraformplandestroy
bash
terraform plan -destroy

Plan destruction of all managed infrastructure.

Apply changes

Create or update infrastructure after approval.

bashANYterraformapply
bash
terraform apply

Create or update infrastructure after approval.

Apply without prompt

Skip interactive approval prompt.

bashANYterraformapplyauto-approve
bash
terraform apply -auto-approve

Skip interactive approval prompt.

Apply saved plan

Apply exactly the actions saved in a plan file.

bashANYterraformapplyplanfile
bash
terraform apply tfplan

Apply exactly the actions saved in a plan file.

Destroy infrastructure

Destroy all resources managed by the current configuration.

bashANYterraformdestroy
bash
terraform destroy

Destroy all resources managed by the current configuration.

Destroy without prompt

Destroy infrastructure without approval prompt.

bashANYterraformdestroyauto-approve
bash
terraform destroy -auto-approve

Destroy infrastructure without approval prompt.

Inspect State and Outputs

Read outputs, state, graphs, providers, and resource dependencies.

Show outputs

Display all output values from state.

bashANYterraformoutput
bash
terraform output

Display all output values from state.

Show one output

Display a single output value.

bashANYterraformoutput
bash
terraform output vpc_id

Display a single output value.

Show raw output

Print a string output without JSON quoting or extra formatting.

bashANYterraformoutputraw
bash
terraform output -raw vpc_id

Print a string output without JSON quoting or extra formatting.

Show outputs as JSON

Emit outputs as JSON for scripting.

bashANYterraformoutputjson
bash
terraform output -json

Emit outputs as JSON for scripting.

Show human-readable state or plan

Show human-readable representation of current state.

bashANYterraformshowstate
bash
terraform show

Show human-readable representation of current state.

Show saved plan

Inspect a previously saved plan file.

bashANYterraformshowplanfile
bash
terraform show tfplan

Inspect a previously saved plan file.

Show state or plan as JSON

Emit a plan or state file as machine-readable JSON.

bashANYterraformshowjson
bash
terraform show -json tfplan

Emit a plan or state file as machine-readable JSON.

List resources in state

Print all resource addresses currently tracked in state.

bashANYterraformstatelist
bash
terraform state list

Print all resource addresses currently tracked in state.

Show one resource from state

Display detailed attributes for one resource instance.

bashANYterraformstateshow
bash
terraform state show aws_instance.web

Display detailed attributes for one resource instance.

List providers used

Show provider requirements and dependency tree.

bashANYterraformproviders
bash
terraform providers

Show provider requirements and dependency tree.

Generate dependency graph

Generate a Graphviz dependency graph for visualization.

bashANYterraformgraphdependencies
bash
terraform graph | dot -Tpng > graph.png

Generate a Graphviz dependency graph for visualization.

Variables and Environment

Pass variables and control Terraform with environment variables.

Disable interactive input

Fail instead of prompting for missing variables.

bashANYterraformplaninputci
bash
terraform plan -input=false

Fail instead of prompting for missing variables.

Set TF_VAR variable

Pass an input variable through environment variables.

bashANYterraformenvironmentvariablestf_var
bash
export TF_VAR_region=us-east-1

Pass an input variable through environment variables.

Default extra CLI args

Inject default arguments for a specific Terraform subcommand.

bashANYterraformenvironmentcli-args
bash
export TF_CLI_ARGS_plan='-lock-timeout=60s'

Inject default arguments for a specific Terraform subcommand.

Enable Terraform logging

Turn on Terraform internal logging for troubleshooting.

bashANYterraformenvironmentlogging
bash
export TF_LOG=INFO

Turn on Terraform internal logging for troubleshooting.

Write Terraform log to file

Persist Terraform debug logs to a file.

bashANYterraformenvironmentlogging
bash
export TF_LOG_PATH=./terraform.log

Persist Terraform debug logs to a file.

Change .terraform directory

Store provider/plugins and local data in a custom directory.

bashANYterraformenvironmentdata-dir
bash
export TF_DATA_DIR=.tfdata

Store provider/plugins and local data in a custom directory.

Locking and Recovery

Handle state locks and interrupted runs.

Force unlock state

Release a stuck state lock by lock ID.

bashANYterraformforce-unlocklocking
bash
terraform force-unlock LOCK_ID

Release a stuck state lock by lock ID.

Apply with lock timeout

Wait for an existing state lock before failing.

bashANYterraformapplylock-timeout
bash
terraform apply -lock-timeout=120s

Wait for an existing state lock before failing.

Refresh-only apply legacy

Preferred modern replacement for older refresh-only workflows.

bashANYterraformrefreshlegacy
bash
terraform apply -refresh-only

Preferred modern replacement for older refresh-only workflows.