Skip to content
Latchkey

Terraform module depends_on Pitfalls in CI

A depends_on on a module block is a blunt instrument: it must reference whole resources/modules (not attributes), and an over-broad one can force the dependent module’s values to be unknown at plan time or create cycles.

What this error means

plan/apply fails on a module depends_on - it references an attribute instead of a resource/module, or the whole dependent module shows "known after apply" and produces churn, or a cycle appears between two modules.

terraform output
Error: Invalid depends_on reference

  on main.tf line 12, in module "app":
  12:   depends_on = [module.network.vpc_id]

The "depends_on" argument must be a reference to a resource or module, not an
attribute of one. Use depends_on = [module.network] instead.

Common causes

depends_on references an attribute

depends_on accepts resource/module addresses, not their attributes. module.network.vpc_id is an attribute; the valid form is module.network.

Over-broad module dependency

A module-level depends_on makes the dependent module wait on the entire referenced module, which can force its outputs to "known after apply" and create avoidable churn - or a cycle if the dependency is mutual.

How to fix it

Reference the module, and prefer implicit dependencies

Use the module/resource address in depends_on, but prefer wiring outputs to inputs so the dependency is implicit and precise.

main.tf
module "app" {
  source = "./modules/app"
  vpc_id = module.network.vpc_id   # implicit, precise dependency
  # depends_on = [module.network]  # only if there is no data reference to express
}

Break module cycles

  1. Remove a mutual depends_on and express the real direction via output→input wiring.
  2. Split a module if two halves depend on each other in opposite directions.
  3. Keep depends_on minimal so it does not force unnecessary "known after apply".

How to prevent it

  • Prefer implicit dependencies (output→input) over explicit depends_on.
  • When you must use depends_on, reference the module/resource, not an attribute.
  • Avoid mutual module dependencies that create cycles.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →