Declarative vs imperative IaC in Terraform - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the number of steps grows when using declarative or imperative infrastructure code.
Which approach takes more repeated actions as the infrastructure grows?
Analyze the time complexity of creating multiple resources using declarative Terraform code.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
This code creates a number of virtual machines equal to instance_count in a declarative way.
Look at what happens repeatedly when this code runs.
- Primary operation: Creating each virtual machine resource via API calls.
- How many times: Once per instance, so as many times as
instance_count.
As you increase the number of instances, the number of API calls grows directly with it.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows evenly as you add more resources.
Time Complexity: O(n)
This means the time to create resources grows directly with the number of resources you want.
[X] Wrong: "Declarative code runs all steps only once no matter how many resources."
[OK] Correct: Each resource still requires its own API call and provisioning, so the total work grows with the number of resources.
Understanding how resource count affects execution helps you explain infrastructure scaling clearly and confidently.
"What if we used a loop in imperative code to create resources one by one? How would the time complexity compare?"
Practice
Solution
Step 1: Understand declarative IaC
Declarative IaC focuses on describing the final desired state of infrastructure without specifying how to achieve it.Step 2: Understand imperative IaC
Imperative IaC lists the exact commands or steps needed to create or change infrastructure.Final Answer:
Declarative IaC describes the desired end state, while imperative IaC specifies exact steps to reach it. -> Option AQuick Check:
Declarative = final state, Imperative = steps [OK]
- Confusing declarative with imperative steps
- Thinking declarative requires manual commands
- Assuming declarative only works for cloud
Solution
Step 1: Identify declarative syntax
Terraform resource blocks declare the desired state, like theresource "aws_instance" "web" { ... }block.Step 2: Compare with imperative styles
Options B, C, and D show commands or logic steps, which are imperative, not declarative.Final Answer:
The Terraform resource block syntax is declarative. -> Option BQuick Check:
Terraform resource block = declarative [OK]
- Choosing imperative command syntax as declarative
- Confusing function calls with resource declarations
- Ignoring the resource block structure
terraform apply if the AWS instance does not exist yet?
resource "aws_instance" "example" {
ami = "ami-abc123"
instance_type = "t2.micro"
}Solution
Step 1: Understand Terraform apply behavior
Terraform compares the current state with the desired state defined in the resource block.Step 2: Predict action if resource missing
If the AWS instance does not exist, Terraform will create it to match the declared state.Final Answer:
Terraform will create a new AWS instance with the specified AMI and instance type. -> Option CQuick Check:
Missing resource = Terraform creates it [OK]
- Thinking Terraform deletes resources by default
- Expecting manual commands during apply
- Assuming syntax error in valid resource block
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?Solution
Step 1: Check syntax of resource attributes
Terraform requires string values to be in quotes. The instance_type value is missing quotes.Step 2: Identify error cause
Missing quotes cause a syntax error, preventing Terraform from parsing the file.Final Answer:
Missing quotes around the instance_type value causes a syntax error. -> Option DQuick Check:
Strings need quotes in Terraform [OK]
- Ignoring missing quotes around strings
- Thinking resource names cause syntax errors
- Expecting commands inside resource blocks
Solution
Step 1: Understand declarative IaC goal
Declarative IaC means describing the final infrastructure state in configuration files.Step 2: Identify Terraform usage
Terraform uses configuration files to declare resources, then applies changes automatically.Final Answer:
Write a Terraform configuration file describing the desired resources and runterraform apply. -> Option AQuick Check:
Declarative IaC = config files + apply [OK]
- Confusing declarative IaC with manual scripting
- Thinking manual cloud console use is declarative
- Using imperative CLI commands instead of config files
