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.47k stars 3.83k forks source link

(ecs): FargateService - register_load_balancer_target doesn't accept a valid listener when used with python 3.10.6 #25991

Closed Balaji-JBC closed 1 year ago

Balaji-JBC commented 1 year ago

Describe the bug

I try to register load balancer target using register load balancer targets. However, I always get a jsii type error. Intellisense and aws_cdk documentation have right examples

Expected Behavior

If the listener is of valid type. It's not supposed to throw any error

Current Behavior

Even when the listener is valid the following error is thrown TypeError: type of argument listener must be aws_cdk.aws_elasticloadbalancingv2.ApplicationListener; got jsii._reference_map.InterfaceDynamicProxy instead

Reproduction Steps

This error can be reproduced by running the example provided in the aws_cdk documentation itself

# cluster: ecs.Cluster
# task_definition: ecs.TaskDefinition
# vpc: ec2.Vpc

service = ecs.FargateService(self, "Service", cluster=cluster, task_definition=task_definition)

lb = elbv2.ApplicationLoadBalancer(self, "LB", vpc=vpc, internet_facing=True)
listener = lb.add_listener("Listener", port=80)
service.register_load_balancer_targets(
    container_name="web",
    container_port=80,
    new_target_group_id="ECS",
    listener=ecs.ListenerConfig.application_listener(listener,
        protocol=elbv2.ApplicationProtocol.HTTPS
    )
)

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.63.2

Framework Version

No response

Node.js Version

18.16.0

OS

Ubuntu 22.04

Language

Python

Language Version

3.10.6

Other information

No response

peterwoodworth commented 1 year ago

I can't reproduce this @Balaji-JBC, but this isn't a full snippet that will run out of the box since some of the resources aren't truly defined here.

In addition to needing to define some additional resources that this snippet doesn't really do, you'll also find that there's something missing in the snippet. I am pretty sure you need to wrap the props passed into register_load_balancer_target with the ecs.EcsTarget builder like so:

        service.register_load_balancer_targets(ecs.EcsTarget(
            container_name="web",
            container_port=80,
            new_target_group_id="ECS",
            listener=ecs.ListenerConfig.application_listener(listener,
                protocol=elbv2.ApplicationProtocol.HTTPS
            )
        ))

Let me know if this helps, and we can track this as a documentation issue. You may also want to try reinstalling your packages and making sure all the CDK versions are the same, that's a pretty odd error you got for this.

Balaji-JBC commented 1 year ago

Hello @peterwoodworth . The proposed solution also didn't work. I got the same error. Here's the snippet

        https_listener = elb.ApplicationListener.from_application_listener_attributes(
            self, "HTTPSListener",
            listener_arn=https_listener_arn,
            security_group=ec2.SecurityGroup.from_security_group_id(
                self,
                "ListenerSecurityGroup",
                security_group_id=sg_id
            )
        )

        service.register_load_balancer_targets(ecs.EcsTarget(
                container_name="web",
                container_port=80,
                new_target_group_id="ECS",
                listener=ecs.ListenerConfig.application_listener(
                    https_listener,
                    protocol=elb.ApplicationProtocol.HTTPS
                ),
            )
        )

Got the same error


TypeError: type of argument listener must be aws_cdk.aws_elasticloadbalancingv2.ApplicationListener; got jsii._reference_map.Interfa
ceDynamicProxy instead

Edit: I'm using Python 3.10.6. Is that maybe causing the issue?

peterwoodworth commented 1 year ago

Oh, this is because you're importing the ApplicationListener, which returns an IApplicationListener. ListenerConfig.application_listener() requires the full ApplicationListener construct, so this shouldn't work

github-actions[bot] commented 1 year 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.