How to fix Error: Cycle: resource dependency in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 30, 2025· 111 views

Terraform detected a circular dependency in your configuration where resources depend on each other in a way that creates an unresolvable loop. This breaks Terraform's directed acyclic graph requirement for determining resource creation order.

Quick fix

Fixes E_CYCLE_DEPENDENCY

terraform graph -draw-cycles | dot -Tsvg > graph.svg
  1. Visualize the dependency graph
  2. Identify the circular reference
  3. Break the cycle using data sources
  4. Use explicit depends_on to guide creation order
terraform — error
terraform graph -draw-cycles | dot -Tsvg > graph.svg
Error: Cycle: resource dependency

What this error means

This error occurs when Terraform detects that two or more resources are dependent on each other, forming a cycle. Terraform needs to determine a valid order to create resources (a directed acyclic graph or DAG), but circular dependencies prevent this. This happens when resource A references resource B's attributes, and resource B references resource A's attributes, creating a loop Terraform cannot resolve.

Typical symptoms

  • Terraform plan or apply fails immediately with the cycle error
  • Error message lists specific resources involved in the cycle
  • Configuration worked previously but fails after refactoring
  • Complex configurations with multiple interconnected resources

Common causes

  1. Explicit references where resources reference each other's attributes
  2. Implicit dependencies created through attribute interpolation
  3. Misconfigured depends_on statements creating circular references
  4. Two resources both trying to manage the same relationship (e.g., VM referencing NIC and NIC referencing VM name)
  5. Complex module compositions where outputs create unintended cycles
  6. Data source and resource combinations that inadvertently form loops

How to fix "Error: Cycle: resource dependency"

1Visualize the dependency graph

Use the terraform graph command to see the dependency relationships. For detailed cycle detection, use:

bash
terraform graph -draw-cycles | dot -Tsvg > graph.svg

This creates a visual representation where cycles are highlighted. You'll need Graphviz installed (apt-get install graphviz on Linux or brew install graphviz on macOS).

2Identify the circular reference

Examine the error message and the graph to find which resources are referencing each other. For example:

bash
Error: Cycle: aws_instance.web, aws_security_group.web

This means the EC2 instance and security group are referencing each other.

3Break the cycle using data sources

Replace direct resource references with data sources where possible. Instead of:

hcl
resource "aws_security_group" "web" {
  vpc_id = aws_vpc.main.id
}

resource "aws_instance" "web" {
  security_groups = [aws_security_group.web.id]
  vpc_id          = aws_vpc.main.id
}

Use a data source to look up the security group by name instead of creating a new reference:

hcl
data "aws_security_group" "web" {
  name = "existing-sg"
  vpc_id = aws_vpc.main.id
}

resource "aws_instance" "web" {
  security_groups = [data.aws_security_group.web.id]
  vpc_id          = aws_vpc.main.id
}
4Use explicit depends_on to guide creation order

In some cases, you can break cycles by explicitly defining the creation order with depends_on. However, this only works if you're referencing specific outputs rather than attributes:

hcl
resource "aws_network_interface" "example" {
  subnet_id = aws_subnet.example.id
}

resource "aws_instance" "example" {
  depends_on = [aws_network_interface.example]
  # Don't reference the network interface directly
}
5Restructure into separate Terraform runs

For complex cycles that can't be broken with data sources, split the configuration into multiple Terraform projects:

1. Create resource A and output its ID
2. In a separate Terraform configuration, use data sources to reference the ID from step 1
3. Create resource B using the data source
4. If needed, manually update resource A in a third Terraform run

This multi-step approach respects the DAG requirement while still managing complex infrastructure.

6Validate your changes

After refactoring, run:

bash
terraform validate
terraform plan

Ensure the plan shows the resources in the correct order and no cycles remain.

Advanced notes

Cycles are by design in Terraform - they prevent undefined behavior. However, there are legitimate real-world use cases for circular dependencies. If you have a genuine need (like a two-way VPC peering where both sides need to reference each other), consider: 1) Using null_resource with local-exec provisioners to handle post-apply updates; 2) Splitting into separate Terraform applies with local state; 3) Contributing to Terraform to enhance cycle detection for specific scenarios. The terraform graph command has a -draw-cycles flag that only works with -type=plan, plan-refresh-only, plan-destroy, or apply operations. Use this for detailed diagnostic information when refactoring complex configurations.

Frequently asked questions

What does "E_CYCLE_DEPENDENCY" mean?
This error occurs when Terraform detects that two or more resources are dependent on each other, forming a cycle. Terraform needs to determine a valid order to create resources (a directed acyclic graph or DAG), but circular dependencies prevent this. This happens when resource A references resource B's attributes, and resource B references resource A's attributes, creating a loop Terraform cannot resolve.
How do I fix "E_CYCLE_DEPENDENCY"?
Terraform detected a circular dependency in your configuration where resources depend on each other in a way that creates an unresolvable loop. This breaks Terraform's directed acyclic graph requirement for determining resource creation order.
What causes "E_CYCLE_DEPENDENCY"?
Explicit references where resources reference each other's attributes Implicit dependencies created through attribute interpolation Misconfigured depends_on statements creating circular references
Is the "E_CYCLE_DEPENDENCY" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading