Terraform CLI: Cheat Sheet
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
| Task | Command |
|---|---|
| Version | terraform version |
| Dependency graph | terraform graph |
| Show providers | terraform providers |
| Lock providers | terraform providers lock |
| Force unlock state | terraform force-unlock LOCK_ID |
| Taint (force recreate) | terraform taint aws_instance.web |
| Untaint | terraform 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"
Related Articles
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
TerraformDeep DiveFresh
Terraform from Zero to Production: Project Structure, Modules, State, and CI/CD
Build production-grade Terraform infrastructure — project structure, module design, state management, testing, and CI/CD pipeline integration.
15 min read
TerraformTutorialFresh
Testing Terraform with Terratest: A Practical Guide
How to write unit and integration tests for Terraform modules using Terratest — because untested infrastructure is a liability.
8 min read
TerraformTutorialFresh
Terraform Module Design Patterns for Large Teams
Battle-tested Terraform module patterns for teams — from file structure to versioning to composition. If it's not in code, it doesn't exist.
9 min read