0
0
Terraformcloud~10 mins

Data source dependencies in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Data source dependencies
Start Terraform Apply
Evaluate Data Sources
Check Dependencies
If dependency exists
Wait for dependency
Dependency resolved
Fetch Data Source Values
Use Data in Resources
Complete Apply
Terraform first evaluates data sources, waits for any dependencies to be ready, fetches their values, then uses those values in resource creation.
Execution Sample
Terraform
data "aws_vpc" "main" {
  filter {
    name   = "tag:Name"
    values = ["main-vpc"]
  }
}

resource "aws_subnet" "example" {
  vpc_id     = data.aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}
This code fetches the main VPC by tag, then creates a subnet inside that VPC using the fetched VPC ID.
Process Table
StepActionDependency CheckResultNext Step
1Start terraform applyN/ABegin executionEvaluate data sources
2Evaluate data source aws_vpc.mainNo dependenciesFetch VPC info with tag 'main-vpc'Data source fetched
3Data source aws_vpc.main fetchedN/AVPC ID availableUse VPC ID in resource aws_subnet.example
4Create resource aws_subnet.exampleDepends on data.aws_vpc.main.idSubnet created with VPC IDApply complete
5Apply completeN/AAll resources created successfullyEnd
💡 All data sources evaluated and dependencies resolved before resource creation.
Status Tracker
VariableStartAfter Step 2After Step 3Final
data.aws_vpc.main.idundefinedfetched VPC ID (e.g., vpc-123abc)availableavailable
aws_subnet.example.vpc_idundefinedundefinedset to data.aws_vpc.main.idset
Key Moments - 3 Insights
Why does Terraform wait before creating the subnet resource?
Terraform waits because the subnet resource depends on the VPC ID from the data source. It must fetch the VPC ID first (see execution_table step 3) before it can create the subnet.
What happens if the data source fails to fetch the VPC?
If the data source fails, Terraform cannot get the VPC ID, so it cannot create dependent resources like the subnet. The apply will fail early during data source evaluation (see execution_table step 2).
Can a resource depend on multiple data sources?
Yes, a resource can depend on multiple data sources. Terraform will wait for all data sources to be fetched before creating the resource, ensuring all dependencies are resolved.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the VPC ID first available?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Result' column for when 'VPC ID available' appears.
According to the variable tracker, what is the value of aws_subnet.example.vpc_id after step 3?
Afetched VPC ID
Bset to data.aws_vpc.main.id
Cundefined
Dnull
💡 Hint
Look at the 'After Step 3' column for aws_subnet.example.vpc_id in variable_tracker.
If the data source aws_vpc.main had a dependency on another data source, what would Terraform do?
AIgnore the dependency and fetch immediately
BWait for the other data source to be fetched first
CFail the apply immediately
DCreate resources before data sources
💡 Hint
Refer to concept_flow where dependencies cause waiting before fetching.
Concept Snapshot
Terraform evaluates data sources before resources.
Resources can depend on data source outputs.
Terraform waits for data sources to resolve dependencies.
Data source failures stop resource creation.
Use data source attributes as inputs to resources.
Full Transcript
Terraform applies infrastructure by first evaluating data sources. It checks if data sources depend on others and waits accordingly. Once data sources are fetched, their outputs become available. Resources that depend on these outputs use them to create infrastructure. If data sources fail, Terraform stops before creating dependent resources. This ensures resources have the correct data before creation.