Root module concept in Terraform - Time & Space Complexity
We want to understand how the work done by Terraform grows when using the root module.
Specifically, how does Terraform handle resources defined in the root module as their number increases?
Analyze the time complexity of applying a root module with multiple resources.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
variable "instance_count" {
type = number
default = 3
}
This code creates multiple virtual machines based on the count variable in the root module.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Creating each AWS instance resource via API calls.
- How many times: Once per instance, equal to the count variable.
As the number of instances increases, the number of API calls grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of resources defined.
Time Complexity: O(n)
This means the time to apply the root module grows linearly with the number of resources.
[X] Wrong: "Adding more resources in the root module does not affect apply time much."
[OK] Correct: Each resource requires separate API calls and provisioning, so more resources mean more work.
Understanding how resource count affects Terraform apply time helps you design efficient infrastructure and explain your choices clearly.
"What if we split resources into multiple child modules instead of the root module? How would the time complexity change?"