We have an existing CDK project that has already created the Application load balancer, VPC, ... I am putting in place a new CDK Global template to deploy our microservices in ECS and auto-register in an existing ALB.
That new project has two stacks TaskDefintionStack and ServiceTask(Dynamic stack). Every new service to deploy will give as env variable the name of its stack, like that we can deploy multiple services with the same CDK project.
_env = {'account': os.environ['CDK_DEFAULT_ACCOUNT'], 'region': os.environ['CDK_DEFAULT_REGION']}
props = {}
environment = os.getenv("ENVIRONMENT", "int")
taskStackName = "{}-{}-task".format(environment, os.environ["SERVICE_NAME"])
serviceStackName = "{}-{}-service".format(environment, os.environ["SERVICE_NAME"])
task = PayiciTaskDefStack(app, taskStackName, props, env=_env)
service = PayiciServiceStack(app, serviceStackName, task.outputs, env=_env)
task.add_dependency(task)
app.synth()
If the service is open to the world it has to register a rule to forward traffics to its target in the existing ALB listener. Unfortunately, from the CDK documentation, it states that we can not call loadBalancer.addListener() and listener.addTargets(). https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_ecs/README.html#using-a-load-balancer-from-a-different-stack
if props["need_alb"] == "True":
_alb = elbv2.ApplicationLoadBalancer.from_lookup(self,id="ALB",load_balancer_arn=props["load_balancer_arn"])
_listener = elbv2.ApplicationListener.from_lookup(self,id="albLiistener",listener_arn=props["listener_arn"], listener_port=80)
if props["need_alb_ssl"] != "True":
# here port 80 check if port 80 listener exist
_target_group = _listener.add_targets("ECS1", port=80, target=[_fargate_service] )
_fargate_service.register_load_balancer_targets()
else:
# here port 443
_target_group = _listener.add_targets("ECS1", port=443, targets=[_fargate_service] )
My question is there a workaround? The workaround gave here does not work, or at least it works only for the first service registered. All new services fall to the issue I stated above. https://github.com/aws-samples/aws-cdk-examples/tree/master/typescript/ecs/cross-stack-load-balancer