Data source vs resource difference in Terraform - Performance Comparison
We want to understand how the time to run Terraform changes when using data sources versus resources.
Specifically, how does Terraform's work grow as we add more data sources or resources?
Analyze the time complexity of this Terraform snippet.
data "aws_ami" "example" {
most_recent = true
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-ebs"]
}
}
resource "aws_instance" "example" {
ami = data.aws_ami.example.id
instance_type = "t2.micro"
}
This code fetches an existing AMI using a data source and then creates an EC2 instance resource using that AMI.
Look at what Terraform does repeatedly when applying this configuration.
- Primary operation: API call to fetch AMI details (data source), and API call to create EC2 instance (resource).
- How many times: Each data source triggers one read API call per apply; each resource triggers one create API call per instance.
As you add more data sources and resources, the number of API calls grows.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 data sources + 10 resources | ~20 API calls (10 reads + 10 creates) |
| 100 data sources + 100 resources | ~200 API calls (100 reads + 100 creates) |
| 1000 data sources + 1000 resources | ~2000 API calls (1000 reads + 1000 creates) |
Pattern observation: The total API calls grow roughly linearly with the number of data sources and resources.
Time Complexity: O(n)
This means the time to apply grows in direct proportion to how many data sources and resources you have.
[X] Wrong: "Data sources are free and do not add to execution time."
[OK] Correct: Data sources make API calls to read existing info, so they add to the total work Terraform does.
Understanding how Terraform scales with data sources and resources helps you design efficient infrastructure code and shows you think about real-world costs.
"What if we replaced many data sources with a single resource that creates and manages all those items? How would the time complexity change?"
Practice
resource and a data source in Terraform?Solution
Step 1: Understand resource role
Resources in Terraform are used to create or manage cloud infrastructure components.Step 2: Understand data source role
Data sources are used to read information about existing infrastructure without changing it.Final Answer:
Resources create or manage infrastructure; data sources only read existing information. -> Option BQuick Check:
Resource = create/manage, Data source = read [OK]
- Thinking data sources create resources
- Confusing resource and data source roles
- Assuming both modify infrastructure
- Believing data sources are temporary
Solution
Step 1: Identify data source syntax
Data sources use the keyworddatafollowed by the type and name.Step 2: Check options
data "aws_ami" "example" {} usesdata "aws_ami" "example" {}, which is correct syntax for a data source.Final Answer:
data "aws_ami" "example" {} -> Option AQuick Check:
Data source starts with 'data' keyword [OK]
- Using 'resource' keyword for data sources
- Mixing 'source' keyword which doesn't exist
- Prefixing resource with 'data_' incorrectly
- Confusing resource and data source syntax
data "aws_vpc" "selected" {
filter {
name = "tag:Name"
values = ["main-vpc"]
}
}
resource "aws_subnet" "example" {
vpc_id = data.aws_vpc.selected.id
cidr_block = "10.0.1.0/24"
}What does the
data.aws_vpc.selected.id represent?Solution
Step 1: Analyze data source usage
Thedata "aws_vpc" "selected"block reads an existing VPC filtered by the tag "Name=main-vpc".Step 2: Understand reference in resource
The resourceaws_subnet.exampleusesdata.aws_vpc.selected.idto get the ID of that existing VPC to create a subnet inside it.Final Answer:
The ID of an existing VPC filtered by tag "Name=main-vpc". -> Option AQuick Check:
Data source reads existing VPC ID [OK]
- Thinking data source creates new VPC
- Confusing subnet ID with VPC ID
- Assuming random ID generation
- Using resource instead of data source for existing info
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
data "aws_vpc" "main" {
id = aws_vpc.main.id
}What is the problem?
Solution
Step 1: Check naming conflict
The resource and data source both use the name "main" which causes a naming conflict in Terraform.Step 2: Terraform rules
Terraform does not allow a resource and a data source to share the same type and name within the same module.Final Answer:
You cannot use the same name for resource and data source. -> Option CQuick Check:
Resource and data source names must be unique [OK]
- Thinking same names cause conflicts
- Believing you always need filters over id
- Assuming data sources can depend on resources
- Blaming missing provider config
Solution
Step 1: Identify existing infrastructure
The VPC already exists, so it should not be recreated or managed as a resource.Step 2: Use data source for existing VPC
Use a data source block to safely read the existing VPC's details without changing it.Step 3: Create subnet resource
Create a resource block for the subnet to add new infrastructure inside the existing VPC.Final Answer:
Use a data source block to read the existing VPC and a resource block to create the subnet. -> Option DQuick Check:
Data source reads existing; resource creates new [OK]
- Trying to recreate existing VPC as resource
- Using data source to create subnet (not possible)
- Ignoring existing VPC and creating subnet without VPC ID
- Using resource for both causing conflicts
Ready to prove it?
Pass the test and this pattern moves unknown
