Skip to content

(ecs-patterns): NetworkLoadBalancedServiceBase and NetworkMultipleTargetGroupsServiceBase hardcode port 80 on target group #18073

Closed
@LukvonStrom

Description

@LukvonStrom
Contributor

What is the problem?

As stated in the title, both base classes default to port 80 for the Loadbalancer TargetGroup.
The culprit for NetworkLoadBalancedServiceBase is located here: https://github.com/aws/aws-cdk/blob/v2-main/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts#L345

it needs to be changed to something like this:

const targetProps = {
      port: props.taskImageOptions?.containerPort ?? 80,
    };

The culprit for NetworkMultipleTargetGroupsServiceBase is located here: https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-ecs-patterns/lib/base/network-multiple-target-groups-service-base.ts#L377

it needs to be

        port: targetProps.containerPort ?? 80,

to result in

{
      "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties": {
        "Port": [Custom Port],
        "Protocol": "TCP",
      },
    }

Reproduction Steps

I will provide a sample for the NetworkLoadBalancedServiceBase bug:

import { Stack, StackProps } from 'aws-cdk-lib';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { Cluster, ContainerImage } from 'aws-cdk-lib/aws-ecs';
import { NetworkLoadBalancedFargateService } from 'aws-cdk-lib/aws-ecs-patterns';
import { Construct } from 'constructs';

export class ElbTestStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
    const vpc = new Vpc(this, 'vpc', {});

    const fargateCluster = new Cluster(this, 'FarGate-Cluster', {
      clusterName: 'test',
      vpc: vpc,
      enableFargateCapacityProviders: true,
      containerInsights: true
    })

    const loadBalancedFargateService = new NetworkLoadBalancedFargateService(this, 'NLBService', {
      cluster: fargateCluster,
      memoryLimitMiB: 1024,
      cpu: 512,
      taskImageOptions: {
        image: ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
        containerPort: 81
      },
      listenerPort: 8181,
    });

  }
}

What did you expect to happen?

I expected the Target Group to look like this:

"NLBServiceLBPublicListenerECSGroup1257E89B": {
      "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties": {
        "Port": 81,
(snip)
      },
      "Metadata": {
        "aws:cdk:path": "ElbTestStack/NLBService/LB/PublicListener/ECSGroup/Resource"
      }
    }

What actually happened?

The Target Group looked like this:

"NLBServiceLBPublicListenerECSGroup1257E89B": {
      "Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
      "Properties": {
        "Port": 80,
(snip)
      },
      "Metadata": {
        "aws:cdk:path": "ElbTestStack/NLBService/LB/PublicListener/ECSGroup/Resource"
      }
    }

when implementing

const targetProps = {
      port: props.taskImageOptions?.containerPort ?? 80,
    };

the bug was fixed.

CDK CLI Version

2.2.0 (build 4f5c27c)

Framework Version

No response

Node.js Version

v16.13.0

OS

WSL 2 on Ubuntu

Language

Typescript

Language Version

No response

Other information

No response

Activity

added
bugThis issue is a bug.
needs-triageThis issue or PR still needs to be triaged.
on Dec 17, 2021
changed the title [-](ecs-patterns): NetworkLoadBalancedServiceBase and NetworkMultipleTargetGroupsServiceBase default to port 80 on target group[/-] [+](ecs-patterns): NetworkLoadBalancedServiceBase and NetworkMultipleTargetGroupsServiceBase hardcode port 80 on target group[/+] on Dec 17, 2021
removed their assignment
on Jan 19, 2022
added a commit that references this issue on Jan 21, 2022
1393729
github-actions

github-actions commented on Jan 21, 2022

@github-actions
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

added a commit that references this issue on Feb 21, 2022
e30b5d5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @LukvonStrom@peterwoodworth@madeline-k

      Issue actions

        (ecs-patterns): NetworkLoadBalancedServiceBase and NetworkMultipleTargetGroupsServiceBase hardcode port 80 on target group · Issue #18073 · aws/aws-cdk