Resource block syntax in Terraform - Time & Space Complexity
We want to understand how the time to create resources grows when we add more resource blocks in Terraform.
How does the number of resources affect the total work Terraform does?
Analyze the time complexity of creating multiple similar resources using resource blocks.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
This code creates a number of AWS instances based on the variable instance_count.
Each resource block creates one instance.
- Primary operation: API call to create one AWS instance.
- How many times: Equal to the value of
instance_count.
When you increase the number of instances, the number of API calls grows the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of instances.
Time Complexity: O(n)
This means the time to create resources grows linearly with the number of resource blocks.
[X] Wrong: "Adding more resource blocks will not increase the total time much because they run in parallel."
[OK] Correct: While some operations may run in parallel, each resource still requires its own API call and provisioning time, so total work grows with the number of resources.
Understanding how resource count affects deployment time helps you design efficient infrastructure and explain your choices clearly in discussions.
"What if we used modules to create resources instead of resource blocks? How would the time complexity change?"