(aws-ecspatterns): Support multi-targetgroup applications on different containers
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 74
Description
### Describe the feature
The ApplicationMultipleTargetGroupsEc2Service module only passes in the default task definition container name
https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts#L149.
All ecs loadbalancer configurations will end up using only the default container name for all target groups.
https://github.com/aws/aws-cdk/blob/2b6a0bede42a367d650c473260f8a643f9fa5180/packages/%40aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts#L513
For example, if i have a task def that has 2 containers, for a SPA. where i have and api and spa server separately.
```go
taskDef.AddContainer(jsii.String("api-container"), &awsecs.ContainerDefinitionOptions{
PortMappings: &[]*awsecs.PortMapping{
{
ContainerPort: jsii.Number(80),
Protocol: awsecs.Protocol_TCP,
},
},
Image: awsecs.ContainerImage_FromEcrRepository(
getRepository(scope, "app-api"),
jsii.String("latest"),
),
Environment: &map[string]*string{
"DEBUG": jsii.String("false"),
},
MemoryLimitMiB: jsii.Number(512),
Secrets: &secrets,
})
taskDef.AddContainer(jsii.String("client-container"), &awsecs.ContainerDefinitionOptions{
PortMappings: &[]*awsecs.PortMapping{
{
ContainerPort: jsii.Number(8080),
Protocol: awsecs.Protocol_TCP,
},
},
MemoryLimitMiB: jsii.Number(256),
Image: awsecs.ContainerImage_FromEcrRepository(
getRepository(scope, "app-client"),
jsii.String("latest"),
),
})
```
If you configure multiple target groups:
```go
TargetGroups: &[]*awsecspatterns.ApplicationTargetProps{
{
ContainerPort: jsii.Number(80),
Listener: jsii.String("api-container"),
},
{
ContainerPort: jsii.Number(8080),
PathPattern: jsii.String("/app"),
Listener: jsii.String("spa-container"),
Priority: jsii.Number(1),
},
},
```
the result is incorrectly assigning the loadbalancers to the same container:
```bash
aws ecs describe-services --cluster ${CLUSTER_NAME} --services web-app
```
```json
"loadBalancers": [
{
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:xxxxxxxx:targetgroup/target-group2",
"containerName": "app-api",
"containerPort": 80
},
{
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:xxxxxxx:targetgroup/target-group1",
"containerName": "app-api",
"containerPort": 8080
}
],
```
### Use Case
The use case is outlined here:
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/register-multiple-targetgroups.html#multiple-targetgroups-example3
### Proposed Solution
Update ApplicationTargetGroup to include Container name otherwise use the default container name.
https://github.com/aws/aws-cdk/blob/2b6a0bede42a367d650c473260f8a643f9fa5180/packages/%40aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts#L513
this line would then be updated to:
```typescript
targets: [
service.loadBalancerTarget({
containerName: targetProps.containerName,
containerPort: targetProps.containerPort,
protocol: targetProps.protocol,
}),
],
```
### Other Information
_No response_
### Acknowledgements
- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.62.1 (build 8641449)
### Environment details (OS name and version, etc.)
Ubuntu 22.04.1 LTS
Contributor guide
Research direction
Read packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts around line 149 and packages/@aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts around line 513. Trace how ApplicationTargetProps reaches each load balancer target; done means different target groups can select different container names while unspecified names retain the default behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100