What is the difference between declarative and imperative infrastructure as code?
What they are really testing: Whether you understand describing desired state versus scripting steps, and why declarative tools converge to a target state and are idempotent.
A real interview question
What is the difference between declarative and imperative infrastructure as code?
What most people say
drag me
“Declarative is what you want and imperative is how to do it.”
The slogan is right but undeveloped. The interviewer wants the consequence: declarative tools compute the diff and are idempotent/converge to desired state, which is why they dominate provisioning. The why is missing.
The follow-ups they ask next
Why does declarative IaC make idempotency easier?
You declare the target state and the tool computes the diff, so re-running converges to that state (no-op if already there). With imperative scripts you must hand-write all the exists-already checks to avoid errors or duplicates.
Is Terraform declarative or imperative?
Declarative: you describe the desired resources and Terraform plans the create/update/delete actions to reach that state. You do not script the steps yourself.
What the interviewer is listening for
- Desired state vs scripted steps
- Knows declarative is idempotent/converges
- Knows why it dominates provisioning
What sinks the answer
- Slogan with no consequence
- Thinks Terraform is imperative
- No notion of convergence/idempotency
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.
“Declarative [describes the desired end state, the tool computes what to create/change/delete, Terraform/CloudFormation]. Imperative [scripts the explicit steps]. Declarative wins for infra because [it is idempotent and converges, re-running is a no-op if matched and reconciles drift], whereas imperative [makes you hand-write exists-already logic]. Imperative suits [one-off procedural tasks].”
Keep going with iac & automation
Foundation
What is Infrastructure as Code, and why use it instead of clicking through the console?
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?
Mid
What are Terraform modules, and how do they help?
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