英文:
How to configure the entrypoint/cmd for docker-based python3 lambda functions?
问题
I switched from a zip-based deployment to a docker-based deployment of two lambda functions (which are used in an API Gateway). Both functions where in the same zip file and I want to have both functions in the same docker-based container (meaning I can't use the cmd
setting in my Dockerfile (or to be precise need to overwrite it anyway). Previously, I used the handler attribute in the cloudformation template for specifying which handler function to call in which module, e.g.
...
ConfigLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: config.handler
...
...
LogLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: logs.handler
...
but with a docker-based build one has to define an ImageConfig
, i.e.
...
LogLambda:
Type: 'AWS::Serverless::Function'
Properties:
PackageType: Image
ImageUri: !Ref EcrImageUri
FunctionName: !Sub "${AWS::StackName}-Logs"
ImageConfig:
WorkingDirectory: /var/task
Command: ['logs.py']
EntryPoint: ['/var/lang/bin/python3']
...
ConfigLambda:
Type: 'AWS::Serverless::Function'
Properties:
PackageType: Image
ImageUri: !Ref EcrImageUri
FunctionName: !Sub "${AWS::StackName}-Config"
ImageConfig:
WorkingDirectory: /var/task
Command: ['config.py']
EntryPoint: ['/var/lang/bin/python3']
I'm a bit stuck because this does not work, no matter what combination I pass to the command array. If I fire a test event in the AWS console, I get the following error
RequestId: <uuid> Error: Runtime exited without providing a reason
Runtime.ExitError
Judging from the full output, the file is loaded and executed, but the handler function is not invoked (there is some output from a logging setup function which is called right after the module imports). The section in the AWS documentation on python3 based lambdas state that naming for handlers should be file_name.function (e.g. function_lambda.lambda_handler), but this doesn't give any clues on how do to this for command array in an ImageConfig.
How do I set the Command section correctly for my lambda function in my CloudFormation template?
英文:
I switched from a zip-based deployment to a docker-based deployment of two lambda functions (which are used in an API Gateway). Both functions where in the same zip file and I want to have both functions in the same docker-based container (meaning I can't use the cmd
setting in my Dockerfile (or to be precise need to overwrite it anyway). Previously, I used the handler attribute in the cloudformation template for specifying which handler function to call in which module, e.g.
...
ConfigLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: config.handler
...
...
LogLambda:
Type: 'AWS::Serverless::Function'
Properties:
Handler: logs.handler
...
but with a docker-based build one has to define an ImageConfig
, i.e.
...
LogLambda:
Type: 'AWS::Serverless::Function'
Properties:
PackageType: Image
ImageUri: !Ref EcrImageUri
FunctionName: !Sub "${AWS::StackName}-Logs"
ImageConfig:
WorkingDirectory: /var/task
Command: ['logs.py']
EntryPoint: ['/var/lang/bin/python3']
...
ConfigLambda:
Type: 'AWS::Serverless::Function'
Properties:
PackageType: Image
ImageUri: !Ref EcrImageUri
FunctionName: !Sub "${AWS::StackName}-Config"
ImageConfig:
WorkingDirectory: /var/task
Command: ['config.py']
EntryPoint: ['/var/lang/bin/python3']
I'm a bit stuck because this does not work, no matter what combination I pass to the command array. If I fire a test event in the AWS console, I get the following error
RequestId: <uuid> Error: Runtime exited without providing a reason
Runtime.ExitError
Judging from the full output, the file is loaded and executed, but the handler function is not invoked (there is some output from a logging setup function which is called right after the module imports). The section in the AWS documentation on python3 based lambdas state that naming for handlers should be file_name.function (e.g. function_lambda.lambda_handler), but this doesn't give any clues on how do to this for command array in a ImageConfig.
How do I set the Command section correctly for my lambda function in my cloudformation template?
答案1
得分: 1
首先,部署到AWS Lambda的容器必须实现Lambda运行时接口。AWS Lambda不是通用的Docker容器运行时,它仅支持运行实现特定接口的容器。确保容器实现此接口的最简单方法是基于AWS提供的基础镜像之一。
注意,在基础的Python镜像中,ENTRYPOINT
不是python
,而是一个自定义脚本。该脚本期望COMMAND
与非容器化Lambda函数的app.handler
格式相同。
可以在此处查看一个示例的Dockerfile。
英文:
First, the container you deploy to AWS Lambda has to implement the Lambda Runtime Interface. AWS Lambda isn't a generic docker container runtime, it only supports running containers that implement a specific interface. The easiest way to ensure your container implements this interface is to base it on one of the AWS provided base images.
Note how the ENTRYPOINT
for the base Python images isn't python
it is a custom script. That script expects the COMMAND
to be in the same app.handler
format it is in for non-container based Lambda functions.
See an example Dockerfile here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论