[英]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.我有一个带有多个 Lambda 的 SAM 应用程序和一些我想在它们之间共享的实用程序代码。 When packaging Lambdas using zip files, code sharing can be done with Lambda Layers.
使用 zip 文件打包 Lambda 时,可以使用 Lambda 层进行代码共享。 However, according to the AWS documentation, Lambda Layers are not supported when using containers.
但是,根据 AWS 文档,使用容器时不支持 Lambda 层。
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
构建容器镜像时,您可以将 package 您首选的运行时和依赖项作为镜像的一部分
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 . 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
.但是,我只是在尝试调试或运行
sam local invoke
时收到Unable to import module 'app': No module named '<my_dependency>'
错误。 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.我在最终图像中验证了依赖项位于我的 python lambda 的
/opt/python/
目录中,并尝试了其他文件夹结构,但没有运气。
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.依赖项(例如,utils.py)确实需要在
/opt/python/
文件夹中,但是当我尝试对此进行测试时,我正在使用 docker cli 手动构建我的 docker 映像,而不是使用sam build
错误的图像标签。 Therefore the debugger used a previous version of my docker image that wasn't working.因此,调试器使用了我的 docker 映像的先前版本,该版本无法正常工作。 To fix the issue I just needed to run
sam build
before trying to debug my lambda locally.为了解决这个问题,我只需要在尝试本地调试 lambda 之前运行
sam build
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.