Skip to content

Commit 1393729

Browse files
authored
fix(ecs-patterns): Fix Network Load Balancer Port assignments in ECS Patterns (#18157)
This PR introduces changeable ports as a regression fix for the hardcoded port 80 in both NLB constructs bases. Closes #18073 Additionally it seems like the regression reported in the linked issue was not spotted in the integration tests either. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent b2b234a commit 1393729

5 files changed

+77
-5
lines changed

Diff for: packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export abstract class NetworkLoadBalancedServiceBase extends CoreConstruct {
347347
const loadBalancer = props.loadBalancer ?? new NetworkLoadBalancer(this, 'LB', lbProps);
348348
const listenerPort = props.listenerPort ?? 80;
349349
const targetProps = {
350-
port: 80,
350+
port: props.taskImageOptions?.containerPort ?? 80,
351351
};
352352

353353
this.listener = loadBalancer.addListener('PublicListener', { port: listenerPort });

Diff for: packages/@aws-cdk/aws-ecs-patterns/lib/base/network-multiple-target-groups-service-base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ export abstract class NetworkMultipleTargetGroupsServiceBase extends CoreConstru
374374
protected registerECSTargets(service: BaseService, container: ContainerDefinition, targets: NetworkTargetProps[]): NetworkTargetGroup {
375375
for (const targetProps of targets) {
376376
const targetGroup = this.findListener(targetProps.listener).addTargets(`ECSTargetGroup${container.containerName}${targetProps.containerPort}`, {
377-
port: 80,
377+
port: targetProps.containerPort ?? 80,
378378
targets: [
379379
service.loadBalancerTarget({
380380
containerName: container.containerName,

Diff for: packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.multiple-network-load-balanced-fargate-service.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@
458458
"myServicelb2listener2ECSTargetGroupweb90Group6841F924": {
459459
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
460460
"Properties": {
461-
"Port": 80,
461+
"Port": 90,
462462
"Protocol": "TCP",
463463
"TargetType": "ip",
464464
"VpcId": {

Diff for: packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.special-listener.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@
404404
"FargateNlbServiceLBPublicListenerECSGroup7501571D": {
405405
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
406406
"Properties": {
407-
"Port": 80,
407+
"Port": 2015,
408408
"Protocol": "TCP",
409409
"TargetType": "ip",
410410
"VpcId": {

Diff for: packages/@aws-cdk/aws-ecs-patterns/test/fargate/load-balanced-fargate-service-v2.test.ts

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Match, Template } from '@aws-cdk/assertions';
22
import { Vpc } from '@aws-cdk/aws-ec2';
33
import * as ecs from '@aws-cdk/aws-ecs';
4+
import { ContainerImage } from '@aws-cdk/aws-ecs';
45
import { CompositePrincipal, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
56
import { Duration, Stack } from '@aws-cdk/core';
6-
import { ApplicationMultipleTargetGroupsFargateService, NetworkMultipleTargetGroupsFargateService, ApplicationLoadBalancedFargateService } from '../../lib';
7+
import { ApplicationLoadBalancedFargateService, ApplicationMultipleTargetGroupsFargateService, NetworkLoadBalancedFargateService, NetworkMultipleTargetGroupsFargateService } from '../../lib';
78

89
describe('When Application Load Balancer', () => {
910
test('test Fargate loadbalanced construct with default settings', () => {
@@ -661,4 +662,75 @@ describe('When Network Load Balancer', () => {
661662
});
662663
}).toThrow(/You must specify one of: taskDefinition or image/);
663664
});
665+
666+
test('test Fargate networkloadbalanced construct with custom Port', () => {
667+
// GIVEN
668+
const stack = new Stack();
669+
const vpc = new Vpc(stack, 'VPC');
670+
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
671+
672+
new NetworkLoadBalancedFargateService(stack, 'NLBService', {
673+
cluster: cluster,
674+
memoryLimitMiB: 1024,
675+
cpu: 512,
676+
taskImageOptions: {
677+
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
678+
containerPort: 81,
679+
},
680+
listenerPort: 8181,
681+
});
682+
683+
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
684+
Port: 81,
685+
Protocol: 'TCP',
686+
TargetType: 'ip',
687+
VpcId: {
688+
Ref: 'VPCB9E5F0B4',
689+
},
690+
});
691+
});
692+
693+
test('test Fargate multinetworkloadbalanced construct with custom Port', () => {
694+
// GIVEN
695+
const stack = new Stack();
696+
const vpc = new Vpc(stack, 'VPC');
697+
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
698+
699+
new NetworkMultipleTargetGroupsFargateService(stack, 'Service', {
700+
cluster,
701+
taskImageOptions: {
702+
image: ecs.ContainerImage.fromRegistry('test'),
703+
},
704+
});
705+
706+
707+
new NetworkMultipleTargetGroupsFargateService(stack, 'NLBService', {
708+
cluster: cluster,
709+
memoryLimitMiB: 1024,
710+
cpu: 512,
711+
taskImageOptions: {
712+
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
713+
},
714+
loadBalancers: [
715+
{
716+
name: 'lb1',
717+
listeners: [
718+
{ name: 'listener1', port: 8181 },
719+
],
720+
},
721+
],
722+
targetGroups: [{
723+
containerPort: 81,
724+
}],
725+
});
726+
727+
Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
728+
Port: 81,
729+
Protocol: 'TCP',
730+
TargetType: 'ip',
731+
VpcId: {
732+
Ref: 'VPCB9E5F0B4',
733+
},
734+
});
735+
});
664736
});

0 commit comments

Comments
 (0)