OpenTofu Cheatsheet
Everyday tofu CLI commands for init, plan, apply, state inspection, formatting, and unlocking—verified against the OpenTofu 1.13 docs.
TL;DR
Daily tofu commands for initializing, planning, applying, formatting, and safely inspecting state. (Note: tofu is not installed on this local machine, so every command below was verified against the OpenTofu 1.13 documentation and marked # not run.)
Setup & validation
# Initialize working directory, download providers, configure backend (not run)
tofu init
tofu init -upgrade # upgrade providers within versions.tf constraints
tofu init -backend=false # init providers only (great for CI validate)
# Format and validate HCL (not run)
tofu fmt -recursive # auto-format all .tf files
tofu fmt -check -recursive # fail if unformatted (CI gate)
tofu validate # syntax & type check without cloud credsPlan, apply, and destroy
# Preview changes and save exact plan for CI apply (not run)
tofu plan -out=tfplan
tofu apply tfplan # applies exact saved plan (no confirmation prompt)
# Target a single resource for emergency debugging only (not run)
tofu plan -target=aws_instance.bastion
# Preview and tear down all managed resources (not run)
tofu plan -destroy
tofu destroyState & outputs
# Inspect state without touching cloud resources (not run)
tofu state list
tofu state show aws_s3_bucket.assets
tofu output -json
# Remove a resource from state WITHOUT deleting it in the cloud (not run)
tofu state rm aws_s3_bucket.legacy
# Import an existing cloud resource into state (or use import {} blocks) (not run)
tofu import aws_s3_bucket.assets my-existing-bucket
# Release a stuck lock ONLY after confirming no apply is running (not run)
tofu force-unlock <LOCK_ID>Gotchas
- Never commit
tfplanfiles: saved plan files can contain plaintext secret variables and resource attributes. - Avoid
-targetas a habit: targeting skips dependency graphs and leaves state partially converged. - Prefer
moved {}andimport {}blocks in.tffiles over imperativetofu state mv/tofu importso teammates and CI get the same state transitions.
Check yourself
Check Yourself
1. You want CI to validate HCL syntax and provider schemas on a pull request without needing AWS credentials for the remote backend. Which command sequence works?
2. What does tofu state rm aws_s3_bucket.logs do?
Sources
Related Notes
Terraform vs OpenTofu: License, Features, and Choice
Compare HashiCorp Terraform and OpenTofu across license history (BSL vs MPL), feature divergence, compatibility, and which one to pick.
GitHub Actions Workflow
How to write a minimal, supply-chain-safe GitHub Actions CI workflow with SHA-pinned actions and deterministic installs.