Courseiva
Understand IaC conceptshardMultiple SelectObjective-mapped

Declarative vs Imperative Infrastructure as Code: Key Differences

Which two statements accurately describe the difference between declarative and imperative IaC approaches? (Choose two.)

Quick Answer

The correct answer is that imperative approaches can lead to configuration drift because step-by-step instructions may produce unintended states, while declarative IaC defines the desired end state and lets the tool determine the steps. This distinction is fundamental: with declarative IaC, as in Terraform using HCL, you simply declare "I want an EC2 instance with ami-abc123 and type t2.micro," and Terraform automatically figures out the create, update, or delete actions needed to reach that state. In contrast, imperative IaC, like a series of AWS CLI commands, explicitly dictates each step—run-instances, wait, tag—which risks drift if a step fails or the environment changes between commands. On the HashiCorp Terraform Associate TF-003 exam, this concept tests your understanding of why Terraform’s declarative model prevents drift and ensures idempotency. A common trap is confusing Ansible’s declarative YAML with imperative scripting; remember, if you’re telling the tool *how* to do it step-by-step, it’s imperative. Memory tip: "Declarative = *what*; Imperative = *how*."

⚠ Common exam trap

HashiCorp often tests the misconception that declarative IaC eliminates the need for idempotency, but in reality, declarative tools enforce idempotency through state management and plan generation, making it a key feature rather than an omission.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Declarative focuses on the desired outcome, while imperative specifies step-by-step commands

Declarative IaC, as used in Terraform with HCL, allows you to define the desired end state of infrastructure (e.g., 'I want an AWS EC2 instance with AMI ami-0c55b159cbfafe1f0 and instance type t2.micro'), and the tool automatically determines the necessary steps to achieve that state. In contrast, imperative IaC, such as using AWS CLI commands or Ansible playbooks with explicit 'command' modules, requires you to specify each step (e.g., 'run aws ec2 run-instances, then wait, then tag'). This fundamental difference in approach is a core concept in the TF-003 exam.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Imperative is only used for scripting, not IaC

    Why it's wrong here

    Imperative approaches can be used for IaC, e.g., using shell scripts or PowerShell DSC.

  • Declarative tools are always faster than imperative tools

    Why it's wrong here

    Speed depends on various factors, not the approach alone.

  • Declarative focuses on the desired outcome, while imperative specifies step-by-step commands

    Why this is correct

    This is the fundamental difference.

  • Imperative can lead to configuration drift because steps may cause unintended states

    Why this is correct

    Imperative scripts can produce different results if run from different starting states.

  • Declarative eliminates the need for idempotency

    Why it's wrong here

    Declarative tools are inherently idempotent; they don't eliminate the concept.

About these practice questions

One of 428 original TF-004 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on TF-004

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A team is evaluating Terraform and Ansible for infrastructure provisioning. They note that Terraform describes the desired end state, while Ansible defines steps to reach that state. This difference is best described as:

easy
  • A.Declarative vs imperative
  • B.Client-server vs agentless
  • C.Immutable vs mutable
  • D.Push vs pull

Why A: The difference between declarative and imperative paradigms is key: Terraform is declarative (you specify the desired end state, and Terraform figures out how to achieve it), while Ansible is imperative (you specify each step to reach the state). Option B (client-server vs agentless) refers to architecture, option C (immutable vs mutable) refers to update strategies, and option D (push vs pull) refers to deployment methods. Therefore, option A is correct.

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This TF-004 practice question is part of Courseiva's free HashiCorp certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the TF-004 exam.