Error: Cycle (dependency cycle) Terraform Origin: Platform & cloud OSI layer 7 · Application Last updated 5 Nov 2026

Terraform: Error: Cycle: aws_security_group.a, aws_security_group.b

Resources reference each other (directly or indirectly), so Terraform can't order them.

Exact message you may see Error: Cycle: aws_security_group.a, aws_security_group.bError: Cycle: module.x.aws_y.z, module.w.aws_v.uError: Cycle:Terraform dependency cycle detectedError: Cycle: local.x, aws_instance.y
Seen on: Terraform

Meaning

Break the loop: security groups referencing each other → use separate aws_security_group_rule resources; moves values to variables/locals; remove unnecessary depends_on; split modules. terraform graph | dot -Tpng visualises the cycle.

Common causes

  • Mutual references between resources (security groups)
  • depends_on creating loops
  • Module outputs feeding each other's inputs
  • Locals referencing resources referencing locals
  • Provisioners referencing self

⚡ Quick fix

  1. Use standalone rule resources (aws_security_group_rule) instead of inline blocks
  2. Remove circular depends_on/references
  3. Visualise with terraform graph

Detailed fix by platform

Terraform

  1. terraform graph | dot -Tsvg > graph.svg

How to diagnose

  1. Cycle — Which resources?
  2. Reference — Mutual?
  3. Fix — Separate rule resources?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Platform & cloud. Errors that are often confused with or caused by this one (click to open):

DependencyViolation: resource has dependent objects (destroy) — Terraform: Error: deleting EC2 VPC (vpc-0abc): DependencyViolation: The vpc 'vpc-0abc' has dependencies and cannot be deleted (Terraform)DependencyViolation: …Error building ARM Config: obtain subscription from Azure CLI — Terraform: Error: building AzureRM Client: obtaining subscription ID / Please run 'az login' to setup account (Terraform)Error building ARM Co…Invalid AWS Region — Terraform: Error: Invalid AWS Region: us-east-3 / Error: configuring Terraform AWS Provider: validating provider credentials: operation error STS: GetCallerIdentity (Terraform)Invalid AWS RegionInvalid count argument (value unknown until apply) — Terraform: Error: Invalid count argument — The "count" value depends on resource attributes that cannot be determined until apply (Terraform)Invalid count argumen…Invalid for_each argument (keys cannot be determined until apply) — Terraform: Error: Invalid for_each argument — The "for_each" map includes keys derived from resource attributes that cannot be determined until apply (Terraform)Invalid for_each argu…No valid credential sources found (AWS provider) — Terraform: Error: No valid credential sources found — failed to refresh cached credentials, no EC2 IMDS role found / Error: configuring Terraform AWS Provider: no valid credential sources (Terraform)No valid credential s…Reference to undeclared resource — Terraform: Error: Reference to undeclared resource / Reference to undeclared input variable (Terraform)Reference to undeclar…Runtime.ImportModuleError — Runtime.ImportModuleError: Unable to import module 'lambda_function': No module named 'x' (AWS)Runtime.ImportModuleE…Error: Cy…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.