3

I want to add a rule to an existing load balancer listener which is listening on PORT:80. I'm also creating a new target group to attach to the listener rule action. enter image description here

in CDK i used fromLookup and grabbed the listener from ARN

const appListener = elbv2.ApplicationListener.fromLookup(this, `ALBListener-${props.stage}`, { listenerArn });


const applicationListenerRule = new elbv2.ApplicationListenerRule(this, `BlablaSyncRule-${props.stage}`, {
      listener: appListener,
      priority: 1000, //do not hardcode
      conditions: [elbv2.ListenerCondition.pathPatterns(['/socket.io*'])],
      action: elbv2.ListenerAction.forward([targetGroup])
});

when i do cdk synth i can see this included in the generated Cloudformation

  ALBPublicListener9C613A95:
    Type: 'AWS::ElasticLoadBalancingV2::Listener'
    Properties:
      DefaultActions:
        - TargetGroupArn:
            Ref: ALBPublicListenerECSGroup7E4FFE32
          Type: forward
      LoadBalancerArn: >-
        arn:aws:elasticloadbalancing:eu-central-1....
      Port: 80
      Protocol: HTTP
    Metadata:
      'aws:cdk:path': SocketFargateStack/ALB/PublicListener/Resource

When I try to deploy I get the error A listener already exists on this port for this load balancer so is it trying to create a new listener on PORT 80. If so why. Is there a way to add a rule to an existing listener using CDK

enter image description here

Full Stack
Related Github Issue?

cmgchess
  • 7,996
  • 37
  • 44
  • 62
  • It seems like you are actually create load balancer in stack. Can you provide entire stack code? – Lasek Jul 28 '22 at 19:46
  • hi @Lasek https://gist.github.com/cmgchess/e53c876b381b8f4ca7ee53b88535a04c what i'm trying to do is create a service to an existing fargate cluster and add a listener rule to existing listener. the load balancer also i import and the listener also belongs to that – cmgchess Jul 28 '22 at 20:36
  • @Lasek i thnk the ApplicationLoadBalancedFargateService is creating a new listener and thats what causing the issue – cmgchess Jul 29 '22 at 04:57
  • yes it seems right, dont use it then. Use `FargateService` and then use `addTargets` method on listener – Lasek Jul 29 '22 at 06:25
  • @Lasek looks like a lot of work :/ . i guess this is the same issue https://github.com/aws/aws-cdk/issues/13759 – cmgchess Jul 29 '22 at 09:55
  • it is not a lot of work in fact this is how i do it every time. Just few lines more – Lasek Jul 29 '22 at 10:10
  • @Lasek im not sure do we need to create servce, task defnition seperately and so on. how do i attach docker img – cmgchess Jul 29 '22 at 10:14
  • yes it's silly that this construct creates application listener. you you only need 3 costructs here i believe. – Lasek Aug 01 '22 at 22:32

1 Answers1

1

Just writing out this answer from the comments. The source of the listener is actually from the construct ApplicationLoadBalancedFargateService: https://github.com/aws/aws-cdk/blob/v1.201.0/packages/%40aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts#L447-L452

You basically can't use ApplicationLoadBalancedFargateService with an ALB that has existing listeners on the same port, there's an issue for it here: https://github.com/aws/aws-cdk/issues/13759

The way forward is to use FargateService and then use addTargets as @Lasek mentioned in the comments.

Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115