How do you run Terraform safely in CI/CD?
What they are really testing: Senior signal: plan on PR for review, apply on merge, remote state with locking, policy-as-code gates, and least-privilege short-lived credentials, rather than running apply from a laptop.
A real interview question
How do you run Terraform safely in CI/CD?
What most people say
drag me
“I would have the pipeline run terraform apply automatically when code is merged.”
Auto-apply on merge is part of it, but the safe version adds plan-on-PR for review, remote state with locking, policy/security gates, and least-privilege short-lived CI credentials. Just auto-applying with no review or guardrails is risky.
The follow-ups they ask next
Why run plan on the PR rather than only applying on merge?
So reviewers see the exact diff (including destroys/replaces) and approve before any change happens, the infra code-review gate. Applying the saved plan on merge then guarantees what runs is exactly what was reviewed.
How should the CI pipeline authenticate to the cloud?
Short-lived credentials via OIDC federation (the CI provider issues a token the cloud trusts to assume a scoped role), not long-lived access keys stored in CI settings. No standing credential to leak.
What the interviewer is listening for
- Plan-on-PR review + apply-on-merge of the saved plan
- Remote state + locking in CI
- Policy-as-code/scanning gates + OIDC least-privilege creds
What sinks the answer
- Auto-apply with no review
- No locking/state strategy
- Long-lived cloud keys in CI
If you genuinely do not know
Say this instead of freezing. Reasoning out loud from what you do know beats silence every single time, and a good interviewer is listening for exactly that.
“Mirror app CI/CD with infra safeguards: [plan on PR and post the diff for review], [apply on merge, applying the exact reviewed plan, automated and audited], [shared remote state + locking so runs never apply concurrently], and [policy-as-code (OPA/Sentinel) + scanning (tfsec/checkov) gates with short-lived least-privilege OIDC credentials]. Not [auto-apply from a laptop with no review].”
Keep going with iac & automation
Foundation
What is Infrastructure as Code, and why use it instead of clicking through the console?
Foundation
What is the difference between declarative and imperative infrastructure as code?
Junior
What is Terraform state, and why does it exist?
Junior
Explain the Terraform workflow: init, plan, and apply.
Mid
Your team provisions infrastructure with code. Why does idempotency matter, and what breaks without it?
Mid
Why do teams use remote state with locking instead of local state?
Knowing the answer is not the same as recalling it under pressure
Sign in to send the questions you fumble to spaced recall, so they come back right before you would forget them, and learn the concepts behind them with hands-on labs.
Start free