Data source dependencies in Terraform - Time & Space 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?
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 the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Each
aws_subnetdata 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.
As the number of subnet IDs increases, the number of API calls grows directly with it.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The number of API calls grows in a straight line with the number of subnets.
Time Complexity: O(n)
This means the time to fetch all subnet data grows directly in proportion to how many subnets you ask for.
[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.
Knowing how data source calls add up helps you design Terraform configurations that scale well and avoid surprises in deployment time.
"What if we combined multiple subnet IDs into one data source call using a filter? How would the time complexity change?"
Practice
Solution
Step 1: Understand data source role
Data sources in Terraform are used to read information about existing resources outside Terraform or created by other means.Step 2: Identify dependency purpose
Dependencies ensure Terraform fetches this existing info before creating or modifying other resources that rely on it.Final Answer:
To fetch existing resource information before creating new resources -> Option AQuick Check:
Data source dependencies = fetch info first [OK]
- Thinking data sources create or delete resources
- Confusing dependencies with resource lifecycle
- Assuming data sources speed up plans by skipping
aws_vpc.main in a resource block?Solution
Step 1: Recall depends_on syntax
Terraform expects depends_on to be a list of resource or data source references without quotes.Step 2: Analyze options
depends_on = [aws_vpc.main] uses a list with the correct reference syntax. depends_on = aws_vpc.main misses list brackets. depends_on = ["aws_vpc.main"] uses quotes making it a string, not a reference. depends_on = {aws_vpc.main} uses braces which is invalid.Final Answer:
depends_on = [aws_vpc.main] -> Option DQuick Check:
depends_on requires list of references [OK]
- Using quotes around resource names
- Omitting square brackets for depends_on
- Using curly braces instead of square brackets
data "aws_ami" "example" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.example.id
instance_type = "t2.micro"
}What happens if the data source
aws_ami.example fails to find a matching AMI?Solution
Step 1: Understand data source failure behavior
If a data source cannot find the requested resource, Terraform plan fails with an error because it cannot resolve the value.Step 2: Check resource dependency
The resource depends on the data source's output for the AMI ID, so without it, resource creation cannot proceed.Final Answer:
Terraform plan will fail with an error before creating the instance -> Option CQuick Check:
Data source failure = plan error [OK]
- Assuming default values are used automatically
- Thinking Terraform retries data sources
- Believing resource creation skips silently
data "aws_subnet" "selected" {
filter {
name = "tag:Name"
values = ["my-subnet"]
}
}
resource "aws_instance" "app" {
ami = "ami-123456"
instance_type = "t2.micro"
subnet_id = data.aws_subnet.selected.id
depends_on = [aws_instance.app]
}What is the problem with this configuration?
Solution
Step 1: Analyze depends_on usage
The resource aws_instance.app declares depends_on referencing itself, which creates a circular dependency.Step 2: Understand circular dependency impact
Circular dependencies cause Terraform to fail planning or applying because it cannot resolve resource order.Final Answer:
The resource depends_on itself causing a circular dependency -> Option AQuick Check:
depends_on self-reference = circular dependency error [OK]
- Confusing data source filter syntax as error
- Thinking subnet_id cannot use data sources
- Assuming instance_type is invalid without checking
Solution
Step 1: Understand implicit dependencies
When a resource uses a data source attribute (like subnet ID), Terraform automatically creates a dependency to fetch data first.Step 2: Evaluate explicit depends_on necessity
Explicit depends_on is not needed unless the dependency is indirect or complex; here, implicit dependency suffices.Final Answer:
No need for depends_on; Terraform infers dependency from data source usage -> Option BQuick Check:
Data source usage creates implicit dependency [OK]
- Adding unnecessary depends_on for direct data source use
- Creating resources in wrong order ignoring implicit dependencies
- Using null_resource unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
