DevOpsil
Terraform
81%
Fresh

Terraform CLI: Cheat Sheet

Zara BlackwoodZara Blackwood3 min read

Init & Providers

terraform init                        # Initialize working directory
terraform init -reconfigure           # Reinitialize (after backend change)
terraform init -upgrade               # Upgrade providers to latest allowed

Plan & Apply

terraform plan                        # Preview changes
terraform plan -out=tfplan            # Save plan to file
terraform apply tfplan                # Apply saved plan (no prompt)
terraform apply -auto-approve         # Apply without prompt (CI/CD)
terraform plan -target=aws_instance.web  # Target specific resource
terraform plan -var="region=us-east-1"   # Pass variable inline
terraform plan -var-file="prod.tfvars"   # Pass variable file
terraform destroy                     # Destroy all resources
terraform destroy -target=aws_instance.web  # Destroy specific resource

State Management

terraform state list                  # List all resources in state
terraform state show aws_instance.web # Show resource details
terraform state mv aws_instance.web aws_instance.app  # Rename in state
terraform state rm aws_instance.web   # Remove from state (no destroy)
terraform state pull > state.json     # Download remote state
terraform state push state.json       # Upload local state

Workspaces

terraform workspace list              # List workspaces
terraform workspace new staging       # Create workspace
terraform workspace select production # Switch workspace
terraform workspace delete staging    # Delete workspace
terraform workspace show              # Show current workspace

Import & Refresh

terraform import aws_instance.web i-0abc123def456
terraform plan -generate-config-out=generated.tf  # Generate config (v1.5+)
terraform refresh                     # Sync state with real infra

Format & Validate

terraform fmt                         # Format .tf files
terraform fmt -recursive              # Format recursively
terraform fmt -check                  # CI check (exits non-zero if unformatted)
terraform validate                    # Validate configuration syntax

Output & Console

terraform output                      # Show all outputs
terraform output -raw db_endpoint     # Raw value (no quotes)
terraform output -json                # JSON format
terraform console                     # Interactive expression REPL

Quick Reference Table

TaskCommand
Versionterraform version
Dependency graphterraform graph
Show providersterraform providers
Lock providersterraform providers lock
Force unlock stateterraform force-unlock LOCK_ID
Taint (force recreate)terraform taint aws_instance.web
Untaintterraform untaint aws_instance.web

Environment Variables

export TF_INPUT=0                     # Skip interactive approval
export TF_LOG=DEBUG                   # Log level: TRACE|DEBUG|INFO|WARN|ERROR
export TF_LOG_PATH="./terraform.log"  # Log to file
export TF_VAR_region="us-west-2"      # Set variable value
export TF_CLI_CONFIG_FILE="$HOME/.terraformrc"
Share:
Zara Blackwood
Zara Blackwood

Platform Engineer

Terraform enthusiast, platform builder, DRY advocate. I believe infrastructure should be versioned, reviewed, and deployed like any other code. GitOps or bust.

Related Articles