How Plan and Apply Work: State, Refresh, Diff
How OpenTofu combines .tf configuration and state to refresh real infrastructure, compute a diff, preview a plan, and apply changes.
TL;DR
tofu plan never guesses from .tf files alone: it loads your configuration and prior state, refreshes tracked resources against real infrastructure, computes the diff, and produces an execution plan that tofu apply executes and records into new state.
Intent
Understand the exact data flow behind tofu plan and tofu apply so you can predict creates, in-place updates, replacements, and destroys—and avoid accidental state drift.
Problem
If an IaC tool only read .tf files, it would have no way to know whether a resource already exists, which cloud ID corresponds to local_file.manifest, or whether someone changed a setting manually in the console. Without a state record, every run would risk recreating existing infrastructure or orphaning deleted resources.
Solution
OpenTofu splits every change into a two-step preview-and-execute workflow:
- Read Config + Prior State: OpenTofu parses
.tffiles to build the dependency graph and loadsterraform.tfstate(local or remote backend) to find existing resource IDs. - Refresh: Providers query the real environment for every resource in state, updating the in-memory state snapshot with any out-of-band changes.
- Diff: OpenTofu compares desired attributes in
.tfagainst the refreshed state and marks each resource as no-op, create (+), update-in-place (~), replace (-/+), or destroy (-). - Plan:
tofu planprints the diff (and optionally writes a binary plan file with-out=tfplan) without mutating anything. - Apply → New State:
tofu applyexecutes the planned provider calls in dependency order and writes the resulting attributes back to state.
Real-world analogy
▶ Flow: watch it work
OpenTofu Lifecycle: Config + State → Plan → Apply
Press Start or tap any step dot to trace the path.
▸All steps (4)
Code
Using the repo's labs/tofu-local-file project (note: tofu is not installed in this environment, so these commands are marked not run):
# Run against labs/tofu-local-file (not run — tofu binary not installed here)
cd labs/tofu-local-file
tofu init
tofu plan -var="environment=dev" -out=tfplan
tofu apply tfplanWhen to use / when to avoid
- You want a reviewable diff before any infrastructure change is applied (for example, posting plan output on a pull request).
- You need dependency-ordered provisioning and clean teardown (tofu destroy) using tracked state.
- You are managing rapidly changing Kubernetes Pods/Deployments (use Kubernetes controllers or GitOps instead).
- You have no remote state locking in a team environment—set up a remote backend with locking before sharing state.
How to implement
Initialize the working directory
Run tofu init to download providers and configure the state backend.
Generate and review a saved plan
Run tofu plan -out=tfplan and inspect +, ~, and especially - / -/+ (destroy/replace) lines.
Apply the exact plan
Run tofu apply tfplan so no unexpected drift slips in between review and execution.
Pros / Cons
- Dry-run safety: tofu plan shows exact creates, updates, and destroys before mutating anything.
- Saved plan files (-out=tfplan) make CI/CD approvals deterministic.
- State files can contain sensitive attributes and must be encrypted and locked in a remote backend for teams.
- Manual console edits cause drift that can surprise the next plan.
Relations
Check yourself
Check Yourself
1. Why does tofu plan run a refresh step before computing the diff?
2. What is the main benefit of running tofu apply tfplan with a saved plan file?
Sources
Related Notes
OpenTofu First Project
Write and run a self-contained OpenTofu project using only the local and random providers with variables and outputs.
Remote State and Locking in OpenTofu / Terraform
Why local tfstate breaks in teams, how to configure S3 or GCS remote backends with state locking, and how OpenTofu encrypts state client-side.