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!