aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.72k stars 3.94k forks source link

[aws-elasticloadbalancingv2]: ApplicationLoadBalancer deployment fails when adding ApplicationListener #21048

Closed tuipopenoe closed 2 years ago

tuipopenoe commented 2 years ago

Describe the bug

Creating an ApplicationLoadBalancer and then adding an ApplicationListener as described in the documentation fails as instantiating a new ApplicationLoadBalancer creates a default ApplicationListener on port 80

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_elasticloadbalancingv2.ApplicationLoadBalancer.html

const lb = new elbv2.ApplicationLoadBalancer(this, 'lb', { vpc });
const listener = lb.addListener('listener', { port: 80 });
EcsStack | 9:56:12 PM | CREATE_FAILED        | AWS::ElasticLoadBalancingV2::Listener     | ALB/IALB-Listener-http (ALBListenerhttp) Resource handler returned message: "A listener already exists on this port for this load balancer 'arn:aws:elasticloadbalancing:us-east-1:<account-no>:loadbalancer/app/<app-no>' (Service: ElasticLoadBalancingV2, Status Code: 400, Request ID: <request-id>, Extended Request ID: null)" (RequestToken: <request-token>, HandlerErrorCode: AlreadyExists)

This makes it impossible to add an ASG as a target of the listener, as the default created ApplicationListener is not accessible in

lb.listeners

What is the expected method to add an ASG as a target of the default listener, or override the default listener with a new listener on port 80?

Expected Behavior

ApplicationLoadBalancer to be created, and then able to add an ApplicationListener on port 80.

Current Behavior

Exception occurs during deployment, stating that a listener already exists on port 80 and cannot be added. ApplicationLoadBalancer creates a default ApplicationListener on port 80, that cannot have targets added to it.

Reproduction Steps

const lb = new elbv2.ApplicationLoadBalancer(this, 'lb', { vpc });
const listener = lb.addListener('listener', { port: 80 });

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.27.0 (build 8e89048)

Framework Version

No response

Node.js Version

v18.3.0

OS

Mac OS 12.4

Language

Typescript

Language Version

No response

Other information

No response

corymhall commented 2 years ago

@tuipopenoe I haven't been able to reproduce this on a new project. I've tried both creating a load balancer with the listener and creating the load balancer first and then creating the listener and I was able to create successfully. Is there anything else in your app?

github-actions[bot] commented 2 years ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

tuipopenoe commented 2 years ago

Setup a brand new stack and was unable to replicate, ALB was created successfully. Closing this as no issue.

github-actions[bot] commented 2 years ago

⚠️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.

tuipopenoe commented 2 years ago

The issue was we were using an ApplicationLoadBalancedFargateService (https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs_patterns.ApplicationLoadBalancedFargateService.html) that creates a PublicListener on port 80 by default. Accessing the listener to change the TargetGroup causes an error, so in the end we switched to manually creating an ALB and attaching it to a FargateService, resolving this error.

yamatatsu commented 1 month ago

I encountered the same problem as tuipopenoe and I solved it by temporarily not using redirectHTTP: true. After deploying it, I added redirectHTTP: true back again.