Module structure and conventions in Terraform - Time & Space Complexity
We want to understand how the time to deploy infrastructure changes when using Terraform modules.
Specifically, how does adding more modules affect the number of operations Terraform performs?
Analyze the time complexity of this Terraform module usage example.
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
}
module "servers" {
source = "./modules/servers"
count = 3
network_id = module.network.id
}
This code uses two modules: one for network setup and one for servers, with the servers module repeated 3 times.
Look at what Terraform does repeatedly when modules are used.
- Primary operation: Terraform initializes and applies each module's resources.
- How many times: Once per module instance; here, 1 for network, 3 for servers.
As you add more module instances, Terraform runs more resource operations.
| Input Size (number of module instances) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 times the operations of one module |
| 100 | About 100 times the operations of one module |
| 1000 | About 1000 times the operations of one module |
Pattern observation: The total operations grow directly with the number of module instances.
Time Complexity: O(n)
This means the time to deploy grows in a straight line as you add more module instances.
[X] Wrong: "Adding more modules won't affect deployment time much because modules are just code."
[OK] Correct: Each module creates real resources, so more modules mean more work for Terraform and cloud APIs.
Understanding how module count affects deployment time helps you design efficient infrastructure and explain your choices clearly.
"What if we changed the modules to use dynamic blocks inside a single module instead of multiple module instances? How would the time complexity change?"