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

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

Closed
jstrunk opened this issue May 31, 2024 · 1 comment · Fixed by #815
Closed

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

jstrunk opened this issue May 31, 2024 · 1 comment · Fixed by #815
Labels
bug Something isn't working needs-triage

Comments

@jstrunk
Copy link
Contributor

jstrunk commented May 31, 2024

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

@jstrunk jstrunk added bug Something isn't working needs-triage labels May 31, 2024
@jstrunk
Copy link
Contributor Author

jstrunk commented May 31, 2024

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

jstrunk added a commit to jstrunk/aws-pdk that referenced this issue Jul 19, 2024
Fixes the issue where generated runtime modules were not packaged
in the Lambda deployment, causing ImportModuleError when executing
Python handlers.

The fix modifies the packaging task in GeneratedPythonHandlersBaseProject
to strip the '-e' flag from exported requirements.txt. This ensures runtime
modules are installed as regular packages instead of editable installations.

Changes:
- Update 'poetry export' command to pipe output through 'sed' and remove
  '-e' prefix.
- Update corresponding snapshot tests for new package task command.

Fixes aws#791
agdimech added a commit that referenced this issue Jul 25, 2024
Fixes the issue where generated runtime modules were not packaged
in the Lambda deployment, causing ImportModuleError when executing
Python handlers.

The fix modifies the packaging task in GeneratedPythonHandlersBaseProject
to strip the '-e' flag from exported requirements.txt. This ensures runtime
modules are installed as regular packages instead of editable installations.

Changes:
- Update 'poetry export' command to pipe output through 'sed' and remove
  '-e' prefix.
- Update corresponding snapshot tests for new package task command.

Fixes #791

Co-authored-by: Adrian Dimech <51220968+agdimech@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant