Declarative vs Imperative approaches

5 minutes 5 Questions

Infrastructure as Code (IaC) can be implemented using two fundamental approaches: Declarative and Imperative. Understanding these paradigms is essential for the Terraform Associate certification. **Declarative Approach:** Terraform uses a declarative approach where you define the desired end state of your infrastructure. You specify WHAT you want, not HOW to achieve it. Terraform's engine determines the necessary steps to reach that state. For example, you declare that you need 5 EC2 instances with specific configurations, and Terraform figures out whether to create, modify, or delete resources to match your specification. Key characteristics include: - Focus on end-state configuration - Idempotent operations (running the same code multiple times produces the same result) - Terraform tracks state and calculates differences - Easier to understand and maintain over time - Self-documenting infrastructure **Imperative Approach:** Imperative IaC requires you to specify the exact sequence of commands and steps to achieve a desired outcome. You define HOW to accomplish tasks step-by-step. Tools like Ansible scripts or custom shell scripts often follow this pattern. Key characteristics include: - Sequential command execution - Explicit control over each operation - More flexibility for complex procedural logic - Requires tracking current state manually - Can be harder to maintain as infrastructure grows **Why Terraform Chooses Declarative:** Terraform's declarative model simplifies infrastructure management by: 1. Automatically handling dependency ordering 2. Managing state files to track resource mappings 3. Providing plan previews before applying changes 4. Supporting idempotency for consistent deployments 5. Enabling version control of infrastructure definitions The HashiCorp Configuration Language (HCL) embodies this declarative philosophy, making Terraform configurations readable and maintainable while abstracting complex provisioning logic from users.

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.
Test mode:
More Declarative vs Imperative approaches questions
28 questions (total)