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?"
Practice
source argument specify in a Terraform module block?Solution
Step 1: Understand the role of
Thesourcein a modulesourceargument tells Terraform where to get the module code from, such as a local folder, registry, or Git repo.Step 2: Identify what
It does not specify Terraform version, cloud provider, or outputs; those are separate configurations.sourcedoes not specifyFinal Answer:
The location where Terraform finds the module code -> Option AQuick Check:
Modulesource= module location [OK]
- Confusing source with Terraform version
- Thinking source defines outputs
- Assuming source sets cloud provider
modules/network?Solution
Step 1: Identify local module source syntax
Local modules use a relative or absolute path as thesource. Here, "modules/network" is a relative path.Step 2: Check other options for correctness
module "net" { source = "registry/modules/network" } uses registry syntax incorrectly; module "net" { source = "git::https://example.com/network.git" } is for Git sources; module "net" { source = "network" } lacks the folder path.Final Answer:
module "net" { source = "modules/network" } -> Option AQuick Check:
Local module source = folder path [OK]
- Using registry or git syntax for local modules
- Omitting folder path in source
- Confusing source with module name
module "web" {
source = "git::https://github.com/example/terraform-web.git"
version = "v1.0.0"
}What will Terraform do when you run
terraform init?Solution
Step 1: Understand Git source syntax in Terraform
Thesourcestarting with "git::" tells Terraform to clone a Git repository from the URL.Step 2: Interpret the version argument with Git sources
Theversionhere refers to a Git tag or branch to checkout, so Terraform will checkout "v1.0.0" after cloning.Final Answer:
Clone the Git repository at the given URL and checkout version v1.0.0 -> Option CQuick Check:
Git source + version = clone and checkout tag [OK]
- Thinking version is Terraform Registry version
- Assuming source is local folder
- Believing version is unsupported for Git
module "db" {
source = "git::https://github.com/example/terraform-db.git"
version = "1.0.0"
}When running
terraform init, you get an error. What is the likely cause?Solution
Step 1: Check Git tag naming conventions
Git tags often start with 'v' (e.g., 'v1.0.0'). If the tag is named with 'v', specifying '1.0.0' causes a mismatch.Step 2: Verify other options for errors
Git URLs are valid sources; local path is not required; module name 'db' is valid.Final Answer:
The version should be prefixed with 'v' like 'v1.0.0' to match Git tags -> Option BQuick Check:
Git version tag must match exact Git tag name [OK]
- Omitting 'v' prefix in version for Git tags
- Thinking Git URLs are invalid module sources
- Assuming module names have strict rules
hashicorp/aws with version 4.0.0. Which is the correct module block to use?Solution
Step 1: Identify Terraform Registry module source syntax
Modules from the Terraform Registry use the format "namespace/module" as the source, e.g., "hashicorp/aws".Step 2: Check other options for correctness
module "aws_module" { source = "git::https://github.com/hashicorp/aws.git" version = "4.0.0" } uses Git URL which is incorrect for Registry modules; module "aws_module" { source = "./hashicorp/aws" version = "4.0.0" } uses local path; module "aws_module" { source = "registry/hashicorp/aws" version = "4.0.0" } uses invalid prefix "registry/".Final Answer:
module "aws_module" { source = "hashicorp/aws" version = "4.0.0" } -> Option DQuick Check:
Registry modules use "namespace/module" source format [OK]
- Using Git URL for Registry modules
- Adding extra prefixes like 'registry/'
- Using local paths for Registry modules
Ready to prove it?
Pass the test and this pattern moves unknown
