Why data sources query existing infrastructure in Terraform - Performance Analysis
When Terraform uses data sources, it asks the cloud for information about resources already there.
We want to know how the time to get this information changes as we ask about more resources.
Analyze the time complexity of the following operation sequence.
data "aws_instance" "example" {
filter {
name = "tag:Name"
values = ["web-server"]
}
}
output "instance_id" {
value = data.aws_instance.example.id
}
This code asks AWS to find an existing instance with a specific tag and returns its ID.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Querying the cloud provider's API to find matching resources.
- How many times: Once per data source block in the configuration.
As you add more data sources querying different resources, the number of API calls grows.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: Each new data source adds one more API call, so the total grows directly with the number of data sources.
Time Complexity: O(n)
This means the time to query existing infrastructure grows linearly with how many data sources you use.
[X] Wrong: "Data sources run once and then don't add to execution time no matter how many are used."
[OK] Correct: Each data source makes its own call to the cloud, so more data sources mean more calls and more time.
Understanding how querying existing resources scales helps you design efficient infrastructure code and shows you think about real-world cloud costs and delays.
"What if multiple data sources queried the same resource? How would the time complexity change?"
Practice
Solution
Step 1: Understand Terraform data sources purpose
Data sources allow Terraform to read details about resources that exist but were not created by the current Terraform configuration.Step 2: Identify the correct use case
Since data sources do not create or delete resources, their main use is to fetch information about existing infrastructure.Final Answer:
To get information about resources created outside Terraform -> Option AQuick Check:
Data sources read existing resources = A [OK]
- Thinking data sources create resources
- Confusing data sources with resource blocks
- Assuming data sources manage resource lifecycle
Solution
Step 1: Identify Terraform block types
Terraform uses the keyworddatato define data sources,resourcefor resources,outputfor outputs, andvariablefor input variables.Step 2: Match syntax for data source
The correct syntax for a data source starts withdata, followed by the provider and resource type, then the name and configuration.Final Answer:
data "aws_vpc" "main" { id = "vpc-123456" } -> Option CQuick Check:
Data source syntax starts with 'data' = A [OK]
- Using 'resource' instead of 'data' for data sources
- Confusing outputs or variables with data sources
- Incorrect block structure or missing quotes
data "aws_subnet" "example" {
filter {
name = "tag:Name"
values = ["public-subnet"]
}
}What does
data.aws_subnet.example.id return?Solution
Step 1: Understand the data source filter
The filter selects the subnet resource with the tag Name equal to "public-subnet".Step 2: Determine the returned value
Accessingdata.aws_subnet.example.idreturns the ID of the subnet matching the filter, not creating or listing all subnets.Final Answer:
The ID of the subnet tagged with Name=public-subnet -> Option DQuick Check:
Filter selects existing subnet ID = B [OK]
- Thinking data source creates resources
- Assuming it returns all subnets without filter
- Misunderstanding filter syntax
data "aws_security_group" "web" {
name = "web-sg"
}But Terraform shows an error:
"name" is not a valid argument. What is the likely fix?Solution
Step 1: Check data source argument rules
Theaws_security_groupdata source does not accept a directnameargument; it uses filters to find resources.Step 2: Correct usage with filter block
Use afilterblock withname = "group-name"andvalues = ["web-sg"]to query by name.Final Answer:
Replacenamewithfilterblock to find security group by name -> Option BQuick Check:
Use filter block for querying by name = D [OK]
- Using unsupported arguments in data sources
- Confusing resource and data source syntax
- Assuming data sources create or modify resources
Solution
Step 1: Identify correct block type for querying existing AMI
To get info about an existing AMI, use adatablock, notresource,variable, oroutput.Step 2: Verify data source configuration
Theaws_amidata source withmost_recent = true,owners, andfilterfor name pattern correctly fetches the latest Ubuntu AMI ID.Final Answer:
Use a data source with most_recent, owners, and filter to get latest AMI -> Option AQuick Check:
Data source with filters fetches latest AMI = C [OK]
- Using resource block instead of data source
- Hardcoding AMI IDs in variables or outputs
- Not filtering or specifying owners in data source
Ready to prove it?
Pass the test and this pattern moves unknown
