DevTools

Terraform CLI Cheatsheet

Essential Terraform commands for infrastructure as code, planning, applying, and managing cloud resources.

41 commands
Windows MacOS Linux
#infrastructure #iac #devops #Popular

Initialize & Setup

Initialize working directory
terraform init
Upgrade provider plugins
terraform init -upgrade
Init with backend config
terraform init -backend-config=cfg.hcl
Show Terraform version
terraform version
List required providers
terraform providers

Plan & Apply

Preview changes
terraform plan
Save plan to file
terraform plan -out=plan.tfplan
Plan with variable
terraform plan -var="key=value"
Apply changes
terraform apply
Apply saved plan
terraform apply plan.tfplan
Apply without confirmation
terraform apply -auto-approve
Apply with variable file
terraform apply -var-file=vars.tfvars

Destroy & Cleanup

Destroy all resources
terraform destroy
Destroy without confirmation
terraform destroy -auto-approve
terraform destroy -target=aws_instance.x # Destroy specific resource

State Management

List resources in state
terraform state list
Show resource details
terraform state show <resource>
Move/rename resource
terraform state mv <src> <dst>
Remove from state
terraform state rm <resource>
Pull remote state
terraform state pull
Push state to remote
terraform state push

Workspace Management

List workspaces
terraform workspace list
Create workspace
terraform workspace new <name>
Switch workspace
terraform workspace select <name>
Delete workspace
terraform workspace delete <name>
Show current workspace
terraform workspace show

Inspect & Debug

Show current state
terraform show
Show all outputs
terraform output
Show specific output
terraform output <name>
Interactive console
terraform console
Validate configuration
terraform validate
Format .tf files
terraform fmt
Format recursively
terraform fmt -recursive
Generate dependency graph
terraform graph | dot -Tpng > graph.png

Import & Taint

Import existing resource
terraform import <resource> <id>
Mark for recreation
terraform taint <resource>
Remove taint mark
terraform untaint <resource>
Sync state with real infra
terraform refresh

Quick Commands

Preview infrastructure changes before applying
terraform plan
Apply changes to create or update infrastructure
terraform apply
List all resources tracked in state
terraform state list