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