Bird
Raised Fist0
Terraformcloud~10 mins

Declarative vs imperative IaC in Terraform - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Declarative vs imperative IaC
Start
↓
Choose IaC Style
↓
Declarative
↓
Describe Desired State
↓
Tool Figures Out Changes
↓
Apply Changes Automatically
↓
Infrastructure Matches State
↓
Imperative
↓
Write Step-by-Step Commands
↓
Run Commands in Order
↓
Infrastructure Changes as Told
↓
Result Depends on Commands
This flow shows the choice between declarative and imperative IaC. Declarative means you say what you want, and the tool figures out how to do it. Imperative means you tell the tool exactly how to do each step.
Execution Sample
Terraform
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

# Imperative: aws ec2 run-instances --image-id ami-123456 --instance-type t2.micro
This shows a declarative Terraform resource block describing an AWS instance, and an imperative AWS CLI command to create the same instance.
Process Table
StepActionDeclarative BehaviorImperative BehaviorResult
1Define desired state or commandWrite resource block with desired instanceWrite CLI command to launch instanceBoth specify instance details
2Run toolTerraform plans changes to match desired stateCLI runs command immediatelyTerraform plans; CLI acts now
3Apply changesTerraform creates instance if missingInstance created by CLI commandInstance launched in both
4Update or changeChange resource block, Terraform updates instanceRun new CLI commands step-by-stepDeclarative auto-updates; imperative manual
5DeleteRemove resource block, Terraform destroys instanceRun CLI terminate commandInstance removed in both
6ExitInfrastructure matches declared stateInfrastructure matches commands runEnd state depends on approach
💡 Execution stops when infrastructure matches desired state (declarative) or after commands finish (imperative).
Status Tracker
VariableStartAfter Step 3After Step 4Final
Infrastructure StateNo instanceInstance createdInstance updated or unchangedMatches desired or commanded state
Terraform PlanEmptyPlan shows create instancePlan shows update or no changePlan empty after apply
CLI Commands RunNoneRun launch instanceRun update or terminate commandsCommands complete
Key Moments - 3 Insights
Why does Terraform plan changes before applying?
Terraform compares current state to desired state and plans only needed changes (see execution_table step 2). This avoids unnecessary actions.
In imperative IaC, what happens if a command is missed?
The infrastructure may not reach the intended state because commands must be run in order (see execution_table step 4). Manual steps can cause errors.
Can declarative IaC handle updates automatically?
Yes, by changing the resource block and reapplying, Terraform updates resources as needed (see execution_table step 4). Imperative requires manual commands.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Terraform create the instance?
AStep 5
BStep 1
CStep 3
DStep 6
💡 Hint
Check the 'Declarative Behavior' column in execution_table at Step 3.
According to variable_tracker, what is the infrastructure state after Step 3?
AInstance created
BInstance updated
CNo instance
DInstance terminated
💡 Hint
Look at 'Infrastructure State' row after Step 3 in variable_tracker.
If you skip running CLI commands in imperative IaC, what happens?
ATerraform fixes the state automatically
BInfrastructure may not match desired state
CCommands run themselves later
DInfrastructure is deleted
💡 Hint
Refer to key_moments about missing commands in imperative IaC.
Concept Snapshot
Declarative IaC: You describe what you want; the tool figures out how to do it.
Imperative IaC: You write exact commands to do each step.
Terraform is declarative; AWS CLI is imperative.
Declarative tools plan changes before applying.
Imperative requires manual command execution.
Declarative reduces errors by managing state automatically.
Full Transcript
This lesson compares declarative and imperative Infrastructure as Code (IaC). Declarative IaC means you write the desired end state, and the tool figures out the steps to get there. Imperative IaC means you write exact commands to run step-by-step. Terraform is an example of declarative IaC where you write resource blocks describing your infrastructure. The tool plans and applies changes automatically. Imperative IaC, like using AWS CLI commands, requires you to run each command manually in order. The execution table shows how Terraform plans and applies changes, while imperative commands act immediately. Variable tracking shows how infrastructure state changes after each step. Key moments clarify why Terraform plans first, the risk of missing commands in imperative IaC, and how declarative IaC handles updates automatically. The quiz tests understanding of when instances are created, infrastructure state changes, and consequences of skipping commands. The snapshot summarizes the key differences and behaviors of declarative vs imperative IaC.

Practice

(1/5)
1. What is the main difference between declarative and imperative Infrastructure as Code (IaC)?
easy
A. Declarative IaC describes the desired end state, while imperative IaC specifies exact steps to reach it.
B. Declarative IaC requires manual commands, while imperative IaC is fully automatic.
C. Declarative IaC is only for cloud, imperative IaC is for on-premises.
D. Declarative IaC uses scripts, imperative IaC uses configuration files.

Solution

  1. Step 1: Understand declarative IaC

    Declarative IaC focuses on describing the final desired state of infrastructure without specifying how to achieve it.
  2. Step 2: Understand imperative IaC

    Imperative IaC lists the exact commands or steps needed to create or change infrastructure.
  3. Final Answer:

    Declarative IaC describes the desired end state, while imperative IaC specifies exact steps to reach it. -> Option A
  4. Quick Check:

    Declarative = final state, Imperative = steps [OK]
Hint: Declarative = what you want; imperative = how to do it [OK]
Common Mistakes:
  • Confusing declarative with imperative steps
  • Thinking declarative requires manual commands
  • Assuming declarative only works for cloud
2. Which of the following Terraform code snippets correctly shows a declarative resource definition?
easy
A. create_instance("ami-123", "t2.micro")
B. resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" }
C. if not instance_exists() then create_instance()
D. run_command("aws ec2 run-instances --image-id ami-123")

Solution

  1. Step 1: Identify declarative syntax

    Terraform resource blocks declare the desired state, like the resource "aws_instance" "web" { ... } block.
  2. Step 2: Compare with imperative styles

    Options B, C, and D show commands or logic steps, which are imperative, not declarative.
  3. Final Answer:

    The Terraform resource block syntax is declarative. -> Option B
  4. Quick Check:

    Terraform resource block = declarative [OK]
Hint: Terraform resource blocks are declarative descriptions [OK]
Common Mistakes:
  • Choosing imperative command syntax as declarative
  • Confusing function calls with resource declarations
  • Ignoring the resource block structure
3. Given this Terraform snippet, what will Terraform do when you run terraform apply if the AWS instance does not exist yet?
resource "aws_instance" "example" {
  ami           = "ami-abc123"
  instance_type = "t2.micro"
}
medium
A. Terraform will prompt for manual commands to create the instance.
B. Terraform will delete any existing instances and do nothing else.
C. Terraform will create a new AWS instance with the specified AMI and instance type.
D. Terraform will throw a syntax error because the resource block is incomplete.

Solution

  1. Step 1: Understand Terraform apply behavior

    Terraform compares the current state with the desired state defined in the resource block.
  2. Step 2: Predict action if resource missing

    If the AWS instance does not exist, Terraform will create it to match the declared state.
  3. Final Answer:

    Terraform will create a new AWS instance with the specified AMI and instance type. -> Option C
  4. Quick Check:

    Missing resource = Terraform creates it [OK]
Hint: Terraform creates missing resources to match declared state [OK]
Common Mistakes:
  • Thinking Terraform deletes resources by default
  • Expecting manual commands during apply
  • Assuming syntax error in valid resource block
4. You wrote this Terraform code but terraform apply fails with an error:
resource "aws_instance" "web" {
  ami = "ami-123"
  instance_type = t2.micro
}
What is the likely cause of the error?
medium
A. Terraform requires an explicit create command inside the resource block.
B. The resource name "web" is invalid and must be changed.
C. The AMI ID "ami-123" is not allowed in Terraform.
D. Missing quotes around the instance_type value causes a syntax error.

Solution

  1. Step 1: Check syntax of resource attributes

    Terraform requires string values to be in quotes. The instance_type value is missing quotes.
  2. Step 2: Identify error cause

    Missing quotes cause a syntax error, preventing Terraform from parsing the file.
  3. Final Answer:

    Missing quotes around the instance_type value causes a syntax error. -> Option D
  4. Quick Check:

    Strings need quotes in Terraform [OK]
Hint: Always quote string values in Terraform resource blocks [OK]
Common Mistakes:
  • Ignoring missing quotes around strings
  • Thinking resource names cause syntax errors
  • Expecting commands inside resource blocks
5. You want to create a cloud infrastructure using Terraform declarative IaC. Which approach best fits this goal?
hard
A. Write a Terraform configuration file describing the desired resources and run terraform apply.
B. Write a script with commands to create each resource step-by-step manually.
C. Manually create resources in the cloud console, then import them into Terraform.
D. Use a shell script to run cloud CLI commands in order.

Solution

  1. Step 1: Understand declarative IaC goal

    Declarative IaC means describing the final infrastructure state in configuration files.
  2. Step 2: Identify Terraform usage

    Terraform uses configuration files to declare resources, then applies changes automatically.
  3. Final Answer:

    Write a Terraform configuration file describing the desired resources and run terraform apply. -> Option A
  4. Quick Check:

    Declarative IaC = config files + apply [OK]
Hint: Use Terraform config files, not manual scripts, for declarative IaC [OK]
Common Mistakes:
  • Confusing declarative IaC with manual scripting
  • Thinking manual cloud console use is declarative
  • Using imperative CLI commands instead of config files