Creating a child module in Terraform - Performance & Efficiency
When using Terraform, creating a child module means calling a separate set of instructions multiple times.
We want to know how the time to apply changes grows as we add more child modules.
Analyze the time complexity of the following operation sequence.
module "child" {
source = "./child_module"
count = var.instance_count
name = "example-${count.index}"
}
variable "instance_count" {
type = number
default = 3
}
This code creates multiple instances of a child module based on a count variable.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Each child module creates its own resources, triggering API calls to provision them.
- How many times: The number of times equals the count variable, as each module instance runs separately.
As the count increases, the number of API calls grows proportionally because each module instance provisions resources independently.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 times the resources in one module |
| 100 | 100 times the resources in one module |
| 1000 | 1000 times the resources in one module |
Pattern observation: The work grows directly with the number of module instances.
Time Complexity: O(n)
This means the time to apply grows linearly as you add more child module instances.
[X] Wrong: "Adding more child modules won't increase the time much because they run together."
[OK] Correct: Each child module creates resources separately, so more modules mean more total work and API calls.
Understanding how module count affects provisioning time helps you design scalable infrastructure and explain your choices clearly.
"What if the child module itself creates multiple resources internally? How would that affect the time complexity?"