Skip to content

Menu

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 init

Download providers and modules and set up the backend for this directory

From the docs, not run by us
terraform init -upgrade

Ignore the lock file's chosen versions and pick the newest provider versions your constraints allow

From the docs, not run by us
terraform init -migrate-state

Move existing state to a backend you just changed in the configuration

From the docs, not run by us
terraform plan -out=tfplan

Save the plan to a file so the apply runs exactly what you reviewed

Try it: Terraform Plan ViewerFrom the docs, not run by us
terraform apply tfplan

Apply a saved plan without asking again, since you already reviewed it

From the docs, not run by us
terraform plan -detailed-exitcode

Return exit code 2 when there are changes, so CI can tell drift from a clean run

From the docs, not run by us
terraform plan -replace=aws_instance.web

Plan to destroy and recreate one resource instance even though its config didn't change

From the docs, not run by us
terraform plan -refresh-only

Show how real infrastructure has drifted from state without proposing config changes

From the docs, not run by us
terraform plan -destroy

Preview what a destroy would remove; terraform destroy is the shortcut that applies it

From the docs, not run by us
terraform plan -var-file=prod.tfvars -var 'region=eu-west-1'

Set input variables from a file and override one on the command line

From the docs, not run by us

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 list

List every resource address tracked in state

From the docs, not run by us
terraform state show 'aws_instance.web[0]'

Print the stored attributes of one resource instance; quote addresses with brackets

From the docs, not run by us
terraform state mv 'packet_device.main' 'packet_device.all[0]'

Change a resource's address in state without destroying the real object

From the docs, not run by us
terraform state rm aws_instance.legacy

Stop tracking a resource without destroying it; a removed block does this in code

From the docs, not run by us
terraform output -json

Print root module outputs as JSON for scripts

From the docs, not run by us

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 list

List workspaces; the current one has an asterisk

From the docs, not run by us
terraform workspace new feature-x

Create a workspace with an empty state and switch to it

From the docs, not run by us
terraform workspace select -or-create staging

Switch to a workspace, creating it first if it doesn't exist

From the docs, not run by us
terraform workspace show

Print the name of the workspace you are in, useful in scripts

From the docs, not run by us

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

Try it: terraform-snippet-generatorFrom the docs, not run by us
terraform plan -generate-config-out=generated.tf

Write HCL for imported resources that don't have a resource block yet (marked experimental)

From the docs, not run by us
moved { from = aws_instance.a to = aws_instance.b }

Rename a resource or move it into a module without destroy and recreate

From the docs, not run by us
removed { from = aws_instance.legacy lifecycle { destroy = false } }

Drop a resource from Terraform's management but leave the real object running

From the docs, not run by us
terraform plan -target=module.network

Plan only this address and what it depends on; meant for recovery, not routine use

From the docs, not run by us
terraform fmt -recursive

Rewrite .tf files in this directory and all subdirectories to the canonical style

From the docs, not run by us
terraform fmt -check -diff

Fail without changing files if anything is unformatted, and show the diff; good for CI

From the docs, not run by us
terraform validate

Check the configuration for syntax and internal consistency without touching remote state

From the docs, not run by us
terraform plan -minimal-refresh

Terraform 1.17+: refresh only the resources that have proposed changes, to cut API calls

From the docs, not run by us

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 tfplan

Same saved-plan workflow as Terraform under the tofu binary

From the docs, not run by us
tofu plan -exclude=module.legacy

Plan everything except this address and what depends on it; Terraform has no -exclude

From the docs, not run by us
tofu plan -target-file=targets.txt

Read target addresses from a file instead of repeating -target flags

From the docs, not run by us
tofu plan -exclude-file=skip.txt

Read addresses to exclude from a file; can't be combined with -target-file

From the docs, not run by us
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

From the docs, not run by us
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.

From the docs, not run by us

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 apply straight after reviewing a plan in another terminal or CI step.

    Save the plan with -out and apply that file. A bare apply re-plans and may include changes you never saw.

  • Using terraform state mv on a laptop to rename resources that the rest of the team also manages.

    Add a moved block and commit it, so every workspace and every colleague gets the same move during a normal plan.

  • Making -target part 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 -upgrade casually and committing whatever provider versions it picked.

    Upgrade providers on purpose: read the changelog, run init -upgrade, review the .terraform.lock.hcl diff and the plan before you commit.

  • Assuming every OpenTofu feature works when you switch the binary back to Terraform.

    Features like state encryption, enabled and -exclude exist 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 select can't reach it.

Sources

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.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…