Module versioning in Terraform - Time & Space Complexity
When using Terraform modules, it's important to understand how the number of module versions affects the time it takes to plan and apply changes.
We want to know how the process grows as we add more module versions.
Analyze the time complexity of using multiple versions of a Terraform module.
module "example" {
source = "terraform-aws-modules/vpc/aws"
version = "2.77.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
}
module "example_old" {
source = "terraform-aws-modules/vpc/aws"
version = "2.50.0"
name = "my-vpc-old"
cidr = "10.1.0.0/16"
}
This code uses two different versions of the same module to create separate VPCs.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Downloading and initializing each module version separately.
- How many times: Once per unique module version used in the configuration.
Each new module version adds a separate initialization step, increasing the total time linearly.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 1 | 1 module download and init |
| 5 | 5 module downloads and inits |
| 10 | 10 module downloads and inits |
Pattern observation: The time grows directly with the number of unique module versions used.
Time Complexity: O(n)
This means the time to initialize modules grows linearly with the number of different module versions.
[X] Wrong: "Using more module versions won't affect initialization time much because modules are cached."
[OK] Correct: Each unique version requires separate download and setup, so more versions mean more work.
Understanding how module versioning affects execution time helps you design efficient Terraform configurations and shows you can think about scaling infrastructure code.
"What if we used the same module version multiple times instead of different versions? How would the time complexity change?"