简体   繁体   English

使用 Docker 容器在 SAM 应用程序中的多个 AWS Lambda 之间共享代码

[英]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.

相关问题 在Docker容器中的phpfpm和nginx之间共享应用程序文件 - Sharing app files between phpfpm and nginx in docker containers 如何将具有多个 Docker 容器的应用程序部署到 AWS Elastic Beanstalk? - How to deploy an app with multiple Docker containers to AWS Elastic Beanstalk? 多个 docker 容器和 AWS scallinguUp - Multiple docker containers and scallinguUp with AWS 使用 AWS Lightstail 容器的 Docker Compose 和 Django 应用程序无法部署 - Docker Compose and Django app using AWS Lighstail containers fail to deploy 使用SAM时AWS sam build和sam package有什么区别? - What's the difference between AWS sam build and sam package when using SAM? 如何在AWS ElasticBeanstalk中连接多个Docker容器? - How to connect multiple Docker containers in AWS ElasticBeanstalk? AWS Lambda之间的同步 - Synchronization between aws lambdas 使用 sam init 部署 AWS Lambda“Hello World”,sam build sam deploy defaults 失败,无法为 sam-app 创建变更集 - Deploying AWS Lambda "Hello World" using sam init, sam build sam deploy defaults fails with Failed to create changeset for sam-app 如何使用AWS Beanstalk和Spring Cloud Netflix在Docker容器之间建立连接 - How to set up connection between Docker containers using AWS Beanstalk and Spring Cloud Netflix 将 CORS 与 AWS SAM 结合使用 - Using CORS with AWS SAM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM