Skip to Content
Mengty Learns
IaC

OpenTofu Cheatsheet

Everyday tofu CLI commands for init, plan, apply, state inspection, formatting, and unlocking—verified against the OpenTofu 1.13 docs.

referencebeginnerTested with opentofu 1.13 (docs only; tofu not installed locally)Updated Nov 27, 2026

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 creds

Plan, 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 destroy

State & 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 tfplan files: saved plan files can contain plaintext secret variables and resource attributes.
  • Avoid -target as a habit: targeting skips dependency graphs and leaves state partially converged.
  • Prefer moved {} and import {} blocks in .tf files over imperative tofu state mv / tofu import so 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