Why Terraform reports a dependency cycle, how to read the resource list in the error and the graph, and how to break the most common cycles: security groups that reference each other, inline policies and lifecycle settings.

Terraform orders every change by walking a dependency graph built from the references in your code. When resources refer to each other in a loop, there is no first step, and terraform validate or plan stops with Error: Cycle: followed by the resources involved.
The loop is nearly always one of a few patterns, and the fix is to split one resource so the references point one way. This guide is written for Terraform 1.x and AWS provider 6.x.
The error lists each resource in the loop, one per line:
With two resources, the loop is obvious. With ten, including module outputs and provider nodes, run terraform graph -type=plan -draw-cycles and render the output with Graphviz. The graph command colors the edges that form the cycle, and following them tells you which reference to cut.
In the most common cycle on AWS, the app group allows egress to the database group, and the database group allows ingress from the app group, both as inline blocks:
resource "aws_security_group" "app" {
name = "app"
vpc_id = var.vpc_id
egress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = [aws_security_group.db.id]
}
}
resource "aws_security_group" "db" {
name = "db"
vpc_id = var.vpc_id
ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = [aws_security_group.app.id]
}
}
Each group needs the other's ID before it can be created. Move the rules out into their own resources, which depend on both groups while the groups depend on nothing:
resource "aws_security_group" "app" {
name = "app"
vpc_id = var.vpc_id
}
resource "aws_security_group" "db" {
name = "db"
vpc_id = var.vpc_id
}
resource "aws_vpc_security_group_egress_rule" "app_to_db" {
security_group_id = aws_security_group.app.id
referenced_security_group_id = aws_security_group.db.id
ip_protocol = "tcp"
from_port = 5432
to_port = 5432
}
resource "aws_vpc_security_group_ingress_rule" "db_from_app" {
security_group_id = aws_security_group.db.id
referenced_security_group_id = aws_security_group.app.id
ip_protocol = "tcp"
from_port = 5432
to_port = 5432
}
terraform plan then shows 4 to add. Do not mix inline rules and rule resources on the same group: the aws_security_group docs warn that the two fight over the rule set and cause perpetual diffs. When migrating an existing group, remove the inline blocks and import or recreate the rules as resources in the same change.
The same shape appears whenever a policy is declared as part of the resource it protects and names something that references that resource. A role whose inline policy grants access to the Lambda function that runs as the role is one example, and a bucket configured with a policy naming the CloudFront distribution that serves the bucket is another. The fix is the same as for security groups: declare the policy as its own resource, such as aws_iam_role_policy or aws_s3_bucket_policy, so it depends on both sides.
depends_on adds an edge the same way a reference does. A depends_on added to fix an ordering problem months ago can close a loop once someone adds a reference in the opposite direction, and the error points at both resources without saying which edge is the extra one. Remove depends_on entries that duplicate an existing reference first.
Modules cycle when an output of module A feeds module B and an output of B feeds A, and the values involved come from resources that depend on each other. Terraform evaluates module outputs individually, so this only fails when the underlying resources form a loop. The fix is usually to move the shared resource, often a security group or a role, up to the root module and pass it into both.
Terraform also builds a graph for destroy and replace, and cycles can appear only there. A common case is create_before_destroy on one resource while a dependent resource is replaced in the default order. The lifecycle docs note that Terraform propagates create_before_destroy to the resource's dependencies, so conflicting settings in a chain can still cycle. Run terraform graph -type=plan-destroy -draw-cycles to see that graph, and set the same lifecycle on both sides.
Encore brings infrastructure from code: a service declares the database it uses in TypeScript, and Encore provisions the database, the networking between the service and the database, and the access rules in your AWS or GCP account. The security groups and policies that form most Terraform cycles are generated rather than written by hand, in the account you connect to Encore.

Run in your terminal to get started locally.
// inventory/inventory.ts
import { api, APIError } from "encore.dev/api";
import { SQLDatabase } from "encore.dev/storage/sqldb";
// RDS on AWS, Cloud SQL on GCP, a local Postgres in Docker under `encore run`.
// Network access between the service and the database is provisioned for you.
const db = new SQLDatabase("inventory", { migrations: "./migrations" });
export const reserve = api(
{ expose: true, method: "POST", path: "/stock/:sku/reserve" },
async ({ sku, qty }: { sku: string; qty: number }): Promise<{ onHand: number }> => {
const row = await db.queryRow<{ on_hand: number }>`
UPDATE stock SET on_hand = on_hand - ${qty}
WHERE sku = ${sku} AND on_hand >= ${qty}
RETURNING on_hand`;
if (!row) throw APIError.failedPrecondition("not enough stock");
return { onHand: row.on_hand };
},
);
With an encore.service.ts file and the migration in inventory/migrations, encore run starts a local Postgres and applies it. On AWS, Encore places the database in private subnets and gives each service its own database role, as the AWS infrastructure docs describe, and Encore provisions an IAM role for the service.
A coding agent adding a second service that reads inventory declares it in TypeScript and runs both locally in the same change, with no security group, rule or policy resource to add and no cycle to debug.
Best fit: Teams whose Terraform is mostly glue between application services and their databases, topics and buckets, and who would rather declare those in code.
Terraform builds a dependency graph from the references in your configuration and must order every resource before the ones that use it. When two or more resources refer to each other, directly or through a chain, no valid order exists, and Terraform lists the resources in the loop and stops before planning.
Remove the inline ingress and egress blocks that reference the other group and declare each rule as its own aws_vpc_security_group_ingress_rule or aws_vpc_security_group_egress_rule resource. The rules depend on both groups, and the groups no longer depend on each other.
Yes. depends_on adds an edge to the graph the same way a reference does, so a depends_on that points back at something that already depends on the resource closes a loop. Remove depends_on entries that duplicate existing references.
No. Encore apps declare the databases, topics and buckets they use in application code, and Encore provisions them along with the networking and access between services and resources. You do not write the security group or policy resources that usually form Terraform cycles.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.