Useful Commands¶
A grab-bag of Terraform / OpenTofu commands worth keeping handy. Most work identically with tofu swapped in for
terraform. Use the one your project standardises on.
Init¶
Standard init.
Re-pull modules and providers without touching backend config.
Re-configure the backend (e.g. switching workspaces / accounts).
Migrate state to a newly-changed backend without prompting.
Plan¶
Plan to a file you can review and apply atomically.
Target a single resource (use sparingly: it bypasses dependency tracking).
Plan with a specific tfvars file (per-environment workflow).
Generate import commands from a plan¶
Useful when you need to adopt a pile of pre-existing real-world resources into a fresh state file in bulk. The
command below scans plan output for will be created lines and emits a terraform import template for each.
You still have to fill in RESOURCE_ID (the cloud-provider ID) for every line before running them:
terraform plan -no-color -var-file=<vars-file-name>.tfvars \
| grep "will be created" \
| sed "s/.*# \(.*\) will be created/terraform import -lock=false -var-file=<vars-file-name>.tfvars '\1' RESOURCE_ID/"
Apply¶
Apply a saved plan: no surprises between plan and apply.
Auto-approve (CI only, never local).
Replace a resource that has drifted or is corrupt.
State inspection¶
List every resource Terraform manages.
Show the full attributes of one resource.
Pull the raw state JSON (great for grep / jq).
State surgery¶
State mutation is destructive
Always back up state before running anything below. Never run these from CI.
Back up state first.
Rename a resource address after a refactor (no destroy/create).
Move a resource into a module.
Forget a resource without destroying the real-world object.
Adopt an existing real-world resource into state.
Refactoring with moved blocks¶
Prefer moved {} blocks in code over terraform state mv whenever possible: they're versioned, reviewable,
and run automatically for every collaborator.
Drift detection¶
Detect drift without applying. Exit code 2 means changes are pending.
Refresh state against real-world resources without planning changes.
Outputs¶
Print all outputs.
Get a single output as raw text (script-friendly).
Get all outputs as JSON.
Formatting & validation¶
Format every .tf file under cwd.
Check formatting without changing files (use in CI).
Validate syntax and types.
Workspaces¶
List workspaces.
Create a new workspace.
Switch to a workspace.
Show the current workspace.
Graph & dependency inspection¶
Render the dependency graph as Graphviz DOT.
Show the providers a config / state requires.
Dump every provider's schema as JSON.
Modules¶
Refresh installed modules without re-initialising the backend.
Console¶
Interactive REPL for evaluating expressions against state and config.
Inside the console:
Testing¶
Run native module tests under tests/.
Filter to a single test file.
OpenTofu-only¶
State encryption (built-in, no third-party tooling required).
Early-evaluation: variables in backend / module source blocks.
Cleanup¶
Remove the local .terraform directory and lockfile.
Destroy every managed resource (read the plan twice).