Terraform Troubleshooting and Migration Cheat Sheet

Commands and patterns for debugging, upgrades, migration, and state repair in Terraform.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Debugging
Enable TRACE logs
export TF_LOG=TRACE

# Turn on maximum logging detail for hard-to-diagnose issues.

Write debug log to file
export TF_LOG=DEBUG && export TF_LOG_PATH=terraform-debug.log

# Capture debug logs into a file for later analysis.

Inspect provider schemas
terraform providers schema -json > providers-schema.json

# Dump provider schemas for automation or debugging tools.

Use detailed exit codes
terraform plan -detailed-exitcode

# Return 0 for no changes, 2 for changes, 1 for errors; useful in CI.

## Upgrade and Migration
Legacy 0.13 upgrade helper
terraform 0.13upgrade

# Legacy helper command that updated provider source declarations in Terraform 0.13.

Upgrade after version changes
terraform init -upgrade && terraform providers lock -platform=linux_amd64 -platform=darwin_arm64

# Refresh provider selections and lock file after changing constraints.

Moved module address snippet
moved {
  from = module.network.aws_subnet.private["a"]
  to   = module.vpc.aws_subnet.private["a"]
}

# Preserve state when refactoring module paths or names.

## State Repair
Inspect module resource
terraform state show module.vpc.aws_vpc.this

# Inspect one resource nested inside a module.

Move module resource
terraform state mv module.old.aws_s3_bucket.logs module.new.aws_s3_bucket.logs

# Move a tracked object between module addresses.

Remove one indexed instance
terraform state rm aws_instance.web[0]

# Remove a single indexed or keyed instance from state.

## Drift and Recovery
Detect and record drift
terraform plan -refresh-only

# Refresh state from real infrastructure without proposing config changes.

Apply refresh-only changes
terraform apply -refresh-only -auto-approve

# Persist refreshed state after infrastructure drift detection.

Import after state loss
terraform import aws_db_instance.main my-db-instance-id

# Rebuild Terraform state associations after state loss or partial migration.