skip to content

Walk through the phases an infrastructure-as-code tool goes through when it previews a change, from reading the current world to applying it. What happens in each?

level: middleimportance: must knowfreq 78%

answer

  1. read reality before comparing anything
  2. four phases, exactly one gate
  3. refresh, then diff, then approve
  4. apply walks the dependency graph
  5. the gap between diff and apply

basics

~20 s

A preview runs in four phases: refresh, where the tool re-reads live infrastructure; diff, where it compares your declared configuration against that reality and lists proposed actions; approval, where a human or policy gate reviews them; and apply, which executes the approved actions in dependency order.

solid answer

~50 s

Every plan-style preview has the same four-step shape. First a refresh or read phase: the tool queries the real provider APIs for the objects it manages, so the comparison is against reality rather than a possibly stale record. Second, the diff: it compares the declared desired state to that refreshed reality and produces a per-object action — create, update in place, replace, destroy, or no change — plus an execution order derived from the dependency graph. Third, an approval gate: a human reading the diff, automated checks, or both. This is the cheapest moment to stop a destructive change. Fourth, apply: the tool executes the approved actions, usually in graph order with some parallelism, recording results as it goes. The property that matters is that the diff and the apply are separated in time, which is exactly where the residual risk lives.

go deeper

for a junior

Be able to say that the tool shows you the changes before making them, and that you read that output before approving anything. Know the run happens in two commands: one that only looks, one that acts.

for a middle

Explain the four phases and what each one contacts — refresh reads the provider APIs, diff compares against your declaration, approval gates, apply executes in dependency order. Name the four possible actions in a diff.

for a senior

Show that you know what the preview does not cover: unknown values assigned at creation, permission and quota failures that only appear on write, and the fact that apply is not transactional so a mid-run failure leaves a partially changed estate.

for a principal

Own the question of where the preview sits in a delivery pipeline: how long the gap between diff and apply is allowed to be, who is entitled to approve which diffs, and what you accept as residual risk rather than trying to engineer away.

## Why a preview exists at all Infrastructure code is pointed at systems where a mistake is expensive and often irreversible: a deleted database, a detached volume, a security group opened to the internet. An imperative script gives you no way to know what it will do except to run it. A declarative tool can do something better — compute the set of API calls it *would* make and show them to you without making any of them. That dry run is the single property that makes it reasonable to let a pipeline change production. The preview is not a simulation of the cloud. It is a diff between three things: what you declared, what the tool has recorded about the objects it manages, and what those objects actually look like right now. ## Phase 1 — refresh / read The tool takes its record of managed objects and queries the provider APIs for each one, reading current attributes. Nothing is written. The purpose is to make the diff a statement about reality rather than about a record that may be months out of date — someone may have resized an instance in the console, or an autoscaler may have moved a count. This phase costs API calls roughly proportional to the number of managed objects, so on a large estate it is the slow part, and it is the phase most likely to hit provider rate limits. Most tools let you skip or narrow it, accepting a stale baseline in exchange for speed. Stateless configuration-management tools do the equivalent by gathering facts and letting each resource check its own current condition on the host; a controller-style system does it by listing live objects. The phase is universal even when the record is not. ## Phase 2 — diff For each declared object the tool compares desired attributes against the refreshed actual attributes and emits an action: ``` + create network / app-vpc ~ update compute / api-server (size: small -> medium) +/- replace database / reports (engine version cannot change in place) - destroy storage / old-bucket ``` It also computes ordering: a subnet before the instance in it, the instance before the DNS record pointing at it. The diff is therefore both a list and a plan of execution. One real limitation belongs here. Some values do not exist until the provider assigns them — generated identifiers, allocated addresses, computed endpoints. The preview shows those as unknown, and every expression downstream of an unknown is also unknown. A preview containing a lot of unknowns is genuinely less informative, and no amount of reviewing recovers that. ## Phase 3 — approval The diff is handed to a decision-maker: a human reading it in a pull request or a terminal prompt, an automated rule set, or both. Two things make this phase real rather than ceremonial — the reviewer must be able to decline, and the artifact they reviewed must be the artifact that gets executed. Where the pipeline recomputes the diff after approval, the approval covered something that no longer exists. ## Phase 4 — apply The tool walks the graph, executing independent actions in parallel up to a concurrency limit, and updating its record as each call returns. Crucially, apply is usually **not** transactional: it issues many independent API calls, and if the tenth fails, the first nine already happened. The record is updated for what succeeded, so the next preview starts from a truthful, partially-applied world. Some server-side systems do attempt an automatic rollback on failure, which trades that truthfulness for a rollback that can itself get stuck. ## What the preview cannot tell you It cannot tell you the change is *correct* — only what will be attempted. It cannot resolve values the provider assigns at creation. It cannot show the effects of any script the change runs on a machine. It will not catch permission or quota failures that only surface on a write. And it describes the world as of the refresh, not as of the apply. ## Interview framing Say the four phases in order, name what each contacts, and finish on the honest limits: unknowns, non-transactional apply, and the time gap before execution. That last point is usually the follow-up the interviewer is steering toward.

  • Why does the preview refresh from the provider APIs instead of just diffing your config against the tool's own record?
    Because the record is only what the tool last saw. Anything can have changed the real object since — a console edit, an autoscaler, provider-side maintenance. Diffing against the record alone produces a preview that looks clean while the apply does something else, or that proposes an update to an object that no longer exists. Refresh costs API calls and time, which is why teams sometimes skip it deliberately and accept a stale baseline.
  • If a preview shows no changes, can you conclude the apply will succeed?
    No. An empty diff means the tool intends no API calls, which is a different claim from success. Permissions, quotas, and provider-side validation are only exercised on write, so a non-empty apply can still fail on the first call. And the diff describes the world as of the refresh — an empty preview says nothing about the state of things by the time you run it.
  • Why do parts of a preview show as unknown, and what does that do to the review?
    Attributes assigned by the provider at creation — identifiers, allocated addresses, computed endpoints — do not exist yet at preview time, and any expression built from them is unknown too. This propagates: one unknown input can render whole downstream blocks unreviewable. The practical consequence is that a preview for a large greenfield change is much weaker evidence than one for an incremental edit to existing resources.

saying these in an interview costs you the question

  • Thinks the refresh phase modifies infrastructure rather than only reading it
  • Diffs the configuration against the tool's record and skips reality entirely
  • Cannot name any action beyond create and destroy
  • Believes an empty preview guarantees a successful apply
  • Assumes apply is transactional and rolls back everything on failure

context