aws_ecs_patterns: ApplicationLoadBalancedFargateService does not allow multiple services to connect to the same load balancer #32958
Description
Describe the bug
(Reopening of #24975)
The ApplicationLoadBalancedFargateService
and ApplicationLoadBalancedServiceBase
classes do not allow for multiple services to be created against the same load balancer. An example repo is https://gitlab.com/bytesnz/fargate-services-fail/-/blob/main/index.ts#L47 based off https://github.com/aws-samples/aws-cdk-examples/tree/main/typescript/application-load-balancer. The cause is a hardcoded name for the listener of PublicListener
.
Regression Issue
- Select this option if this issue appears to be a regression.
Last Known Working CDK Version
No response
Expected Behavior
ApplicationLoadBalancedFargateService
and ApplicationLoadBalancedServiceBase
classes use a unique name for the listener and cdk synth generates that stack correctly and stack is able to be deployed
Current Behavior
cdk synth throws an error
/projects/node_modules/constructs/src/construct.ts:430
throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
^
Error: There is already a Construct with name 'PublicListener' in ApplicationLoadBalancer [NZTCSLoadBalancer]
at Node.addChild (/projects/node_modules/constructs/src/construct.ts:430:13)
at new Node (/projects/node_modules/constructs/src/construct.ts:71:17)
at new Construct (/projects/node_modules/constructs/src/construct.ts:482:17)
at new Resource (/projects/node_modules/aws-cdk-lib/core/lib/resource.js:1:1309)
at new BaseListener (/projects/node_modules/aws-cdk-lib/aws-elasticloadbalancingv2/lib/shared/base-listener.js:1:1721)
at new ApplicationListener (/projects/node_modules/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-listener.js:1:3663)
at ApplicationLoadBalancer.addListener (/projects/node_modules/aws-cdk-lib/aws-elasticloadbalancingv2/lib/alb/application-load-balancer.js:1:6005)
at new ApplicationLoadBalancedServiceBase (/projects/node_modules/aws-cdk-lib/aws-ecs-patterns/lib/base/application-load-balanced-service-base.js:1:4013)
at new ApplicationLoadBalancedFargateService (/projects/node_modules/aws-cdk-lib/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.js:1:864)
at new BackEnd (/projects/src/stacks/backEnd.ts:79:23)
Reproduction Steps
- Clone the repo https://gitlab.com/bytesnz/fargate-services-fail
- Install node dependencies
- Try to synth or deploy with CDK
Possible Solution
I was wondering if the id could be something like id + 'Listener'
instead of 'PublicListener'
. Can do a PR for, but there are a few other ids that might need the same treatment.
Additional Information/Context
No response
CDK CLI Version
2.175.1
Framework Version
2.175.1
Node.js Version
22
OS
Ubuntu
Language
TypeScript
Language Version
5.1.6
Other information
No response