Module structure and conventions in Terraform - Time & Space Complexity
We want to understand how the time to deploy infrastructure changes when using Terraform modules.
Specifically, how does adding more modules affect the number of operations Terraform performs?
Analyze the time complexity of this Terraform module usage example.
module "network" {
source = "./modules/network"
cidr_block = "10.0.0.0/16"
}
module "servers" {
source = "./modules/servers"
count = 3
network_id = module.network.id
}
This code uses two modules: one for network setup and one for servers, with the servers module repeated 3 times.
Look at what Terraform does repeatedly when modules are used.
- Primary operation: Terraform initializes and applies each module's resources.
- How many times: Once per module instance; here, 1 for network, 3 for servers.
As you add more module instances, Terraform runs more resource operations.
| Input Size (number of module instances) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 times the operations of one module |
| 100 | About 100 times the operations of one module |
| 1000 | About 1000 times the operations of one module |
Pattern observation: The total operations grow directly with the number of module instances.
Time Complexity: O(n)
This means the time to deploy grows in a straight line as you add more module instances.
[X] Wrong: "Adding more modules won't affect deployment time much because modules are just code."
[OK] Correct: Each module creates real resources, so more modules mean more work for Terraform and cloud APIs.
Understanding how module count affects deployment time helps you design efficient infrastructure and explain your choices clearly.
"What if we changed the modules to use dynamic blocks inside a single module instead of multiple module instances? How would the time complexity change?"
Practice
Solution
Step 1: Understand module purpose
Terraform modules are designed to group related cloud resources together for reuse and better organization.Step 2: Compare options
Only To group and reuse cloud resource configurations correctly describes this purpose. Other options describe unrelated tasks.Final Answer:
To group and reuse cloud resource configurations -> Option AQuick Check:
Module purpose = reuse and grouping [OK]
- Confusing modules with scripts
- Thinking modules store credentials
- Assuming modules monitor usage
network located in a folder modules/network?Solution
Step 1: Identify module call syntax
Terraform usesmodule "name" { source = "path" }to call modules.Step 2: Check options
Only module "network" { source = "modules/network" } uses correctmoduleblock andsourceattribute.Final Answer:
module "network" { source = "modules/network" } -> Option AQuick Check:
Module call uses module block and source [OK]
- Using resource instead of module
- Using path instead of source
- Omitting quotes around module name
modules/
webserver/
main.tf
variables.tf
outputs.tf
What is the purpose of the variables.tf file inside the module?Solution
Step 1: Understand file roles in module
In Terraform modules,variables.tfdefines input variables the module accepts.Step 2: Match options
To define input variables for the module correctly states this. Outputs are inoutputs.tf, main resources inmain.tf, and provider configs are outside modules.Final Answer:
To define input variables for the module -> Option BQuick Check:
variables.tf = input variables [OK]
- Confusing variables.tf with outputs.tf
- Putting resource code in variables.tf
- Adding provider config inside module
module "db" {
source = "modules/database"
db_name = "prod"
}
But Terraform shows an error: "Unsupported argument: db_name". What is the likely cause?Solution
Step 1: Understand error meaning
"Unsupported argument" means the module does not expect the given input variable.Step 2: Check module inputs
Ifdb_nameis not declared as a variable in the module's variables.tf, Terraform rejects it.Final Answer:
The module does not declare an input variable named db_name -> Option CQuick Check:
Input variables must be declared to be used [OK]
- Assuming source path causes unsupported argument
- Thinking module names restrict underscores
- Trying to declare inputs inside main.tf
Solution
Step 1: Define optional input variable
To make tags optional, define a variable with type map(string) and default empty map{}.Step 2: Evaluate options
In variables.tf:variable "tags" { type = map(string) default = {} }correctly defines this in variables.tf. In main.tf:variable "tags" { default = null }lacks type and uses null default. In outputs.tf:output "tags" { value = var.tags }is output, not input. In variables.tf:variable "tags" { type = list(string) }uses list instead of map.Final Answer:
In variables.tf: variable "tags" { type = map(string) default = {} } -> Option DQuick Check:
Optional map variable needs default empty map [OK]
- Using list instead of map for tags
- Not setting a default value
- Declaring variables in outputs.tf
Ready to prove it?
Pass the test and this pattern moves unknown
