简体   繁体   中英

Sharing code between multiple AWS Lambdas in a SAM app using Docker Containers

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM