question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Referencing local libraries from parent directory fails quietly

See original GitHub issue

Description:

Prior to using SAM, it was common for me to import “internal libraries” into my lambda package by referencing them in the requirements file:

/project
  /shared_lib
    /<code>
    /setup.py
  /lambda_1
    /<code>
    /requirements.txt <- this will contain "../shared_lib"

When I run pip install -r lambda_1/requirements.txt -t deployment_package this will add shared_lib to the deployment_package.

It seems in sam, this is wholly unsupported. When I run sam build, it runs successfully, but the deployment package doesn’t actually contain the shared_lib (so it fails at runtime). It appears that sam is using pip download instead of pip install, which includes the dependencies from shared_lib/setup.py, but not shared_lib itself.

Tangentially: This fails when trying to run in a container (sam build -c), but I care a lot less because at least it fails at build time.

I have a workaround for this where I reference local packages using layers, but it seems pretty awful that this “works” until runtime.

Steps to reproduce the issue:

  1. Setup a library at the same level as your SAM lambda (ie. clone somepackage)
  2. Add the library reference to your SAM lambda requirements.txt (../somepackage)
  3. Run sam build

Observed result:

  • .aws-sam/build/<yourLambdaFunction>/ does not contain somepackage

Expected result:

  • .aws-sam/build/<yourLambdaFunction>/ contains somepackage
  • OR this fails

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wchengrucommented, Mar 11, 2021

Hi @jyoungs , It seems layer is the solution you are looking for. You can package the shared_lib into a zip file and use it. To use layer with SAM CLI, please find this document: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html This issue is more like a SAM CLI issue. I will transfer it to the related repository if you don’t mind: https://github.com/aws/aws-sam-cli.

0reactions
mndevecicommented, May 16, 2022

Resolving this issue since SAM CLI already includes the Lambda Builders with following PR https://github.com/aws/aws-lambda-builders/pull/250

Read more comments on GitHub >

github_iconTop Results From Across the Web

c++ - #include parent directory's file - Stack Overflow
some files in My algorithm directory needs to include definitions from its parent folder. I cannot make all functions templated because the types...
Read more >
Python - Import from parent directory - GeeksforGeeks
Method 1: Import from parent directory using sys.​​ Add the parent directory to the sys. path using the append() method. It is a...
Read more >
Running mypy and managing imports
mypy file_1.py foo/file_2.py file_3.pyi some/directory ... Error: Cannot find implementation or library stub for module named 'does_not_exist' import ...
Read more >
os — Miscellaneous operating system interfaces — Python ...
If dst is a non-empty directory, an OSError is raised. If both are files, dst will be replaced silently if the user has...
Read more >
file — CMake 3.25.1 Documentation
These arguments can be used to exclude unwanted system libraries when resolving the dependencies, or to include libraries from a specific directory.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found