0
0
Terraformcloud~5 mins

Module sources (local, registry, git) in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Module sources (local, registry, git)
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As you add more modules, Terraform fetches each one separately.

Input Size (n)Approx. Api Calls/Operations
1010 fetch operations
100100 fetch operations
10001000 fetch operations

Pattern observation: The number of fetches grows directly with the number of modules.

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch modules grows linearly as you add more modules.

Common Mistake

[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.

Interview Connect

Understanding how module fetching scales helps you design efficient Terraform projects and troubleshoot slow runs.

Self-Check

"What if multiple modules share the same git repository source? How would the time complexity change?"