2

I have a SAM app with multiple Lambdas and some utility code I'd like to share between them. When packaging Lambdas using zip files, code sharing can be done with Lambda Layers. However, according to the AWS documentation, Lambda Layers are not supported when using containers.

Functions defined as container images do not support layers. When you build a container image, you can package your preferred runtimes and dependencies as a part of the image

I've tried copying my dependencies into a separate docker image, then pulling from that image in my lambda dockerfile, which is suggested in the AWS blog post Working with Lambda layers and extensions in container images. However, I just get the Unable to import module 'app': No module named '<my_dependency>' error when trying to debug or run sam local invoke. I verified in my final image that the dependencies are in the /opt/python/ directory for my python lambda, and tried other folder structure as well, but no luck.

Has anyone been able to get this to work?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

0

I've figured this out. Dependencies (eg, utils.py) do need to be in the /opt/python/ folder, but when I was trying to test this out, I was manually building my docker image with the docker cli instead of using sam build and I had the wrong image tag. Therefore the debugger used a previous version of my docker image that wasn't working. To fix the issue I just needed to run sam build before trying to debug my lambda locally.

  • I am encountering a need to have a container image as lambda with a significant size dependency (over 250 MB). I want to share this significant size dependency with other lambdas. By following this and having the container layer in the `/opt` directory work just like the lambda layer, which can be used by other lambdas? – XYZ Apr 26 '23 at 06:10