I have two (and several other) stacks which create a cyclic dependency:
- app.py
app = cdk.App()
vpc_stack = RcsVpcStack(app, "RcsVPCStack")
efs_stack = RcsEfsStack(app, "RcsEfsStack", vpc_stack=vpc_stack)
ecs_stack = RcsECSStack(app, "RcsECSStack", vpc_stack=vpc_stack, fileSystem=efs_stack.efs_filesystem)
elbv2_stack = RcsELBv2Stack(app, "RcsELBv2Stack",
vpc_stack=vpc_stack,
cert_arn=CERT_ARN
)
ecs_service_stack = RcsECSServiceStack(app, "RcsECSServiceStack",
vpc_stack=vpc_stack,
ecs_stack=ecs_stack,
elbv2_stack=elbv2_stack
)
Elbv2Stack
defines an ApplicationLoadbalancer with listenerself.domain_cert = certm.Certificate.from_certificate_arn( self, "rcs-domain-cert", cert_arn ) self.elbv2_loadbalancer = elbv2.ApplicationLoadBalancer( self, "rcs-elastic-loadbalancer", load_balancer_name="rcs-elastic-loadbalancer", vpc=vpc_stack.vpc, vpc_subnets=ec2.SubnetSelection( subnets=vpc_stack.vpc.public_subnets ), idle_timeout=Duration.seconds(300), internet_facing=True ) self.elbv2_loadbalancer_listener = self.elbv2_loadbalancer.add_listener( "rcs-elastic-loadbalancer-listener", port=443, protocol=elbv2.ApplicationProtocol.HTTPS, certificates=[elbv2.ListenerCertificate.from_certificate_manager(self.domain_cert)] ) self.elbv2_loadbalancer_listener.add_targets( "rcs-default-target-group", port=80, protocol=elbv2.ApplicationProtocol.HTTP )
EcsStack
defines an ECS Container service# ... service definitions elbv2_stack.elbv2_loadbalancer_listener.add_targets( "rcs-api-backend-loadbalancer-target", port=80, targets=[ ecs_service_stack.ecs_api_backend.load_balancer_target( container_name="testService", container_port=someContainerPort ) ] ) self.test_target_group = elbv2.ApplicationTargetGroup( self, "rcs-api-backend-target-group", vpc=vpc_stack.vpc, port=80, protocol=elbv2.ApplicationProtocol.HTTP, health_check=elbv2.HealthCheck( healthy_http_codes="200-299", interval=Duration.seconds(20), path="/health-check", protocol=elbv2.Protocol.HTTP, timeout=Duration.seconds(5), healthy_threshold_count=2 ) ) elbv2_stack.elbv2_loadbalancer_listener.add_action( "rcs-api-backend-elbv2-action", action=elbv2.ListenerAction.forward( target_groups=[self.test_target_group], ), priority=2, conditions=[ elbv2.ListenerCondition.path_patterns(["*"]) ] )
The Error:
RuntimeError: Error: RcsECSServiceStack depends on RcsELBv2Stack
({RcsECSServiceStack/rcs-api-backend/Service}.addDependency({RcsELBv2Stack/rcs-elastic-loadbalancer/rcs-elastic-loadbalancer-listener/Resource}),
{RcsECSServiceStack/rcs-api-backend/Service}.addDependency({RcsELBv2Stack/rcs-elastic-loadbalancer/rcs-elastic-loadbalancer-listener/rcs-default-target-groupGroup/Resource}),
{RcsECSServiceStack/rcs-api-backend/Service}.addDependency({RcsELBv2Stack/rcs-elastic-loadbalancer/rcs-elastic-loadbalancer-listener/rcs-api-backend-loadbalancer-targetGroup/Resource}),
{RcsECSServiceStack/rcs-api-backend/Service}.addDependency({RcsELBv2Stack/rcs-elastic-loadbalancer/rcs-elastic-loadbalancer-listener/rcs-api-backend-elbv2-actionRule/Resource})). Adding this dependency (RcsELBv2Stack -> RcsECSServiceStack/rcs-api-backend-target-group/Resource.Ref) would create a cyclic reference.
To have an overview, it would be great to define the container service with its target group in the dedicated stack, but I get a cyclic dependency if I do that.
Is it a problem with the action added?
Is there a possible solution to this?