aws / aws-pdk

The AWS PDK provides building blocks for common patterns together with development tools to manage and build your projects.
https://aws.github.io/aws-pdk/
Apache License 2.0
342 stars 71 forks source link

[BUG] Python Lambda handlers in type-safe-api can't import runtime module #791

Open jstrunk opened 1 month ago

jstrunk commented 1 month ago

Describe the bug

Since the release of Poetry 1.8.3, the Lambda functions can't import the generated runtime modules because they are no longer getting packaged in the zip file.

Expected Behavior

The generated runtime modules are successfully imported when executing the handlers in Lambda.

Current Behavior

[ERROR] Runtime.ImportModuleError: Unable to import module 'api_detective_api_python_handlers.generate_spec': No module named 'api_detective_api_python_runtime'

Locally, it works when I run pytest. However, it looks like the asset in cdk.out installs the runtime package with a pointer to the source path.

Reproduction Steps

Install poetry 1.8.3. Build and deploy a project with a type-safe-api project with Python handlers.

Possible Solution

A workaround would be to use sed to strip the '-e' flag from the exported requirements.txt according to https://github.com/pypa/pip/issues/4812

Additional Information/Context

The latest version of poetry fixed a bug regarding editable installs: https://github.com/python-poetry/poetry-plugin-export/issues/145 .

PDK version used

0.23.38 and 0.23.40

What languages are you seeing this issue on?

Typescript, Python

Environment details (OS name and version, etc.)

MacOS 14.4.1 intel

jstrunk commented 1 month ago

Workaround

const pythonHandlersPackageTask = api.handlers.python?.tasks.tryFind("package");
if (pythonHandlersPackageTask) {
  const writeRequirementsIndex = pythonHandlersPackageTask.steps.findIndex(
    (step) => step.exec?.startsWith("poetry export"),
  );
  if (writeRequirementsIndex !== -1) {
    pythonHandlersPackageTask.updateStep(writeRequirementsIndex, {
      exec: "poetry export --without-hashes --format=requirements.txt | sed -E 's/^-e[[:space:]]+//' > dist/lambda/requirements.txt",
    });
  }
}