#plan-vs-apply-semantics
What does `terraform plan` do versus `apply`? What does apply do with a plan?
Что отвечать
Plan reads state plus HCL, queries the provider (refresh), computes the diff, and prints what it intends to do and why. It changes nothing in the provider. Apply takes an already computed plan (from the file `-out=plan.tfplan` or on the fly through the interactive prompt) and runs the change set. If apply runs without `-out`, it makes a plan under the hood and then applies, which is convenient locally. In CI the right way is plan, then artifact, then apply from the artifact. That way review sees exactly what ships.
Что хотят услышать
A senior should: - name the key difference: plan is side-effect free (apart from refresh), apply mutates and updates state - explain `-out=plan.tfplan`: a binary artifact that pins a snapshot of state plus the change set, and apply runs exactly that change set - say that state can change between plan and apply (someone went into the Console and changed it), so an apply on a stale plan can fail with "expected state did not match" - mention `terraform show -json plan.tfplan` for parsing and post-processing in CI (cost estimation, OPA policy)
Подводные камни
- ✗ Running `apply -auto-approve` in CI without a saved plan. What the reviewer saw and what shipped can differ
- ✗ Forgetting that a refresh between plan and apply can find new drift, and an apply on a stale plan fails
- ✗ Saving the plan file as a public artifact. It holds state secrets in plain text
Follow-up
- ? Why use `-out` when you can run `terraform apply` directly?
- ? What happens if apply tries to apply a stale plan?
- ? How do you protect the tfplan artifact in CI? What in it is sensitive?
Глубина в базе знаний