Q&A-3Design the plan-and-apply workflow.
Answer
Assume a Terraform-like tool, remote state with locking, CI/CD, and a shared production environment. The plan is a pure function of four things: config, variables, provider/module versions, and the exact state snapshot. Apply mutates state. So a plan is only valid against the state serial it read. That single fact drives the workflow.
Core answer: freeze config, module and provider versions plus validated inputs. Read state under a lock or consistent read. Compute the plan: creates, updates, replacements, deletes, unknowns, cost, policy results. Persist the plan file with a hash, the state serial, actor, scope, and expiry. Approve. Before apply, re-read the state serial and compare. If it changed, re-plan. If not, acquire the lock, apply with short-lived credentials, stream status, verify outputs and effective policy, reconcile partial failure, retain audit.
Why the signed plan goes stale: Terraform state carries lineage and serial. The plan file stores the prior state serial. If another apply bumps the serial, Terraform refuses with "Saved plan is stale". That is the mechanism. It is not bureaucracy; it prevents your apply from deleting a resource someone else just created. Example: plan reads serial 41. A teammate applies and state becomes 42. Your apply errors. If you force it, you risk a destructive diff against state you never saw.
Trade-offs: plan expiry vs re-plan-on-apply. Expiry is cheap and bounds approval age, but it does not catch state changes. Re-plan-on-apply is safest but discards the exact diff a human approved. Manual approval gates catch intent; policy-as-code catches invariants like "no public buckets". Use both when blast radius is high. A lighter workflow is fine for a dev sandbox with a single writer and no shared state: skip approval, keep the lock.
Failure modes:
| Failure | Detection | Cost |
|---|---|---|
| Lock contention | lock error | pipeline stalls; set -lock-timeout=5m instead of default 0s |
| State corruption mid-apply | state serial mismatch, refresh shows half-created | manual import/reconcile |
| Drift between plan and apply | stale plan error | re-plan |
| Partial apply | apply exit code + refresh | orphaned resources |
Concrete numbers: Terraform's -lock-timeout defaults to 0s, so a contended lock fails immediately. Set 5m for CI. Plan TTL: 30 minutes is a common bound. Credential TTL: AWS STS AssumeRole defaults to 3600s; use 15-60 minutes for apply. These are examples, not universal defaults.
What changes the decision: if your backend has no locking or you are single-writer, drop the lock and approval gate. If you have compliance or shared prod, keep signed plan, TTL, and re-check serial before apply.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.