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.38k stars 3.79k forks source link

aws_cdk.aws_lambda.Function: Python's `Function` uses Node.js' `__dirname` #29099

Open garysassano opened 5 months ago

garysassano commented 5 months ago

Describe the issue

Example

import aws_cdk.aws_signer as signer

signing_profile = signer.SigningProfile(self, "SigningProfile",
    platform=signer.Platform.AWS_LAMBDA_SHA384_ECDSA
)

code_signing_config = lambda_.CodeSigningConfig(self, "CodeSigningConfig",
    signing_profiles=[signing_profile]
)

lambda_.Function(self, "Function",
    code_signing_config=code_signing_config,
    runtime=lambda_.Runtime.NODEJS_18_X,
    handler="index.handler",
    code=lambda_.Code.from_asset(path.join(__dirname, "lambda-handler"))
)

The JSII's autogenerated code uses Node.js' __dirname, which isn't valid in Python.

Ideally, you should want to use pathlib as best practice:

code=lambda_.Code.from_asset(
  str(Path(__file__).parent / "lambda-handler")
)

Also, the default Lambda function's handler in Python is:

handler="lambda_function.lambda_handler"

Links

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_lambda/Function.html

pahud commented 5 months ago

I guess this might be a bug from JSII as the sample is auto generated from the one in TypeScript. Thank you for the report.

gshpychka commented 5 months ago

jsii-rosetta, rather