Terraform is a popular Infrastructure-as-Code (IaC) tool that lets you provision and manage infrastructure efficiently.
This cheatsheet covers the most common and essential Terraform CLI commands, grouped by category for quick reference.
🚀 Getting Started
terraform -help # Show all commands
terraform version # Show current Terraform version
terraform -install-autocomplete # Enable shell autocomplete
📦 Initialization
Initialize a working directory containing Terraform configuration files.
terraform init # Initialize Terraform in the current directory
terraform init -upgrade # Re-download modules/providers with the latest versions
✅ Validate & Format
terraform validate # Check if configuration is valid
terraform fmt # Format .tf files to canonical style
terraform fmt -recursive # Format files in all subdirectories
terraform fmt -check # Checks if all files are formatted
📋 Plan & Apply
terraform plan # Show what changes will be applied
terraform plan -out=tfplan # Save plan to a file
terraform apply # Apply changes
terraform apply tfplan # Apply saved plan
terraform apply -auto-approve # Apply without asking for confirmation
🔥 Destroy
terraform destroy # Destroy infrastructure
terraform destroy -auto-approve # Destroy without confirmation
📂 State Management
Inspect and modify Terraform’s state file.
terraform state list # List resources in state
terraform state show <address> # Show resource details
terraform state rm <address> # Remove a resource from state
terraform state mv <src> <dest> # Move resource in state
🌎 Workspaces
Workspaces let you manage multiple environments (e.g., dev, staging, prod).
terraform workspace list # List all workspaces
terraform workspace show # Show current workspace
terraform workspace new dev # Create a new workspace
terraform workspace select dev # Switch to a workspace
🔌 Providers & Modules
terraform providers # Show required providers
terraform providers mirror ./mirror-dir # Download provider plugins
terraform get # Download and update modules
terraform get -update # Re-fetch modules
🛠 Debugging & Misc
terraform graph # Generate dependency graph (DOT format)
terraform refresh # Update state with real infrastructure
terraform output # Show outputs from configuration
terraform output # Show specific output
TF_LOG=DEBUG terraform plan # Enable debug logging
📑 Quick Workflow Example
terraform init
terraform validate
terraform plan -out=tfplan
terraform apply tfplan
terraform output
terraform destroy
🚀 Conclusion
This cheatsheet provides a quick reference for everyday Terraform usage.
From init to destroy, mastering these commands will speed up your infrastructure automation workflow.
Bookmark this page and use it as your go-to Terraform CLI reference!



You must be logged in to post a comment.