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

aws-lambda: aws_lambda.Function.from_function_arn returns FunctionBaseProxy instead of IFunction object #4657

Closed
1 task
arutz12 opened this issue Oct 10, 2024 · 13 comments
Assignees
Labels
effort/medium Medium work item – a couple days of effort language/python Related to Python bindings p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. wontfix We have determined that we will not resolve the issue.

Comments

@arutz12
Copy link

arutz12 commented Oct 10, 2024

Describe the bug

CDK: 2.161.1
Python: 3.9.18 (and 3.11.7)
aws-solutions-constructs.aws-eventbridge-lambda: 2.72.0

aws_lambda.Function.from_function_arn returns FunctionBaseProxy instead of IFunction as documented.
Therefore the object fails when typechecked in the aws-solutions-constructs.aws-eventbridge-lambda module:

Regression Issue

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

Last Known Working CDK Version

No response

Expected Behavior

Function returns object of type IFunction as documented.

Current Behavior

Function returned FunctionBaseProxy object.

Reproduction Steps

#!/usr/bin/env python3

from constructs import Construct
import aws_cdk as cdk
from aws_cdk import aws_lambda, aws_events
from aws_solutions_constructs import aws_eventbridge_lambda

class LambdaFunctionTest(cdk.Stack):

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

        function_arn = f"arn:aws:lambda:us-east-1:1234567890:function:LambdaFunctionName"

        event_lambda = aws_lambda.Function.from_function_arn(self, "LambdaFunction", function_arn)

        event_to_lambda = aws_eventbridge_lambda.EventbridgeToLambda(
            scope=self,
            id="EventToLambda",
            event_rule_props=aws_events.RuleProps(),
            existing_lambda_obj=event_lambda
        )


app = cdk.App()
LambdaFunctionTest(app, "LambdaFunctionTest")
app.synth()


Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.161.1

Framework Version

No response

Node.js Version

v18.17.1

OS

KDE neon 6.2 (Ubuntu 22.04)

Language

Python

Language Version

3.9.18 (also 3.11.7)

Other information

Running synth on the code:

Traceback (most recent call last):
  File "/tmp/lambda_test/app.py", line 26, in <module>
    LambdaFunctionTest(app, "LambdaFunctionTest")
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
  File "/tmp/lambda_test/app.py", line 17, in __init__
    event_to_lambda = aws_eventbridge_lambda.EventbridgeToLambda(
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/jsii/_runtime.py", line 118, in __call__
    inst = super(JSIIMeta, cast(JSIIMeta, cls)).__call__(*args, **kwargs)
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/aws_solutions_constructs/aws_eventbridge_lambda/__init__.py", line 214, in __init__
    props = EventbridgeToLambdaProps(
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/aws_solutions_constructs/aws_eventbridge_lambda/__init__.py", line 281, in __init__
    check_type(argname="argument existing_lambda_obj", value=existing_lambda_obj, expected_type=type_hints["existing_lambda_obj"])
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/aws_solutions_constructs/aws_eventbridge_lambda/__init__.py", line 164, in check_type
    return typeguard.check_type(argname=argname, value=value, expected_type=expected_type) # type:ignore
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/typeguard/__init__.py", line 757, in check_type
    checker_func(argname, value, expected_type, memo)
  File "/tmp/lambda_test/.venv/lib/python3.9/site-packages/typeguard/__init__.py", line 558, in check_union
    raise TypeError('type of {} must be one of ({}); got {} instead'.
TypeError: type of argument existing_lambda_obj must be one of (aws_cdk.aws_lambda.Function, NoneType); got aws_cdk.aws_lambda._FunctionBaseProxy instead

@arutz12 arutz12 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 10, 2024
@arutz12 arutz12 changed the title aws-lambda: aws_lambda.Function.from_function_arn returns FunctionBaseProxy instead of IFunction object aws-lambda: aws_lambda.Function.from_function_arn returns _FunctionBaseProxy_ instead of _IFunction_ object Oct 10, 2024
@arutz12 arutz12 changed the title aws-lambda: aws_lambda.Function.from_function_arn returns _FunctionBaseProxy_ instead of _IFunction_ object aws-lambda: aws_lambda.Function.from_function_arn returns FunctionBaseProxy instead of IFunction object Oct 10, 2024
@khushail khushail added needs-reproduction This issue needs reproduction. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Oct 10, 2024
@khushail khushail self-assigned this Oct 10, 2024
@kmonihen
Copy link

I'm having this same issue and it seems to be caused by an upgrade to typeguard. If I pin typeguard to 2.13.3 I don't get the error.

I had 4.3.0 installed before pinning to 2.13.3:

Installing collected packages: typeguard
  Attempting uninstall: typeguard
    Found existing installation: typeguard 4.3.0
    Uninstalling typeguard-4.3.0:
      Successfully uninstalled typeguard-4.3.0
Successfully installed typeguard-2.13.3

@medley56
Copy link

medley56 commented Oct 10, 2024

I'm having a similar issue with the recent typeguard changes. 2.13.3 works for me but if I upgrade to v3 I get a bunch of warnings and v4 actually prevents cdk synthesis because sns_subscriptions.LambdaSubscription(function) expects an IFunction, which a DockerImageFunction apparently is not.

This happened between CDK version 2.157 and 2.158 where the typeguard dependency was incremented drastically from 2.13.3 to 4.3.0.

@joshua-haunty
Copy link

Trying to avoid saying the same thing, but this issue is reproducable on python 3.11 as well. When creating a lambda function, the layers supposedly are not of type ILayerVersion and the deploy fails.

typeguard.TypeCheckError: list did not match any element in the union:
  Sequence[aws_cdk.aws_lambda.ILayerVersion]: item 0 is not compatible with the ILayerVersion protocol because it has no method named '__jsii_proxy_class__'

Setting typeguard==2.13.3 mitigates.

@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-reproduction This issue needs reproduction. labels Oct 10, 2024
@khushail
Copy link
Contributor

khushail commented Oct 10, 2024

@arutz12 , thanks for reaching out.

Sharing my analysis -

  1. On checking the implementation of fromFunctionArn, which internally invokes fromFunctionAttributes(), with return of IFunction object-

https://github.com/aws/aws-cdk/blob/048975e75e718eb980cf33eb47bea04be095c585/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L721

https://github.com/aws/aws-cdk/blob/048975e75e718eb980cf33eb47bea04be095c585/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L721C1-L723C3

  public static fromFunctionArn(scope: Construct, id: string, functionArn: string): IFunction {
    return Function.fromFunctionAttributes(scope, id, { functionArn });
  }
 
  1. fromFunctionAttributes() returns IFunction -

https://github.com/aws/aws-cdk/blob/048975e75e718eb980cf33eb47bea04be095c585/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L736

  1. this IFunction is actually from the Function-base-

https://github.com/aws/aws-cdk/blob/048975e75e718eb980cf33eb47bea04be095c585/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L10C64-L10C72

However I am trying to repro the scenario.

@khushail
Copy link
Contributor

khushail commented Oct 10, 2024

As documented in cdk docs, the fromFunctionArn should return IFunction object -https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda.Function.html#static-fromwbrfunctionwbrarnscope-id-functionarn

@khushail khushail added p1 and removed p2 labels Oct 10, 2024
@khushail
Copy link
Contributor

This seems like a JSII issue, so moving to JSII Repo for further action.

@khushail khushail added effort/medium Medium work item – a couple days of effort and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Oct 11, 2024
@khushail khushail transferred this issue from aws/aws-cdk Oct 11, 2024
@khushail khushail removed their assignment Oct 11, 2024
@mrgrain
Copy link
Contributor

mrgrain commented Oct 11, 2024

Hey @arutz12 Thanks for reporting this issue. While there are some current ongoings with jsii in this area (like @khushail confirmed), I don't think the issue you are running into is that.

aws-solutions-constructs.aws-eventbridge-lambda defines existing_lambda_obj as lambda.Function (see:
https://docs.aws.amazon.com/solutions/latest/constructs/aws-eventbridge-lambda.html#pattern-properties)

However you are trying to give it the result of from_function_arn, which is an IFunction. That is never going to work. Not sure if aws-solutions-constructs could offer something that fits your needs better.

@mrgrain mrgrain added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Oct 11, 2024
@mrgrain mrgrain self-assigned this Oct 11, 2024
@kmonihen
Copy link

https://github.com/aws/constructs/releases/tag/v10.3.2
They added a workaround to the constructs package in the latest release. I confirmed constructs 10.3.2 installs typeguard~=2.13.3 and the code synths without error now.

It looks like they will address this issue in the constructs repo, but if you need more code that fails with typeguard on 4.3.0 I was using this code to replicate the error:

from aws_cdk import App, Stack
from aws_cdk import aws_lambda, aws_s3
from aws_cdk import aws_s3_notifications as s3_notify

app = App()
stack = Stack(app, "bucket-stack")

bucket = aws_s3.Bucket(stack, "Bucket")

bucket.add_event_notification(
    event=aws_s3.EventType.OBJECT_CREATED,
    dest=s3_notify.LambdaDestination(
        aws_lambda.Function.from_function_arn(
            stack,
            "ObjectCreatedLambda",
            "arn:aws:lambda:us-west-2:460106496004:function:ObjectCreatedLambda-qQkkxlUaClaJ",
        )
    ),
)

app.synth()

@mrgrain
Copy link
Contributor

mrgrain commented Oct 11, 2024

Thanks @kmonihen More reproduction code is always helpful. Your example should work indeed because LambdaDestination() expects and IFunction and Function.from_function_arn() returns an IFunction.

It does seem that the old version of typeguard incorrectly does NOT catch the type-missmatch in the original report. Because the types of EventbridgeToLambda() and Function.from_function_arn() are just not compatible.

@arutz12
Copy link
Author

arutz12 commented Oct 11, 2024

Hey @arutz12 Thanks for reporting this issue. While there are some current ongoings with jsii in this area (like @khushail confirmed), I don't think the issue you are running into is that.

aws-solutions-constructs.aws-eventbridge-lambda defines existing_lambda_obj as lambda.Function (see: https://docs.aws.amazon.com/solutions/latest/constructs/aws-eventbridge-lambda.html#pattern-properties)

However you are trying to give it the result of from_function_arn, which is an IFunction. That is never going to work. Not sure if aws-solutions-constructs could offer something that fits your needs better.

So even if the issue mentioned above (proper return values for from_function_arn) is fixed this complex construct can only be implemented in the same stack, so I have to look for another solution in my case.
Thanks @mrgrain for the clarification.

@mrgrain
Copy link
Contributor

mrgrain commented Oct 11, 2024

Yes, I that's about it. You can try reaching out to aws-solutions-constructs if they would be willing to support your use case.

Closing this. The open jsii issue is tracked here #4658

@mrgrain mrgrain closed this as completed Oct 11, 2024
@mrgrain mrgrain closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2024
Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

1 similar comment
Copy link
Contributor

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort/medium Medium work item – a couple days of effort language/python Related to Python bindings p1 response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. wontfix We have determined that we will not resolve the issue.
Projects
None yet
Development

No branches or pull requests

6 participants