Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elasticloadbalancingv2:Cannot construct AlbListenerTarget using lookUp #33220

Open
1 task
gratinierer opened this issue Jan 29, 2025 · 3 comments
Open
1 task
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@gratinierer
Copy link

Describe the bug

Constructing AlbListenerTarget with Listener fromLookup fails:

alb = aws_cdk.aws_elasticloadbalancingv2.ApplicationLoadBalancer.from_lookup(
            self,
            'ApplicationBalancer',
            load_balancer_tags={
                'elasticbeanstalk:environment-name': 'myenv'})
logging.info(f'found alb with arn <{alb.load_balancer_arn}>')
aws_elasticloadbalancingv2_targets.AlbTarget(alb=alb, port=80) 
listener = ApplicationListener.from_lookup(self, 'ApplicationListener',
                                                   load_balancer_arn=alb.load_balancer_arn, listener_port=80)
logging.info(f'found listener with arn <{listener.listener_arn}>')
alb_target = aws_elasticloadbalancingv2_targets.AlbListenerTarget(listener)

==>

2025-01-29 05:32:38 INFO     found alb with arn <arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/application/my-load-balancer/50dc6c495c0c9188>
2025-01-29 05:32:38 INFO     found listener with arn <arn:aws:elasticloadbalancing:us-west-2:123456789012:listener/application/my-load-balancer/50dc6c495c0c9188/f2f7dc8efc522ab2>
(....)py", line 62, in get_alb_listener
    alb_target = aws_elasticloadbalancingv2_targets.AlbListenerTarget(listener)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.pyenv/versions/3.11.11/lib/python3.11/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.pyenv/versions/3.11.11/lib/python3.11/site-packages/aws_cdk/aws_elasticloadbalancingv2_targets/__init__.py", line 157, in __init__
    check_type(argname="argument alb_listener", value=alb_listener, expected_type=type_hints["alb_listener"])
  File "/root/.pyenv/versions/3.11.11/lib/python3.11/site-packages/aws_cdk/aws_elasticloadbalancingv2_targets/__init__.py", line 25, in check_type
    return typeguard.check_type(argname=argname, value=value, expected_type=expected_type) # type:ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.pyenv/versions/3.11.11/lib/python3.11/site-packages/typeguard/__init__.py", line 785, in check_type
    raise TypeError(
TypeError: type of argument alb_listener must be aws_cdk.aws_elasticloadbalancingv2.ApplicationListener; got jsii._reference_map.InterfaceDynamicProxy instead

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

it should be possible to construct the listener.

Current Behavior

cosntruction fails due to type-problems.

Reproduction Steps

it should be possible to reproduce with the code above.
we have to switch wo AlbListenerTarget because of deprecation of AlbTarget.
As you can see in the code AlbTarget works (in production here) fine with the 'ApplicationLoadBalancer.from_lookup', so the ALB and also the listeners are in place.

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.166.0

Framework Version

No response

Node.js Version

20.15.0

OS

Linux

Language

Python

Language Version

No response

Other information

No response

@gratinierer gratinierer added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 29, 2025
@github-actions github-actions bot added the @aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 label Jan 29, 2025
@pahud pahud self-assigned this Jan 29, 2025
@pahud
Copy link
Contributor

pahud commented Jan 29, 2025

reproducible in 2.177.0

from aws_cdk import (
    Stack,
    aws_ec2 as ec2,
    aws_elasticloadbalancingv2 as elbv2,
    aws_elasticloadbalancingv2_targets as elbv2_targets,
)
from constructs import Construct
import logging

class IssueTriagePyStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Get the default VPC
        vpc = ec2.Vpc.from_lookup(self, "VPC", is_default=True)

        # Create Application Load Balancer
        alb = elbv2.ApplicationLoadBalancer(
            self, "MyALB",
            vpc=vpc,
            internet_facing=True
        )

        # Create Target Group
        target_group = elbv2.ApplicationTargetGroup(
            self, "MyTargetGroup",
            vpc=vpc,
            port=80,
            protocol=elbv2.ApplicationProtocol.HTTP,
            target_type=elbv2.TargetType.INSTANCE
        )

        # Add Listener
        listener = alb.add_listener(
            "MyListener",
            port=80,
            default_target_groups=[target_group]
        )

        # add import statements below
        alb = elbv2.ApplicationLoadBalancer.from_lookup(
            self, 'ApplicationBalancer',
            load_balancer_tags={'elasticbeanstalk:environment-name': 'myenv'}
        )
        logging.info(f'found alb with arn <{alb.load_balancer_arn}>')

        elbv2_targets.AlbTarget(alb=alb, port=80)

        listener = elbv2.ApplicationListener.from_lookup(
            self, 'ApplicationListener',
            load_balancer_arn=alb.load_balancer_arn,
            listener_port=80
        )
        logging.info(f'found listener with arn <{listener.listener_arn}>')

        alb_target = elbv2_targets.AlbListenerTarget(listener)
% cdk diff
[WARNING] aws-cdk-lib.aws_elasticloadbalancingv2_targets.AlbTarget is deprecated.
  Use `AlbListenerTarget` instead or
`AlbArnTarget` for an imported load balancer. This target does not automatically
add a dependency between the ALB listener and resulting NLB target group,
without which may cause stack deployments to fail if the NLB target group is provisioned
before the listener has been fully created.
  This API will be removed in the next major release.
Traceback (most recent call last):
  File "/Users/hunhsieh/repos/issue-triage-py/app.py", line 10, in <module>
    IssueTriagePyStack(app, "IssueTriagePyStack",
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
        # If you don't specify 'env', this stack will be environment-agnostic.
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<13 lines>...
        # For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        )
        ^
  File "/Users/hunhsieh/repos/issue-triage-py/.venv/lib/python3.13/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
  File "/Users/hunhsieh/repos/issue-triage-py/issue_triage_py/issue_triage_py_stack.py", line 57, in __init__
    alb_target = elbv2_targets.AlbListenerTarget(listener)
  File "/Users/hunhsieh/repos/issue-triage-py/.venv/lib/python3.13/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
  File "/Users/hunhsieh/repos/issue-triage-py/.venv/lib/python3.13/site-packages/aws_cdk/aws_elasticloadbalancingv2_targets/__init__.py", line 157, in __init__
    check_type(argname="argument alb_listener", value=alb_listener, expected_type=type_hints["alb_listener"])
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hunhsieh/repos/issue-triage-py/.venv/lib/python3.13/site-packages/aws_cdk/aws_elasticloadbalancingv2_targets/__init__.py", line 25, in check_type
    return typeguard.check_type(argname=argname, value=value, expected_type=expected_type) # type:ignore
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/hunhsieh/repos/issue-triage-py/.venv/lib/python3.13/site-packages/typeguard/__init__.py", line 785, in check_type
    raise TypeError(
        'type of {} must be {}; got {} instead'.
        format(argname, qualified_name(expected_type), qualified_name(value)))
TypeError: type of argument alb_listener must be aws_cdk.aws_elasticloadbalancingv2.ApplicationListener; got jsii._reference_map.InterfaceDynamicProxy instead
Subprocess exited with error 1

I'll check if this issue exists in TS later.

@pahud
Copy link
Contributor

pahud commented Jan 29, 2025

OK I see the gap here.

If we check the TS doc, AlbListenerTarget requires a ApplicationListener typed argument while elbv2.ApplicationListener.fromLookup() essentially returns the interface, not a concrete ApplicationListener. I can see this error in TypeScript

Image

Now, looking at CDK in python on AlbListenerTarget, it requires

Parameters
:
alb_listener (ApplicationListener) – The application load balancer listener to target.

but if you check the from_lookup method:

Return type
:
IApplicationListener

So I think you can't simply create an AlbListenerTarget() using an imported listener like that.

@pahud pahud added p3 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 29, 2025
@pahud pahud removed their assignment Jan 29, 2025
@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jan 29, 2025
@gratinierer
Copy link
Author

gratinierer commented Jan 29, 2025

Thanks for checking, pahud.
It may be technically true, that I can‘t do that at the moment, but with the deprecation of AlbTaget we will be forced to use AlbListenerTarget to keep our code running.
What would be the migation-path for that?

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-elasticloadbalancingv2 Related to Amazon Elastic Load Balancing V2 bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
Development

No branches or pull requests

2 participants