Immutable infrastructure concept in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to update infrastructure changes when using immutable infrastructure.
Specifically, how does creating new resources instead of changing existing ones affect the work done?
Analyze the time complexity of replacing servers by creating new ones instead of updating in place.
resource "aws_instance" "web" {
count = var.server_count
ami = var.ami_id
instance_type = var.instance_type
lifecycle {
create_before_destroy = true
}
}
This code creates a number of servers and replaces them by creating new ones before destroying old ones.
Look at what happens repeatedly when the number of servers changes.
- Primary operation: Creating and destroying each server instance.
- How many times: Once per server, so as many times as the server count.
As you add more servers, the number of create and destroy actions grows directly with that number.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 create + 10 destroy operations |
| 100 | About 100 create + 100 destroy operations |
| 1000 | About 1000 create + 1000 destroy operations |
Pattern observation: The work grows directly with the number of servers you manage.
Time Complexity: O(n)
This means the time to update grows in a straight line with the number of servers you replace.
[X] Wrong: "Replacing servers one by one is faster because only one server changes at a time."
[OK] Correct: Even if done one by one, each server still requires a full create and destroy operation, so total work grows with the number of servers.
Understanding how immutable infrastructure scales helps you explain deployment strategies clearly and shows you grasp how cloud resources behave as they grow.
"What if we changed from replacing all servers to updating them in place? How would the time complexity change?"
Practice
immutable infrastructure mean in Terraform?Solution
Step 1: Understand the definition of immutable infrastructure
Immutable infrastructure means you do not change existing resources but replace them entirely when updates are needed.Step 2: Compare options with this definition
Only Replacing resources instead of modifying them in place describes replacing resources instead of modifying them, which matches the concept.Final Answer:
Replacing resources instead of modifying them in place -> Option AQuick Check:
Immutable infrastructure = Replace, not modify [OK]
- Thinking mutable means immutable
- Confusing manual updates with Terraform-managed changes
- Assuming resources are changed in place
Solution
Step 1: Identify lifecycle arguments related to resource replacement
Terraform lifecycle has arguments like create_before_destroy, prevent_destroy, ignore_changes, and replace_triggered_by.Step 2: Match argument to immutable infrastructure behavior
Create_before_destroy ensures new resource is created before old one is destroyed, supporting immutable infrastructure.Final Answer:
create_before_destroy -> Option CQuick Check:
Lifecycle create_before_destroy = create new before delete old [OK]
- Confusing prevent_destroy with create_before_destroy
- Using ignore_changes which skips updates but doesn't replace
- Misunderstanding replace_triggered_by purpose
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
lifecycle {
create_before_destroy = true
}
}
What happens when you change instance_type from t2.micro to t2.small?Solution
Step 1: Understand create_before_destroy lifecycle effect
This setting tells Terraform to create the new resource before destroying the old one to avoid downtime.Step 2: Apply this to instance_type change
Changing instance_type requires replacement, so Terraform creates new instance first, then destroys old.Final Answer:
Terraform creates a new instance first, then destroys the old one -> Option AQuick Check:
create_before_destroy means create new before destroy old [OK]
- Assuming in-place update happens
- Thinking old resource is destroyed before new is ready
- Believing lifecycle ignores changes
lifecycle {
create_before_destroy = false
}
But you want to implement immutable infrastructure with zero downtime. What is the problem?Solution
Step 1: Analyze create_before_destroy false effect
When false, Terraform destroys old resource before creating new one, causing downtime.Step 2: Match with immutable infrastructure goal
Immutable infrastructure aims for zero downtime by creating new resource first, so false breaks this goal.Final Answer:
Resources will be destroyed before new ones are created, causing downtime -> Option DQuick Check:
False create_before_destroy = destroy first, downtime risk [OK]
- Thinking false means no replacement
- Assuming lifecycle block is ignored
- Believing duplicates are created without destroy
Solution
Step 1: Identify lifecycle settings that enable immutable infrastructure
Create_before_destroy = true ensures new resource is ready before old is removed, supporting immutable infrastructure.Step 2: Consider resource management best practices
Avoid manual changes outside Terraform to keep infrastructure consistent and manageable.Final Answer:
Use create_before_destroy = true and avoid manual changes outside Terraform -> Option BQuick Check:
Immutable infrastructure = create_before_destroy + Terraform-only changes [OK]
- Using prevent_destroy which blocks replacement
- Ignoring changes disables updates, not replacement
- Manual changes cause drift and errors
