aws / aws/aws-cdk

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

Open
#29,099 2 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-lambda bug documentation jsii p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the issue

### Example
```py
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](https://docs.python.org/3/library/pathlib.html) as best practice:
```py
code=lambda_.Code.from_asset(
str(Path(__file__).parent / "lambda-handler")
)
```

Also, the default Lambda function's handler in Python is:
```py
handler="lambda_function.lambda_handler"
```

### Links

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

Contributor guide

Open the contributing guide

Research direction

Start with the Python Function API documentation linked in the issue and trace how the JSII-generated Python example is produced. Check how the asset path and default Lambda handler are represented for Python, then verify that the generated example uses pathlib-compatible syntax and the Python handler value shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, node.js, python, typescript
Domain
cloud, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.