Course contentsShow
← Cloud and Infrastructure
Lesson 822 of 2,98915. Terraform in PracticeFree lesson

Implicit and Explicit Dependencies

Understand Terraform's dependency graph and when to use depends_on for explicit dependency management.

Implicit and Explicit Dependencies

What you'll learn: How Terraform automatically understands resource relationships and when you need to manually define them.

The Core Idea

Terraform builds a dependency graph to determine the order in which resources must be created, updated, or destroyed. Most of the time, Terraform figures this out automatically through implicit dependencies—when one resource references another's attributes. But sometimes you need explicit dependencies using depends_on to handle hidden relationships Terraform can't detect.

Implicit Dependencies: Automatic and Preferred

When you reference another resource's attribute, Terraform automatically knows one must exist before the other:

resource "aws_instance" "web" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

resource "aws_eip" "web_ip" {
  instance = aws_instance.web.id  # Implicit dependency created here
}

Terraform sees aws_instance.web.id and understands: "Create aws_instance.web first, then aws_eip.web_ip." This is implicit—you didn't declare it, Terraform inferred it.

Explicit Dependencies: When You Need Control

Sometimes resources have relationships Terraform can't see—like when a security group must exist before an instance launches, but you're not directly referencing it. Use depends_on:

resource "aws_iam_role_policy" "example" {
  role   = aws_iam_role.example.id
  policy = "..."

  depends_on = [aws_iam_role.example]  # Explicit dependency
}

Use depends_on sparingly—only when implicit dependencies don't capture the true relationship. Overuse makes configurations harder to maintain.

Key Takeaway: Terraform automatically tracks dependencies through attribute references (implicit), but use depends_on for explicit control when hidden operational dependencies exist that Terraform can't infer from your configuration.