0
0
Terraformcloud~10 mins

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

Choose your learning style9 modes available
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.