What is declarative vs imperative infrastructure and where does Terraform fit?
Learn declarative vs imperative infrastructure, why it matters for idempotency and drift, and where Terraform fits as a declarative IaC tool.
Expected Interview Answer
Declarative infrastructure means you describe the desired end state and let the tool figure out how to reach it, whereas imperative means you specify the exact ordered steps to perform. Terraform is fundamentally declarative: you declare what resources should exist, and it computes the actions needed to converge on that state.
In an imperative approach you write commands like 'create this VM, then attach this disk, then open this port' in a specific order, and you own the logic for handling existing state. In a declarative approach you state the goal and the engine reconciles current reality with your definition, adding, changing, or destroying only what is necessary. Terraform reads your configuration, compares it against its state file, builds a dependency graph, and produces an execution plan — so you focus on the target, not the procedure. This makes runs idempotent: applying the same config repeatedly converges to the same result.
- You describe the goal, not the procedure
- Idempotent runs converge to the same end state
- Terraform builds a dependency graph automatically
- Drift is detected by comparing state to reality
- Less error-prone than hand-ordered imperative scripts
AI Mentor Explanation
Declarative infrastructure is like telling the groundsman 'the pitch must be dry and rolled by match day' and trusting him to choose the steps, while imperative is dictating 'first mow, then water, then roll twice' in order. Terraform is declarative: you state the desired pitch condition and it works out which tasks are needed to get there, skipping any already done.
Step-by-Step Explanation
Step 1
Declare desired state
Write HCL describing the resources you want to exist, not the commands to create them.
Step 2
Terraform reads state
Terraform loads its state file to learn what infrastructure currently exists.
Step 3
Build a graph
It constructs a dependency graph so resources are handled in the correct order automatically.
Step 4
Compute the plan
Terraform diffs desired versus current state and generates a plan of adds, changes, and deletes.
Step 5
Converge idempotently
Apply executes only the needed actions; re-running with no changes results in a no-op.
What Interviewer Expects
- Clear definition of declarative versus imperative
- Recognition that Terraform is declarative
- Understanding of idempotency and convergence to end state
- Awareness of the dependency graph and execution plan
- How state enables drift detection
Common Mistakes
- Calling Terraform imperative because you run commands like apply
- Confusing idempotency with simply re-running a script
- Ignoring that Terraform reconciles against its state file
- Assuming you must order resources manually in the config
Best Answer (HR Friendly)
“Declarative means you describe the result you want and the tool works out how to get there, while imperative means you list the exact steps yourself. Terraform is declarative: you say what your infrastructure should look like, and it figures out the changes needed.”
Code Example
# You declare WHAT should exist, not the steps.
resource "aws_s3_bucket" "assets" {
bucket = "skillveris-assets"
}
resource "aws_s3_bucket_versioning" "assets" {
bucket = aws_s3_bucket.assets.id
versioning_configuration {
status = "Enabled"
}
}
# Terraform figures out the order via the dependency
# between the bucket and its versioning resource,
# and re-running with no edits is a no-op (idempotent).Follow-up Questions
- What does idempotency mean in the context of Terraform?
- How does Terraform build its dependency graph?
- How does declarative IaC help detect configuration drift?
- Can Terraform ever behave imperatively, for example with provisioners?
- Why is the execution plan important before applying?
MCQ Practice
1. Which statement describes a declarative approach?
Declarative means declaring the target state and letting the engine determine the actions to reach it.
2. Where does Terraform fit on this spectrum?
Terraform is fundamentally declarative: you declare resources and it computes and applies the needed changes.
3. What makes a Terraform apply idempotent?
Because Terraform converges to the declared state, applying an unchanged config makes no changes.
Flash Cards
Declarative infrastructure? — You describe the desired end state and the tool determines how to reach it.
Imperative infrastructure? — You specify the exact ordered steps to perform to change infrastructure.
Where does Terraform fit? — It is fundamentally declarative, reconciling declared state against its state file.
What is idempotency here? — Applying the same config repeatedly converges to the same result; no changes means a no-op.