0
0
Terraformcloud~5 mins

Data source dependencies in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Data source dependencies
O(n)
Understanding Time Complexity

When Terraform uses data sources, it fetches information from existing infrastructure. Understanding how the time to fetch this data grows helps us plan for bigger setups.

We want to know: how does the number of data sources affect the total time Terraform takes to get all the data?

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


    data "aws_subnet" "example" {
      count = var.subnet_count
      id    = var.subnet_ids[count.index]
    }

    output "subnet_cidrs" {
      value = [for s in data.aws_subnet.example : s.cidr_block]
    }
    

This code fetches details for multiple subnets using their IDs, then outputs their CIDR blocks.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Each aws_subnet data source makes one API call to fetch subnet details.
  • How many times: The API call repeats once for each subnet ID in var.subnet_ids.
How Execution Grows With Input

As the number of subnet IDs increases, the number of API calls grows directly with it.

Input Size (n)Approx. API Calls/Operations
1010 API calls
100100 API calls
10001000 API calls

Pattern observation: The number of API calls grows in a straight line with the number of subnets.

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch all subnet data grows directly in proportion to how many subnets you ask for.

Common Mistake

[X] Wrong: "Fetching multiple data sources happens all at once, so time stays the same no matter how many."

[OK] Correct: Each data source triggers a separate API call, so more data sources mean more calls and more time.

Interview Connect

Knowing how data source calls add up helps you design Terraform configurations that scale well and avoid surprises in deployment time.

Self-Check

"What if we combined multiple subnet IDs into one data source call using a filter? How would the time complexity change?"