Skip to Content
Mengty Learns
IaC

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.

explanationbeginnerTested with opentofu 1.13 (docs; tofu not installed locally)Updated Nov 26, 2026

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

TL;DR

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.

OpenTofu reconciles desired configuration (.tf) with real-world state via refresh before computing a diff.

Solution

OpenTofu splits every change into a two-step preview-and-execute workflow:

  1. Read Config + Prior State: OpenTofu parses .tf files to build the dependency graph and loads terraform.tfstate (local or remote backend) to find existing resource IDs.
  2. Refresh: Providers query the real environment for every resource in state, updating the in-memory state snapshot with any out-of-band changes.
  3. Diff: OpenTofu compares desired attributes in .tf against the refreshed state and marks each resource as no-op, create (+), update-in-place (~), replace (-/+), or destroy (-).
  4. Plan: tofu plan prints the diff (and optionally writes a binary plan file with -out=tfplan) without mutating anything.
  5. Apply → New State: tofu apply executes 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

Overview of OpenTofu Lifecycle: Config + State → Plan → Apply. 4 steps.
Overview4 steps in this flow

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 tfplan

When to use / when to avoid

When to use
  • 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.
When to avoid
  • 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

Pros
  • Dry-run safety: tofu plan shows exact creates, updates, and destroys before mutating anything.
  • Saved plan files (-out=tfplan) make CI/CD approvals deterministic.
Cons
  • 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