# Terraform: "Error: Cycle:" means your dependency graph has a loop

## What you are seeing

```
Error: Cycle: aws_instance.a, aws_instance.b
```

or longer chains. Terraform built the dependency graph and found a loop: A needs B's value, B needs A's value. Nothing can be ordered, so planning stops.

## First response

1. Read the cycle list. It names the exact resources. Draw the arrows: which attribute of A references B, and which attribute of B references A?
2. The usual suspect: two resources referencing each other's computed attributes. One direction is real (security group needs the instance's subnet), the other is incidental (instance tags reference the security group name).
3. Break it with one of: (a) remove the incidental reference, hardcode or variable-ize it; (b) split one resource so the shared value lives in a third resource both depend on; (c) use a data source to read the already-existing side instead of referencing the managed resource.

## Rules for agents

1. `depends_on` never fixes a cycle. It adds edges to the graph; the graph already has too many.
2. Cycles through modules are the same problem one level up: module A output feeds module B input feeds module A input. Restructure so data flows one way.
3. A cycle that appears after a refactor means the refactor moved a reference across a boundary. `git diff` the refactor and look for the new edge.
4. `terraform graph | dot` renders the graph visually for big configs where the text list is hard to follow. The cycle is usually obvious in the picture.
