Creating a child module in Terraform - Performance & Efficiency
When using Terraform, creating a child module means calling a separate set of instructions multiple times.
We want to know how the time to apply changes grows as we add more child modules.
Analyze the time complexity of the following operation sequence.
module "child" {
source = "./child_module"
count = var.instance_count
name = "example-${count.index}"
}
variable "instance_count" {
type = number
default = 3
}
This code creates multiple instances of a child module based on a count variable.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Each child module creates its own resources, triggering API calls to provision them.
- How many times: The number of times equals the count variable, as each module instance runs separately.
As the count increases, the number of API calls grows proportionally because each module instance provisions resources independently.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 times the resources in one module |
| 100 | 100 times the resources in one module |
| 1000 | 1000 times the resources in one module |
Pattern observation: The work grows directly with the number of module instances.
Time Complexity: O(n)
This means the time to apply grows linearly as you add more child module instances.
[X] Wrong: "Adding more child modules won't increase the time much because they run together."
[OK] Correct: Each child module creates resources separately, so more modules mean more total work and API calls.
Understanding how module count affects provisioning time helps you design scalable infrastructure and explain your choices clearly.
"What if the child module itself creates multiple resources internally? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand module purpose
Child modules help organize infrastructure by splitting it into smaller parts.Step 2: Identify benefits
This makes code reusable and easier to manage across projects.Final Answer:
To break down infrastructure into smaller, reusable pieces -> Option BQuick Check:
Child modules = reusable infrastructure parts [OK]
- Thinking child modules increase resource count in root
- Believing child modules remove need for variables
- Assuming child modules limit to one cloud provider
./modules/network?Solution
Step 1: Identify correct block type
Child modules are included using themoduleblock in Terraform.Step 2: Check syntax for source
Thesourceattribute points to the child module folder path.Final Answer:
module "network" { source = "./modules/network" } -> Option DQuick Check:
Use module block with source path [OK]
- Using resource or provider blocks instead of module
- Placing source inside resource or variable blocks
- Incorrect block type for including modules
module "web" {
source = "./modules/webserver"
instance_count = 3
}And the child module
variables.tf contains:variable "instance_count" {
type = number
}What will be the value of
var.instance_count inside the child module?Solution
Step 1: Check variable passing
The root module passesinstance_count = 3to the child module.Step 2: Confirm variable usage
The child module declaresinstance_countvariable to receive this value.Final Answer:
3 -> Option AQuick Check:
Passed variable value = 3 [OK]
- Assuming default or null if variable is declared
- Confusing variable names or missing passing
- Thinking child module variables reset automatically
module "db" {
source = "./modules/database"
db_name = "prod"
}But Terraform shows an error:
An argument named "db_name" is not expected here. Did you mean to define a variable named "db_name"?What is the likely cause?
Solution
Step 1: Analyze error message
The error indicates that the argument "db_name" is not expected, which happens when the child module does not declare a variable named "db_name".Step 2: Check child module variables
If child module does not declarevariable "db_name", Terraform cannot accept the passed value.Final Answer:
The child module does not declare a variable named db_name -> Option CQuick Check:
Passing undeclared argument causes "not expected" error [OK]
- Ignoring variable declaration in child module
- Assuming source path error without checking variables
- Thinking modules can't receive variables
variables.tf and main.tf snippets inside the child module is the best practice?Solution
Step 1: Check variable types and defaults
Bucket name should be string without default to force user input. Versioning is boolean with default true.Step 2: Validate resource usage
Resource usesvar.bucket_nameandvar.versioningcorrectly with boolean type for versioning enabled.Final Answer:
variables.tf with string and bool (default true), main.tf using these variables correctly -> Option AQuick Check:
Correct types and defaults = variables.tf:variable "bucket_name" { type = string } variable "versioning" { type = bool, default = true }
main.tf:resource "aws_s3_bucket" "bucket" { bucket = var.bucket_name versioning { enabled = var.versioning } }[OK]
- Using string type for boolean variables
- Setting wrong default types
- Using number type for bucket name
Ready to prove it?
Pass the test and this pattern moves unknown
