0

I'm building a python application with AWS SAM. The application uses custom lambda layers. It's easy to import functions from these layers when running the lambda handler because of how AWS packages the code. If my_lib is included in the layer, I can add from my_lib import my_func to the lambda handler source code.

These imports obviously don't work in the context of the repository source code because the file structure and python path are different. The imports need to be something along the lines of from src.libs.layers.my_lib import my_func or from ..layers.my_lib import my_func.

Doe anyone have a good approach for managing the imports so that they work when running locally and on AWS? I'd like to be able to:

  1. run pylint in CI without getting E0401 errors
  2. write unit tests that will test functions in the lambda handlers

I'd like to do both of the above without needing an SAM build step which means the import needs to work properly in the source code file structure and the AWS file structure.

One obvious approach would be to use sed on the files prior to the SAM build to update the imports to the format AWS expects, but I'm hoping there's a cleaner solution.

  • This really is not that much about AWS but about how to setup your local projects, setup.py(s) etc. so that the normal non-relative imports work. E.g. https://stackoverflow.com/q/54265944/2442804 – luk2302 Mar 26 '23 at 21:37

1 Answers1

0

I add the appropriate folder to the interpreter path, so my IDE sees a similar folder structure to the lambda function. That avoids the need for any changes to import statements

In this case you'd add /src/libs/layers to your path, so when you import mylib it also checks in /src/lib/layers/mylib

In PyCharm you can add to the path by Settings->Project->Python Interpreter->(Drop Down List)->Show All..., select your interpreter, click the 'Show Interpreter Paths' button (to the right of the filter icon), then click '+'. It's pretty well hidden.

weegolo
  • 354
  • 2
  • 14