简体   繁体   English

部署为 Docker 图像时如何运行多个 lambda 函数?

[英]How to run multiple lambda functions when deploying as a Docker image?

How does the dockerfile look like for aws lambda with docker image via aws-sam when declaring multiple functions/apps in templates.yaml ?当在templates.yaml中声明多个函数/应用程序时,dockerfile 对于 aws lambda 和dockerfile图像通过 aws-sam 看起来如何?

Here is the sample dockerfile to run "a single app"这是运行“单个应用程序”的示例dockerfile

FROM public.ecr.aws/lambda/python:3.8

COPY app.py requirements.txt ./

RUN python3.8 -m pip install -r requirements.txt -t .

# Command can be overwritten by providing a different command in the template directly.
CMD ["app.lambda_handler"]

The Dockerfile itself looks the same. Dockerfile本身看起来是一样的。 No changes needed there.那里不需要改变。

The presence of the CMD line in the Docker file looks like it needs to change, but that is misleading. Docker 文件中CMD行的存在看起来需要更改,但这是一种误导。 The CMD value can be specified on a per-function basis in the template.yaml file.可以在template.yaml文件中基于每个函数指定CMD值。

The template.yaml file must be updated with information about the new function. You will need to add an ImageConfig property to each function. The ImageConfig property must specify the name of the function in the same way the CMD value otherwise would have done so.必须使用有关新 function 的信息更新template.yaml文件。您需要为每个 function 添加一个ImageConfig属性ImageConfig属性必须以与CMD值相同的方式指定 function 的名称,否则会这样做。

You will also need to update each function's DockerTag value to be unique, though this may be a bug .您还需要将每个函数的DockerTag值更新为唯一的,尽管这可能是一个错误

Here's the NodeJs "Hello World" example template.yaml's Resources section, updated to support multiple functions with a single Docker image:这是 NodeJs“Hello World”示例 template.yaml 的资源部分,已更新以支持单个 Docker 图像的多个功能:

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageConfig:
        Command: [ "app.lambdaHandler" ]
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get
    Metadata:
      DockerTag: nodejs14.x-v1-1
      DockerContext: ./hello-world
      Dockerfile: Dockerfile
  HelloWorldFunction2:
    Type: AWS::Serverless::Function
    Properties:
      PackageType: Image
      ImageConfig:
        Command: [ "app.lambdaHandler2" ]
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello2
            Method: get
    Metadata:
      DockerTag: nodejs14.x-v1-2
      DockerContext: ./hello-world
      Dockerfile: Dockerfile

This assumes the app.js file has been modified to provide both exports.lambdaHandler and exports.lambdaHandler2 .这假设app.js文件已被修改为同时提供exports.lambdaHandlerexports.lambdaHandler2 I assume the corresponding python file should be modified similarly.我假设相应的 python 文件应该进行类似的修改。

After updating template.yaml in this way, sam local start-api works as expected, routing /hello to lambdaHandler and /hello2 to lambdaHandler2 .以这种方式更新template.yaml后, sam local start-api将按预期工作,将/hello路由到lambdaHandler并将/hello2路由到lambdaHandler2

This technically creates two separate Docker images (one for each distinct DockerTag value).这在技术上创建了两个单独的 Docker 图像(每个图像对应一个不同的DockerTag值)。 However, the two images will be identical save for the tag, and based on the same Dockerfile , and the second image will therefore make use of Docker's cache of the first image.然而,除了标签之外,这两个图像将是相同的,并且基于相同的Dockerfile ,因此第二个图像将使用第一个图像的 Docker 缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 docker 图像中运行 lambda - Unable to run lambda in docker image 将 docker 镜像部署到云运行时如何解决“容器启动失败错误” - How to resolve "container failed to start error" while deploying a docker image to cloud run 在 Google Cloud Run 中部署多阶段 Docker 映像 - Deploying multi-stage Docker image in Google Cloud Run Lambda Docker 图像未运行 - Lambda Docker Image Not Running 如何在 Lambda 处理程序中处理多个异步函数? - How to handle multiple async functions in a Lambda handler? 在 Lambda 中使用多个函数 - Use multiple functions in Lambda 基于Docker Image执行Lambda时的Runtime.InvalidEntrypoint - Runtime.InvalidEntrypoint when executing Lambda based on Docker Image 部署 firebase 函数时如何修复“ERESOLVE 无法解析” - How to fix "ERESOLVE could not resolve" when deploying firebase functions 如何使用 docker 图像在 aws lambda 中安装 gifsicle - How to install gifsicle in aws lambda using docker image 如何在云端使用调整图像 lambda 边缘函数修复 503 错误? - How to fix 503 error with resize image lambda edge functions on cloudfront?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM