Declarative vs Imperative IaC
You'll learn: The two fundamental approaches to automating infrastructure provisioning—describing what you want versus how to build it.
Core Concept
Infrastructure as Code comes in two flavors that differ in how you express your intentions:
Declarative IaC lets you define your desired end state, and the tool figures out how to get there. You describe what infrastructure should exist—like "I need 3 web servers with 4GB RAM each"—without specifying the exact steps to create them. Tools like Terraform, CloudFormation, and Azure Resource Manager use this approach. If you later change your declaration to "5 web servers," the tool intelligently adds 2 more without touching the existing 3.
Imperative IaC requires you to write explicit, step-by-step instructions—the how. You'd script commands like "create server 1," "create server 2," "create server 3," "attach storage to server 1," and so on. Traditional shell scripts, Ansible playbooks (though Ansible can be declarative too), and early automation tools follow this pattern.
Real-World Analogy
Think of ordering at a restaurant:
- Declarative: "I want a Caesar salad"—the chef determines all steps
- Imperative: "Chop romaine, add dressing, grate parmesan, add croutons"—you control every action
Key Differences
Declarative IaC handles drift detection and idempotency automatically—run it 10 times, get the same result. Imperative code requires you to manually check "does this resource already exist?" before each action.
Most modern cloud infrastructure tools favor declarative approaches because they're easier to maintain, self-documenting, and prevent configuration drift.
Key Takeaway: Declarative IaC describes your desired infrastructure state (what), while imperative IaC specifies exact provisioning steps (how)—declarative is generally preferred for cloud infrastructure management.