Terraform and OpenTofu Cheat Sheet: Plan, Apply, State and Import
Checked against Terraform / OpenTofu Terraform 1.17 docs (latest release v1.17.2); OpenTofu 1.13 docs (latest release v1.13.10) on 28 November 2026. Each section links to the official docs it was checked against.
This sheet covers the everyday CLI for Terraform and OpenTofu, which share almost every command: swap terraform for tofu and the rows still work unless a note says otherwise. It follows the Terraform 1.17 docs and the OpenTofu 1.13 docs as of 28 Nov 2026. Neither binary is installed in the shell used to write it, so the rows come from the documentation and weren't run.
When to use what
Always save the plan and apply that file: plan -out=tfplan, read it, then apply tfplan. Running apply on its own makes a fresh plan, and the world can change between the plan you reviewed and the one that runs. In CI, -detailed-exitcode tells you whether there is anything to apply without parsing text. The Terraform plan viewer helps when a JSON plan is too long to read in a terminal.
For refactoring, prefer blocks in code over state commands. A moved block renames a resource in a reviewed commit and works for everyone who pulls it; terraform state mv changes the state once, on one machine, with no record in git. The same goes for import blocks versus the old terraform import command, and for removed blocks versus state rm. Keep the state commands for emergencies.
Treat -target as a crowbar, not a workflow. Both projects say so in their docs, because a targeted apply leaves the rest of the configuration unchecked. If you keep needing it, your configuration probably wants splitting into smaller root modules.
Terraform or OpenTofu? The CLI is the same muscle memory. OpenTofu adds things Terraform doesn't have, like state encryption, -exclude and the enabled meta-argument, and Terraform 1.17 has its own additions such as -minimal-refresh. Pick by licence and by the features your team needs; our Terraform vs Pulumi comparison covers the wider choice, and the Terraform MCP server gives an AI assistant provider and module docs while you write code.
Exit codes with -detailed-exitcode: 0 means no changes, 1 means error, 2 means the plan has changes.
terraform initDownload providers and modules and set up the backend for this directory
terraform init -upgradeIgnore the lock file's chosen versions and pick the newest provider versions your constraints allow
terraform init -migrate-stateMove existing state to a backend you just changed in the configuration
terraform plan -out=tfplanSave the plan to a file so the apply runs exactly what you reviewed
terraform apply tfplanApply a saved plan without asking again, since you already reviewed it
terraform plan -detailed-exitcodeReturn exit code 2 when there are changes, so CI can tell drift from a clean run
terraform plan -replace=aws_instance.webPlan to destroy and recreate one resource instance even though its config didn't change
terraform plan -refresh-onlyShow how real infrastructure has drifted from state without proposing config changes
terraform plan -destroyPreview what a destroy would remove; terraform destroy is the shortcut that applies it
terraform plan -var-file=prod.tfvars -var 'region=eu-west-1'Set input variables from a file and override one on the command line
Every state command that writes makes a backup first, but on a shared backend you still want a lock and a quiet moment. Prefer moved, import and removed blocks for planned changes.
terraform state listList every resource address tracked in state
terraform state show 'aws_instance.web[0]'Print the stored attributes of one resource instance; quote addresses with brackets
terraform state mv 'packet_device.main' 'packet_device.all[0]'Change a resource's address in state without destroying the real object
terraform state rm aws_instance.legacyStop tracking a resource without destroying it; a removed block does this in code
terraform output -jsonPrint root module outputs as JSON for scripts
CLI workspaces give one configuration several state files on the same backend. They suit short-lived copies; separate directories or backends are clearer for prod versus staging.
terraform workspace listList workspaces; the current one has an asterisk
terraform workspace new feature-xCreate a workspace with an empty state and switch to it
terraform workspace select -or-create stagingSwitch to a workspace, creating it first if it doesn't exist
terraform workspace showPrint the name of the workspace you are in, useful in scripts
These blocks run as part of a normal plan and apply, so the change shows up in review. Once applied everywhere, you can delete moved and import blocks or keep them as history.
import {
to = aws_instance.web
id = "i-0abc123"
}Adopt an existing object into state at the next apply instead of running terraform import
terraform plan -generate-config-out=generated.tfWrite HCL for imported resources that don't have a resource block yet (marked experimental)
moved {
from = aws_instance.a
to = aws_instance.b
}Rename a resource or move it into a module without destroy and recreate
removed {
from = aws_instance.legacy
lifecycle {
destroy = false
}
}Drop a resource from Terraform's management but leave the real object running
terraform plan -target=module.networkPlan only this address and what it depends on; meant for recovery, not routine use
terraform fmt -recursiveRewrite .tf files in this directory and all subdirectories to the canonical style
terraform fmt -check -diffFail without changing files if anything is unformatted, and show the diff; good for CI
terraform validateCheck the configuration for syntax and internal consistency without touching remote state
terraform plan -minimal-refreshTerraform 1.17+: refresh only the resources that have proposed changes, to cut API calls
OpenTofu mirrors the Terraform commands above as tofu init, tofu plan, tofu state mv and so on. The rows here are the places it differs.
tofu plan -out=tfplan && tofu apply tfplanSame saved-plan workflow as Terraform under the tofu binary
tofu plan -exclude=module.legacyPlan everything except this address and what depends on it; Terraform has no -exclude
tofu plan -target-file=targets.txtRead target addresses from a file instead of repeating -target flags
tofu plan -exclude-file=skip.txtRead addresses to exclude from a file; can't be combined with -target-file
lifecycle {
enabled = var.create_bucket
}OpenTofu 1.11+: create or skip a single resource without count; can't be used with count or for_each
terraform {
encryption {
key_provider "pbkdf2" "main" { passphrase = var.passphrase }
method "aes_gcm" "main" { keys = key_provider.pbkdf2.main }
state { method = method.aes_gcm.main }
}
}Encrypt state at rest with OpenTofu's built-in state encryption; Terraform has no equivalent block
Can also be set through the TF_ENCRYPTION environment variable. Read the rollover section before changing keys.
Worked example: Rename a resource safely and prove nothing gets destroyed
You renamed aws_s3_bucket.logs to aws_s3_bucket.access_logs in code. Without help, Terraform would plan to destroy the old bucket and create a new empty one.
# main.tf
resource "aws_s3_bucket" "access_logs" {
bucket = "acme-access-logs"
}
moved {
from = aws_s3_bucket.logs
to = aws_s3_bucket.access_logs
}
terraform fmt -check
terraform validate
terraform plan -out=tfplan -detailed-exitcode
echo "exit: $?"
terraform apply tfplan
What to look for: the plan should say the resource has moved and list no destroy or create for the bucket. If the summary line counts any resource to destroy, stop and check the addresses in the moved block. With -detailed-exitcode, exit code 2 is expected here because the move is a change to state; 1 would mean an error. After the apply, terraform state list shows only the new address. The same files and commands work with tofu in place of terraform. This example follows the docs and wasn't run, because neither binary is installed here.
Common mistakes
Running
terraform applystraight after reviewing a plan in another terminal or CI step.Save the plan with
-outand apply that file. A bareapplyre-plans and may include changes you never saw.Using
terraform state mvon a laptop to rename resources that the rest of the team also manages.Add a
movedblock and commit it, so every workspace and every colleague gets the same move during a normal plan.Making
-targetpart of the regular deploy script.Apply the whole configuration. If one part is too slow or risky, split it into its own root module with its own state.
Running
terraform init -upgradecasually and committing whatever provider versions it picked.Upgrade providers on purpose: read the changelog, run
init -upgrade, review the.terraform.lock.hcldiff and the plan before you commit.Assuming every OpenTofu feature works when you switch the binary back to Terraform.
Features like state encryption,
enabledand-excludeexist only in OpenTofu. If you need to stay portable, avoid them or keep them in modules only OpenTofu uses.Using workspaces to separate production from staging in the same backend and credentials.
Give production its own backend and credentials so a wrong
workspace selectcan't reach it.
Related cheat sheets
Sources
- https://developer.hashicorp.com/terraform/cli/commands/plan
- https://developer.hashicorp.com/terraform/cli/commands/apply
- https://developer.hashicorp.com/terraform/cli/commands/init
- https://developer.hashicorp.com/terraform/cli/commands/state/mv
- https://developer.hashicorp.com/terraform/cli/commands/state/rm
- https://developer.hashicorp.com/terraform/cli/commands/workspace
- https://developer.hashicorp.com/terraform/cli/commands/fmt
- https://developer.hashicorp.com/terraform/language/block/import
- https://developer.hashicorp.com/terraform/language/moved
- https://developer.hashicorp.com/terraform/language/block/removed
- https://github.com/hashicorp/terraform/releases/tag/v1.17.0
- https://opentofu.org/docs/cli/commands/plan/
- https://opentofu.org/docs/language/state/encryption/
- https://opentofu.org/docs/language/meta-arguments/enabled/
- https://github.com/opentofu/opentofu/releases/tag/v1.13.0
How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.