Module sources (local, registry, git) in Terraform - Time & Space Complexity
When Terraform uses modules from different sources, it needs to fetch and prepare them before use.
We want to understand how the time to get these modules grows as we add more modules or change their sources.
Analyze the time complexity of fetching modules from various sources.
module "local_mod" {
source = "./modules/local"
}
module "registry_mod" {
source = "terraform-aws-modules/vpc/aws"
}
module "git_mod" {
source = "git::https://github.com/example/repo.git"
}
This sequence shows modules sourced locally, from the Terraform registry, and from a Git repository.
Each module source requires fetching and preparing the module code.
- Primary operation: Download or copy module files from source locations.
- How many times: Once per module used in the configuration.
As you add more modules, Terraform fetches each one separately.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 fetch operations |
| 100 | 100 fetch operations |
| 1000 | 1000 fetch operations |
Pattern observation: The number of fetches grows directly with the number of modules.
Time Complexity: O(n)
This means the time to fetch modules grows linearly as you add more modules.
[X] Wrong: "Fetching modules from local, registry, or git sources all take the same time regardless of number."
[OK] Correct: Each module requires a separate fetch or copy operation, so more modules mean more time.
Understanding how module fetching scales helps you design efficient Terraform projects and troubleshoot slow runs.
"What if multiple modules share the same git repository source? How would the time complexity change?"