aws/aws-pdk

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

Closed this issue · 1 comments

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 pypa/pip#4812

Additional Information/Context

The latest version of poetry fixed a bug regarding editable installs: python-poetry/poetry-plugin-export#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

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",
    });
  }
}