aws / aws/aws-cdk

aws_ecs_patterns: ApplicationLoadBalancedFargateService does not allow multiple services to connect to the same load balancer

Open
#32,958 1 comment 2 reactions 0 assignees View on GitHub
@aws-cdk/aws-ecs-patterns bug effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

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](https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts#L510) 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

1. Clone the repo https://gitlab.com/bytesnz/fargate-services-fail
2. Install node dependencies
3. 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_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the synth failure with the linked fargate-services-fail example, then read packages/aws-cdk-lib/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts around the hardcoded PublicListener ID. Check the related listener IDs and verify that creating multiple services against one load balancer synthesizes successfully without construct-name collisions.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.