【问题标题】:How to run multiple lambda functions when deploying as a Docker image?部署为 Docker 映像时如何运行多个 lambda 函数?
【发布时间】:2021-02-24 06:26:41
【问题描述】:

templates.yaml 中声明多个函数/应用程序时,通过 aws-sam 使用 docker 映像的 aws lambda 的 dockerfile 看起来如何?

这是运行“单个应用程序”的示例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"]

【问题讨论】:

    标签: aws-lambda aws-sam aws-sam-cli


    【解决方案1】:

    Dockerfile 本身看起来是一样的。那里不需要更改。

    Docker 文件中的CMD 行看起来需要更改,但这是一种误导。 CMD 值可以在 template.yaml 文件中按功能指定。

    template.yaml 文件必须使用有关新功能的信息进行更新。您需要为每个函数添加一个ImageConfig 属性。 ImageConfig 属性必须以与 CMD 值相同的方式指定函数的名称,否则会这样做。

    您还需要将每个函数的 DockerTag 值更新为唯一的,尽管这是 may be a bug

    这是 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
    

    这假设 app.js 文件已被修改为同时提供 exports.lambdaHandlerexports.lambdaHandler2。我假设相应的python文件应该类似地修改。

    以这种方式更新template.yaml后,sam local start-api按预期工作,将/hello路由到lambdaHandler/hello2lambdaHandler2

    这在技术上创建了两个独立的 Docker 映像(每个不同的 DockerTag 值一个)。但是,两个镜像除了标签之外是相同的,并且基于相同的Dockerfile,因此第二个镜像将使用 Docker 对第一个镜像的缓存。

    【讨论】:

    • 这实际上创建了两个 docker 图像标签,从长远来看这将难以管理。希望 aws sam 团队能尽快解决这些问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2021-10-15
    • 2021-09-20
    • 1970-01-01
    • 2021-07-02
    • 2021-04-12
    • 1970-01-01
    相关资源
    最近更新 更多