Declarative vs Imperative Approaches in Infrastructure as Code
Why This Concept Is Important
Understanding the difference between declarative and imperative approaches is fundamental to mastering Terraform and Infrastructure as Code (IaC). This distinction affects how you design, write, and maintain your infrastructure configurations. For the Terraform Associate exam, this topic frequently appears in questions about Terraform's core principles and advantages.
What Are Declarative and Imperative Approaches?
Declarative Approach:
In a declarative approach, you describe the desired end state of your infrastructure. You specify what you want, not how to achieve it. The tool determines the steps needed to reach that state.
Example: "I want 3 web servers running in AWS us-east-1 region."
Terraform uses the declarative approach. You define resources in configuration files, and Terraform figures out the order of operations, dependencies, and changes required to match your desired state.
Imperative Approach:
In an imperative approach, you specify the exact steps and commands to execute in a specific order. You describe how to achieve the desired outcome.
Example: "First, create a VPC. Then, create a subnet. Next, launch an EC2 instance in that subnet."
Traditional scripting languages like Bash or PowerShell typically follow an imperative approach.
How Terraform's Declarative Model Works
1. State Management: Terraform maintains a state file that tracks the current state of your infrastructure.
2. Plan Phase: When you run terraform plan, Terraform compares your configuration (desired state) with the current state and determines what changes are needed.
3. Apply Phase: Terraform executes only the necessary changes to reach the desired state, handling dependencies and ordering automatically.
4. Idempotency: Running the same configuration multiple times produces the same result. If infrastructure already matches the desired state, no changes occur.
Key Differences Summary
Declarative:
- Defines the end state
- Tool determines the steps
- Easier to understand and maintain
- Naturally idempotent
- Terraform, CloudFormation, Kubernetes manifests
Imperative:
- Defines step-by-step instructions
- User controls the process
- More complex to maintain at scale
- Requires manual idempotency handling
- Bash scripts, AWS CLI commands, Ansible ad-hoc commands
Exam Tips: Answering Questions on Declarative vs Imperative Approaches
1. Remember the key phrase: Declarative = "What" you want, Imperative = "How" to do it.
2. Terraform is declarative: If a question asks about Terraform's approach or methodology, the answer involves declarative concepts.
3. Look for keywords: Questions mentioning "desired state," "end state," or "configuration defines resources" point to declarative answers.
4. Idempotency connection: Declarative approaches are naturally idempotent. Questions linking idempotency often relate to declarative benefits.
5. Benefits questions: When asked about advantages of Terraform's approach, think about reduced complexity, automatic dependency resolution, and state comparison.
6. Comparison questions: If comparing Terraform to shell scripts or procedural code, emphasize that Terraform abstracts the execution details.
7. Watch for trap answers: Options suggesting you must specify the exact order of resource creation are describing imperative approaches, not Terraform's declarative model.
8. State file relevance: The state file enables Terraform's declarative model by tracking current infrastructure status for comparison with desired configuration.