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?"
Practice
version when using a Terraform module from a registry?Solution
Step 1: Understand module versioning purpose
Specifying a version locks the module to a specific release, preventing unexpected changes.Step 2: Compare options
Only locking the version ensures stability; automatic updates or speed improvements are not guaranteed by versioning.Final Answer:
To keep your infrastructure stable by locking the module version -> Option DQuick Check:
Module versioning = Stability [OK]
- Thinking version speeds up execution
- Assuming version auto-updates modules
- Confusing local modules with versioned modules
Solution
Step 1: Recall correct version attribute
The correct attribute to specify module version isversionwith an equals sign.Step 2: Check syntax correctness
module "example" { source = "registry/module" version = "1.2.0" } uses correct attribute and syntax; others use incorrect attribute names or syntax.Final Answer:
module "example" { source = "registry/module" version = "1.2.0" } -> Option AQuick Check:
Correct attribute is version = "x.y.z" [OK]
- Using 'ver' or 'v' instead of 'version'
- Using colon instead of equals sign
- Omitting quotes around version number
module "network" {
source = "terraform-aws-modules/vpc/aws"
version = "3.0.0"
}What happens if a newer version 3.1.0 is released and you run
terraform init without changing the version?Solution
Step 1: Understand version locking behavior
Specifying version "3.0.0" locks the module to that exact version.Step 2: Effect of terraform init
Running terraform init downloads the locked version; it does not upgrade unless version is changed.Final Answer:
Terraform uses version 3.0.0 and does not upgrade automatically -> Option BQuick Check:
Locked version stays fixed until changed [OK]
- Assuming terraform init upgrades modules automatically
- Expecting errors on version mismatch
- Thinking latest version is always used
module "db" {
source = "git::https://github.com/example/db-module.git"
}You want to use a specific version tag
v1.4.2. What is missing?Solution
Step 1: Understand versioning for Git modules
Git modules use URL query parameter?ref=to specify version or tag.Step 2: Identify correct way to specify tag
Adding?ref=v1.4.2to source URL locks module to that tag.Final Answer:
Add?ref=v1.4.2to the source URL -> Option CQuick Check:
Git module version = source URL + ?ref=tag [OK]
- Using version attribute for Git modules
- Using tag attribute which does not exist
- Assuming latest is always used without specifying ref
./modules/app and a remote module from the registry. You want to ensure only the remote module uses versioning. How should you configure the modules?Solution
Step 1: Recall versioning rules for local modules
Local modules do not support versioning; version attribute is ignored or causes error.Step 2: Apply version only to remote modules
Only remote modules from registries or Git support versioning and require version locking.Final Answer:
Specifyversiononly for the remote module; omit for local module -> Option AQuick Check:
Version only for remote modules [OK]
- Adding version to local modules causing errors
- Omitting version for remote modules causing instability
- Confusing local and remote module versioning rules
Ready to prove it?
Pass the test and this pattern moves unknown
