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
Show Terraform version
terraform version

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

Show Terraform help
terraform -help

# List Terraform subcommands and global options.

Initialize working directory
terraform init

# Install required providers and modules and initialize backend configuration.

Init and upgrade providers/modules
terraform init -upgrade

# Upgrade providers and modules to newest allowed versions during initialization.

Init without backend
terraform init -backend=false

# Initialize modules and providers without configuring remote/backend state.

Reconfigure backend
terraform init -reconfigure

# Reinitialize backend configuration without migrating existing state automatically.

## Formatting and Validation
Format current directory
terraform fmt

# Rewrite Terraform files to canonical style.

Format recursively
terraform fmt -recursive

# Format Terraform files in current directory and subdirectories.

Validate configuration
terraform validate

# Check whether configuration is syntactically valid and internally consistent.

Validate and return JSON
terraform validate -json

# Emit machine-readable validation output, useful in CI.

Open Terraform console
terraform console

# Interactive REPL for testing expressions and inspecting values from state.

Run Terraform tests
terraform test

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

## Plan and Apply
Create execution plan
terraform plan

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

Save plan to file
terraform plan -out=tfplan

# Write the generated plan to a file for later apply.

Plan with variable
terraform plan -var='environment=prod'

# Set an input variable directly on the command line.

Plan with var file
terraform plan -var-file=prod.tfvars

# Use a tfvars file during planning.

Target one resource
terraform plan -target=aws_instance.web

# Plan changes for a specific resource address.

Refresh-only plan
terraform plan -refresh-only

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

Plan destroy
terraform plan -destroy

# Plan destruction of all managed infrastructure.

Apply changes
terraform apply

# Create or update infrastructure after approval.

Apply without prompt
terraform apply -auto-approve

# Skip interactive approval prompt.

Apply saved plan
terraform apply tfplan

# Apply exactly the actions saved in a plan file.

Destroy infrastructure
terraform destroy

# Destroy all resources managed by the current configuration.

Destroy without prompt
terraform destroy -auto-approve

# Destroy infrastructure without approval prompt.

## Inspect State and Outputs
Show outputs
terraform output

# Display all output values from state.

Show one output
terraform output vpc_id

# Display a single output value.

Show raw output
terraform output -raw vpc_id

# Print a string output without JSON quoting or extra formatting.

Show outputs as JSON
terraform output -json

# Emit outputs as JSON for scripting.

Show human-readable state or plan
terraform show

# Show human-readable representation of current state.

Show saved plan
terraform show tfplan

# Inspect a previously saved plan file.

Show state or plan as JSON
terraform show -json tfplan

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

List resources in state
terraform state list

# Print all resource addresses currently tracked in state.

Show one resource from state
terraform state show aws_instance.web

# Display detailed attributes for one resource instance.

List providers used
terraform providers

# Show provider requirements and dependency tree.

Generate dependency graph
terraform graph | dot -Tpng > graph.png

# Generate a Graphviz dependency graph for visualization.

## Variables and Environment
Disable interactive input
terraform plan -input=false

# Fail instead of prompting for missing variables.

Set TF_VAR variable
export TF_VAR_region=us-east-1

# Pass an input variable through environment variables.

Default extra CLI args
export TF_CLI_ARGS_plan='-lock-timeout=60s'

# Inject default arguments for a specific Terraform subcommand.

Enable Terraform logging
export TF_LOG=INFO

# Turn on Terraform internal logging for troubleshooting.

Write Terraform log to file
export TF_LOG_PATH=./terraform.log

# Persist Terraform debug logs to a file.

Change .terraform directory
export TF_DATA_DIR=.tfdata

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

## Locking and Recovery
Force unlock state
terraform force-unlock LOCK_ID

# Release a stuck state lock by lock ID.

Apply with lock timeout
terraform apply -lock-timeout=120s

# Wait for an existing state lock before failing.

Refresh-only apply legacy
terraform apply -refresh-only

# Preferred modern replacement for older refresh-only workflows.