liveBook · Manning

Circular dependency

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

A circular dependency is a dependency relationship in which a chain of resources forms a closed cycle, causing each resource to depend directly or indirectly on another resource in the same chain.

A circular dependency occurs when resources form a closed chain: one resource waits for another, which eventually waits for the first. Terraform models dependencies as a directed acyclic graph, so it cannot determine a creation or modification order for a cycle and fails during planning. section 1.3.3 section 5.2

Three resources caught in a cycle

Three null_resource instances can create the cycle alpha → charlie → bravo → alpha when each trigger references another resource's ID. Running terraform plan produces a Cycle error naming all three resources. The dependency may be indirect and can also arise between services, such as authentication and logging services that depend on each other. section 5.7.1

When Terraform reports a circular dependency:

  • Inspect every resource named in the error and trace how their attributes connect. section 5.7.1
  • Look for a shared attribute that can be generated independently as a variable or local value. section 5.7.1
  • Prefer simpler dependency chains; a cycle often indicates architecture that is unnecessarily complex and harder to manage or debug. section 5.7.1
Figure 1.4 An example of a circular dependency in which resources are dependent on one another

Use the dependency graph to see how resources waiting on one another create a closed cycle with no valid launch order. section 1.3.3

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines cycles, demonstrates a three-resource failure, and gives a variable-based workaround without detailing broader resolution tools.
Examples
Uses authentication and logging services, three null_resource instances, a Cycle plan error, and a shared build_id variable.
What it assumes
Assumes Terraform resources connect through attributes or parameters and introduces directed acyclic graph constraints through those relationships.
What you can do after
Diagnose Terraform cycle errors and refactor shared values into variables or local values to remove unnecessary resource dependencies.
How it connects
Links Terraform resource graphs and directed acyclic graphs with dependencies, Terraform plan, variables, local values, and null_resource.

Also explained in

Compare how 5 books explain circular dependency →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.