Data source block syntax in Terraform - Time & Space Complexity
When Terraform runs, it often reads existing cloud resources using data sources.
We want to know how the time to read these resources changes as we ask for more data.
Analyze the time complexity of this data source usage.
data "aws_ami" "example" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
This block fetches the latest Amazon Linux 2 AMI matching a name pattern.
Look at what happens when Terraform runs this data source.
- Primary operation: API call to AWS to list AMIs matching filters.
- How many times: Once per data source block during plan or apply.
As the number of matching AMIs grows, the API call returns more data to process.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 1 API call returning 10 items |
| 100 | 1 API call returning 100 items |
| 1000 | 1 API call returning 1000 items |
Pattern observation: The number of API calls stays the same, but processing time grows with the number of items returned.
Time Complexity: O(n)
This means the time grows linearly with the number of matching resources returned by the data source.
[X] Wrong: "The data source always takes the same time no matter how many resources match."
[OK] Correct: Even though the API call count is one, processing more returned items takes more time.
Understanding how data source calls scale helps you design efficient Terraform configurations and explain your choices clearly.
"What if we added multiple data source blocks each fetching different AMIs? How would the time complexity change?"
Practice
data source block in Terraform?Solution
Step 1: Understand the role of data sources
Data sources in Terraform are used to fetch information about existing resources, not to create or delete them.Step 2: Compare options with this role
Only To read information about existing cloud resources without creating new ones describes reading existing resource info without creation, matching the data source purpose.Final Answer:
To read information about existing cloud resources without creating new ones -> Option DQuick Check:
Data source = read existing info [OK]
- Confusing data sources with resource blocks
- Thinking data sources create resources
- Assuming data sources delete resources
Solution
Step 1: Identify correct block type and syntax
Data source blocks start withdata, followed by resource type and local name in quotes.Step 2: Check each option
data "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } } uses correct quotes and block structure. resource "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } } usesresourceinstead ofdata. Options A and C miss quotes around local name or resource type.Final Answer:
data "aws_ami" "example" { filter { name = "name" values = ["my-ami"] } } -> Option CQuick Check:
Data source syntax = data "type" "name" { ... } [OK]
- Using resource instead of data
- Omitting quotes around type or name
- Incorrect block nesting or missing braces
data "aws_vpc" "selected" {
filter {
name = "tag:Name"
values = ["main-vpc"]
}
}What will
data.aws_vpc.selected.id return?Solution
Step 1: Understand the filter block
The filter selects the VPC resource with tag Name equal to main-vpc.Step 2: Identify the returned attribute
data.aws_vpc.selected.idaccesses the ID of the matched VPC, not the tag value or a list.Final Answer:
The ID of the VPC tagged with Name=main-vpc -> Option AQuick Check:
Filter selects resource, .id returns its ID [OK]
- Confusing tag value with resource ID
- Expecting a list instead of single ID
- Assuming filter syntax is wrong
data "aws_subnet" "example" {
filter {
name = "availability-zone"
values = "us-west-2a"
}
}Solution
Step 1: Check the values attribute type
In Terraform, filter values must be a list, even if only one value is given.Step 2: Verify the given values
The code uses a string instead of a list, which causes a syntax error.Final Answer:
The values attribute should be a list, not a string -> Option AQuick Check:
Filter values = list, not string [OK]
- Using string instead of list for values
- Assuming filter block needs no braces
- Thinking local name case matters
Solution
Step 1: Confirm correct block type and attributes
Usedatablock for reading AMI info, withmost_recent = trueandownersas a list.Step 2: Check filter and owner syntax
data "aws_ami" "latest" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } } correctly usesownerslist and a filter for name pattern. data "aws_ami" "latest" { filter { name = "owner" values = ["amazon"] } filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } } incorrectly filters owner as a filter. resource "aws_ami" "latest" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } } uses resource block instead of data. data "aws_ami" "latest" { most_recent = true owner = "amazon" filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } } uses singularownerinstead ofownerslist.Final Answer:
data "aws_ami" "latest" { most_recent = true owners = ["amazon"] filter { name = "name" values = ["amzn2-ami-hvm-*-x86_64-gp2"] } } -> Option BQuick Check:
Use data block, owners list, most_recent true [OK]
- Using resource block instead of data
- Filtering owner instead of using owners attribute
- Using singular owner instead of owners list
Ready to prove it?
Pass the test and this pattern moves unknown
