0
0
Terraformcloud~5 mins

Creating a child module in Terraform - Performance & Efficiency

Choose your learning style9 modes available
Time Complexity: Creating a child module
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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 Repeating Operations

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

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
1010 times the resources in one module
100100 times the resources in one module
10001000 times the resources in one module

Pattern observation: The work grows directly with the number of module instances.

Final Time Complexity

Time Complexity: O(n)

This means the time to apply grows linearly as you add more child module instances.

Common Mistake

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

Interview Connect

Understanding how module count affects provisioning time helps you design scalable infrastructure and explain your choices clearly.

Self-Check

"What if the child module itself creates multiple resources internally? How would that affect the time complexity?"