0
0
Terraformcloud~10 mins

Why data sources query existing infrastructure in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why data sources query existing infrastructure
Terraform Plan Start
Identify Data Sources
Query Existing Infrastructure
Retrieve Current State Info
Use Info in Resource Config
Apply Changes with Updated Data
Terraform Plan End
Terraform starts by finding data sources, queries existing infrastructure to get current info, then uses that info to configure resources before applying changes.
Execution Sample
Terraform
data "aws_vpc" "selected" {
  filter {
    name   = "tag:Name"
    values = ["my-vpc"]
  }
}

resource "aws_subnet" "example" {
  vpc_id     = data.aws_vpc.selected.id
  cidr_block = "10.0.1.0/24"
}
This code queries an existing VPC by tag and uses its ID to create a subnet inside it.
Process Table
StepActionQuery ResultResource Config UseOutcome
1Terraform starts planN/AN/ABegin execution
2Identify data source aws_vpc.selectedN/AN/APrepare to query existing VPC
3Query AWS for VPC with tag Name=my-vpcVPC found with id vpc-123abcN/AData source populated
4Use data.aws_vpc.selected.id in aws_subnet resourcevpc-123abcSubnet vpc_id set to vpc-123abcSubnet config ready
5Apply resource creationN/ASubnet created in existing VPCInfrastructure updated
6Plan completeN/AN/AExecution ends
💡 Terraform finishes after querying existing infrastructure and applying resources using that data.
Status Tracker
VariableStartAfter Step 3After Step 4Final
data.aws_vpc.selected.idundefinedvpc-123abcvpc-123abcvpc-123abc
aws_subnet.example.vpc_idundefinedundefinedvpc-123abcvpc-123abc
Key Moments - 2 Insights
Why does Terraform query existing infrastructure before creating new resources?
Terraform queries existing infrastructure to get current details like IDs, so new resources can link correctly. See execution_table step 3 where the VPC ID is retrieved before subnet creation.
What happens if the data source query finds no matching resource?
Terraform will fail the plan because it cannot get required info to configure dependent resources. This is implied in step 3 where a VPC must be found to proceed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the query result for the data source?
ANo VPC found
BVPC found with id vpc-123abc
CSubnet created
DTerraform plan ends
💡 Hint
Check the 'Query Result' column at step 3 in execution_table.
At which step does Terraform use the data source value to configure a resource?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Resource Config Use' column in execution_table.
If the data source returned a different VPC ID, how would the variable_tracker change?
ANo change in variables
Baws_subnet.example.vpc_id would remain undefined
Cdata.aws_vpc.selected.id would update to new ID
DTerraform would skip subnet creation
💡 Hint
See how data.aws_vpc.selected.id flows into aws_subnet.example.vpc_id in variable_tracker.
Concept Snapshot
Terraform data sources query existing infrastructure to get current info.
This info is used to configure new resources correctly.
Data sources run before resource creation.
If data source fails, plan stops.
Example: Query VPC ID to create subnet inside it.
Full Transcript
Terraform uses data sources to query existing infrastructure before creating new resources. This lets Terraform get current details like IDs or names needed to link new resources properly. For example, it queries a VPC by tag to get its ID, then uses that ID to create a subnet inside that VPC. The execution flow starts with identifying data sources, querying the infrastructure, retrieving info, using it in resource configs, and finally applying changes. If the data source query fails to find the resource, Terraform cannot proceed. This process ensures new resources fit correctly into existing setups.