Why is this creating a circular dependency error?
Error: Deployment failed: Error [ValidationError]: Circular dependency between resources: [docDbSG, elasticDocDbCluster]
I even tried separating the code for DocDbCluster and SecurityGroup in separate stacks but it still shows the same error
// Fetch VPC from ID
const vpc = ec2.Vpc.fromLookup(this, 'docDbVpc', {
vpcId: props!.vpcId
});
const securityGroup = new ec2.SecurityGroup(this, 'docDbSG', {
vpc: vpc,
});
// Add ingress rules
// I believe this is where the issue is coming from, if I remove this line it works
securityGroup.addIngressRule(ec2.Peer.securityGroupId(securityGroup.securityGroupId), ec2.Port.allTraffic());
new docdbelastic.CfnCluster(this, 'elasticDocDbCluster', {
adminUserName: props!.AdminUserName,
adminUserPassword: "password",
authType: props!.AuthType,
clusterName: props!.ClusterName,
shardCapacity: props!.ShardCapacity,
shardCount: props!.ShardCount,
subnetIds: props!.dataSubnets,
vpcSecurityGroupIds: [securityGroup.securityGroupId],
});
EDIT:
How to add security group as a source itself in its rules without creating circular dependency?
2
Answers
I think the error is misleading. While
gives a circular dependency error between the DocDBCluster and SecurityGroup, commenting out the docDbCluster still generates a circular dependency error -
This means that the security group itself has a circular dependency. It is probably due to accessing the
securityGroupId
before it is being created.A workaround provided by Zapl in the comments works -
Another approach suggested by Andre also works -
Indeed, you are causing a circular dependency. Here is how you do it: