pa-07 — Analysis

What the engine must get right

  1. Plan correctness: create/update(+diff)/delete/no-op vs state.
  2. Dependency-ordered apply: topo order for create/update, reverse for delete; reject cycles and missing deps.
  3. Idempotency: re-apply == no-op.
  4. Drift: report where the live world diverges from state (incl. unmanaged resources).

Design decisions

  • Deterministic topo sort (Kahn). Sorted ready-set so plans/applies are reproducible (testable, reviewable) — the same determinism discipline as the rest of the book.
  • State as the diff baseline. Plan diffs desired vs last-applied, not desired vs live — matching real tools (and why drift, a separate state-vs-live check, exists).
  • Deletes in reverse order. Dependencies must be torn down children- first; computing this from the graph avoids orphan/ordering errors.
  • Attr-map equality for change detection. Minimal but real: a changed attribute yields an Update with a human-readable diff.

Tradeoffs worth flagging

  • State is the crown jewel and the biggest hazard. Lost/stale/corrupt state makes the engine plan destructive nonsense. Production needs remote state + locking + backups + import; this lab keeps state in-memory to expose the mechanics.
  • Plan/apply is not transactional. A partial apply (some resources created, then a failure) leaves a mixed world; real tools record partial state and continue/retry. Model this as an exercise.
  • Drift correction can be destructive. Auto-reverting drift (pa-08 self-heal) overwrites a human's emergency fix; sometimes you want alert-not-revert. An operational policy choice.
  • Ordering is from declared deps only. Implicit dependencies (a resource referencing another's output) must be made explicit or the graph is wrong — a real Terraform footgun.

What production adds beyond this lab

  • Providers (real cloud APIs), modules, variables/outputs, remote state + locking, partial-apply recovery, import, and plan as a reviewed CI artifact gating apply (GitOps, pa-08).