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

Module Dependencies and Ordering

Managing implicit and explicit dependencies between modules to control provisioning order.

Module Dependencies and Ordering

You'll learn how Terraform determines the sequence in which modules are provisioned and how to control that order when needed.

How Terraform Orders Module Execution

When you use multiple modules in your configuration, Terraform automatically analyzes dependencies between them. Just like resources, modules don't execute in the order they appear in your code—they execute based on their relationships.

Implicit Dependencies

Terraform creates implicit dependencies when one module references outputs from another:

module "vpc" {
  source = "./modules/network"
  cidr   = "10.0.0.0/16"
}

module "app_servers" {
  source    = "./modules/compute"
  subnet_id = module.vpc.subnet_id  # Implicit dependency!
}

Here, app_servers must wait for vpc to complete because it needs the subnet_id output. Terraform sees this reference and enforces the order automatically.

Explicit Dependencies

Sometimes modules have relationships that Terraform can't detect—for example, when a database must exist before an application is configured, even without direct attribute references. Use depends_on for this:

module "database" {
  source = "./modules/db"
}

module "app_config" {
  source = "./modules/config"
  
  depends_on = [module.database]  # Explicit dependency
}

Think of It Like Building a House

You wouldn't install windows before building the walls. Implicit dependencies are like obvious construction logic (walls need a foundation), while explicit dependencies are like project management decisions (paint must dry before furniture arrives).

Key Takeaway: Terraform automatically orders module provisioning based on output references (implicit), but you can enforce additional ordering with depends_on when modules have hidden relationships that aren't captured through data flow.