Course contentsShow
← Cloud and Infrastructure
Lesson 752 of 2,98914. Terraform FundamentalsFree lesson

Data Sources and Dependencies

Understand how data sources create implicit dependencies and affect the order of Terraform operations.

Data Sources and Dependencies

What You'll Learn

You'll learn how Terraform automatically creates dependencies when data sources are used, and why this affects the order in which operations are executed.

How Data Sources Create Implicit Dependencies

When you reference a data source inside a resource block, Terraform automatically understands that the data must be fetched before the resource can be created. This creates an implicit dependency — you don't need to use depends_on explicitly; Terraform figures it out from your references.

Think of it like cooking: if your recipe says "add the measured flour," you must first complete the "measure flour" step. Terraform reads your configuration the same way — any reference to data.aws_ami.example.id tells Terraform: "I need that AMI ID first."

How This Affects Operation Order

Terraform builds a dependency graph during planning. Data sources that are referenced become nodes in that graph, and resources that use them must wait:

data "aws_ami" "ubuntu" {
  most_recent = true
  owners      = ["099720109477"]
}

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id  # Implicit dependency created here
  instance_type = "t2.micro"
}

In this example, Terraform must query the data source first (to get ubuntu.id) before it can create the aws_instance. If the data source fails or returns no results, the resource creation won't even be attempted.

This ordering ensures your infrastructure is built logically and prevents errors from missing information.

Key Takeaway: Data sources automatically create implicit dependencies when referenced in resources, ensuring Terraform fetches external information before using it — no manual depends_on required.