Terraform: Error: Cycle: aws_security_group.a, aws_security_group.b
Resources reference each other (directly or indirectly), so Terraform can't order them.
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 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_oncreating loops- Module outputs feeding each other's inputs
- Locals referencing resources referencing locals
- Provisioners referencing self
⚡ Quick fix
- Use standalone rule resources (
aws_security_group_rule) instead of inline blocks - Remove circular
depends_on/references - Visualise with
terraform graph
Detailed fix by platform
Terraform
terraform graph | dot -Tsvg > graph.svg
How to diagnose
- Cycle — Which resources?
- Reference — Mutual?
- 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:
Similar errors
- DependencyViolation: resource has dependent objects (destroy) Terraform: Error: deleting EC2 VPC (vpc-0abc): DependencyViolation: The vpc 'vpc-0abc' has dependencies and cannot be deleted
- Error building ARM Config: obtain subscription from Azure CLI Terraform: Error: building AzureRM Client: obtaining subscription ID / Please run 'az login' to setup account
- Invalid AWS Region Terraform: Error: Invalid AWS Region: us-east-3 / Error: configuring Terraform AWS Provider: validating provider credentials: operation error STS: GetCallerIdentity
Most viewed in Terraform
- Error acquiring the state lock Terraform: Error: Error acquiring the state lock — ConditionalCheckFailedException / state blob is already locked
- Inconsistent dependency lock file Terraform: Error: Inconsistent dependency lock file — provider x: required by this configuration but no version is selected
- Resource is tainted and must be replaced Terraform: # aws_instance.web is tainted, so must be replaced — -/+ destroy and then create replacement
Other ways to find it
Where this error sits
Origin: Platform & cloud. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026