Skip to content
ohsudo, Home
Infrastructure as Code

Terraform Cheat Sheet

The Terraform commands you use every day: init, plan and apply, state, import and moved blocks, workspaces, outputs, the lock file, unlocking and debugging.

Updated November 15, 2026 · Applies to Terraform 1.5+ · OpenTofu 1.6+

The everyday loop

Terminal window
terraform init # download providers, set up the backend
terraform fmt -recursive # format the files
terraform validate # check the configuration
terraform plan -out=tfplan # what would change, saved
terraform apply tfplan # apply exactly that plan
terraform apply -auto-approve # plan and apply, no question (CI)
terraform destroy

tofu takes the same commands in OpenTofu.

init options

Terminal window
terraform init -upgrade # newer provider versions within the constraints
terraform init -reconfigure # backend changed: start fresh
terraform init -migrate-state # backend changed: copy the state over
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64

Commit .terraform.lock.hcl, not the .terraform/ folder.

Targeted changes

Terminal window
terraform plan -target=aws_instance.web # only this (exceptional use)
terraform apply -replace=aws_instance.web # recreate it
terraform plan -refresh-only # see drift, change nothing
terraform plan -var 'env=prod' -var-file=prod.tfvars

State

Terminal window
terraform state list
terraform state show aws_instance.web
terraform state mv aws_instance.web aws_instance.app
terraform state rm aws_instance.old # forget it, don't destroy it
terraform force-unlock LOCK_ID # only if no run is still active

Import and move, in code

import {
to = aws_s3_bucket.logs
id = "acme-logs"
}
moved {
from = aws_instance.web
to = aws_instance.app
}
Terminal window
terraform plan -generate-config-out=generated.tf # write resource blocks for imports

Outputs, workspaces, console

Terminal window
terraform output
terraform output -json
terraform output -raw db_endpoint
terraform workspace list
terraform workspace new staging
terraform workspace select staging
terraform console # try expressions: > upper("x")

Debugging

Terminal window
TF_LOG=DEBUG terraform plan 2> debug.log
terraform graph | dot -Tsvg > graph.svg
terraform version

Common errors

Other cheat sheets