Plan/Apply Workflow & CI

💤0
Lv 10 XP
← 📜 Infrastructure as Code · Terraform

Plan/Apply Workflow & CI

Advanced ⭐ 120 XP ⏱ 18 min #terraform#ci#workflow

Run Terraform safely in a team via pull requests, plan review, and automated apply.

📖Theory

In a team, you don’t run apply from your laptop. Infrastructure changes flow through pull requests, just like application code:

  1. Open a PR with the Terraform change
  2. CI runs fmt, validate, and plan, posting the plan to the PR
  3. A human reviews the plan — especially destroy actions
  4. On merge, CI runs apply (often with a manual approval gate)

This gives review, an audit trail, and consistent automated runs. Use remote state with locking, a -out plan file so apply matches the reviewed plan, and OIDC for keyless cloud auth in CI.

graph LR
  PR["Pull request"] --> CI["fmt + validate + plan"]
  CI --> REV["Human reviews plan"]
  REV --> M["Merge"]
  M --> AP["apply (gated)"]
Terraform CI flow
✍️Hands-On Exercise
  1. Describe how a Terraform change should flow from PR to production.
  2. Explain why CI posts the plan to the pull request.
  3. Why save a plan file and apply exactly that file?
  4. How should CI authenticate to the cloud without storing static keys?
🧾Cheat Sheet
StepCommand
Formatterraform fmt -check
Validateterraform validate
Plan to fileterraform plan -out=tfplan
Apply that planterraform apply tfplan
ReviewHuman reads plan in PR
Auth in CIOIDC (no static keys)
StateRemote + locking
💬Common Interview Questions
How do you run Terraform safely in a team?

Through pull requests: CI runs fmt/validate/plan and posts the plan for human review, then applies on merge (often with an approval gate), using remote state with locking and keyless OIDC auth.

Why save and apply a plan file rather than re-planning at apply time?

So apply executes exactly what was reviewed. A fresh plan at apply could pick up new drift or changes and do something different from what the reviewer approved.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type