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
Was this article helpful?
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
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.
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.
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.
Terraform Remote State: S3 Backends, Locking, Workspaces, and State Surgery
Everything you need to know about Terraform remote state — from setting up S3 backends with locking to workspace strategies and emergency state surgery.
Writing Reusable Terraform Modules: A Practical Guide
Learn how to write Terraform modules that are actually reusable — with input validation, sensible defaults, and clean interfaces across environments.
Fix Terraform 'Error Acquiring the State Lock' with DynamoDB
Resolve Terraform DynamoDB state lock errors caused by permission issues, missing tables, or misconfigured backends.
More in Terraform
View all →Terraform "Error: No Valid Credential Sources Found" On AWS — Fixing Authentication And Provider Configuration Issues
If you've spent any time with Terraform and AWS, you've almost certainly run into this gem at some point: It's one of those errors that looks simple but ca...
Terraform "Error: Cycle" In Resource Dependencies — How To Detect And Break Circular References
If you've spent any meaningful time with Terraform, you've probably encountered this at the worst possible moment: Everything was working fine, you added o...
Terraform Dynamic Blocks For Managing Variable Infrastructure Resources
If you've been working with Terraform for a while, you've probably hit that wall where you need to create resources with configurations that vary based on...
Terraform Error: Invalid For_each Argument Must Be A Map Or Set Of Strings - Complete Fix Guide
If you've been working with Terraform's `for_each` argument, you've probably hit this error at least once. Don't worry – it's one of those frustrating but...