如何配置基于Docker的Python3 Lambda函数的入口点/cmd?

huangapple go评论65阅读模式
英文:

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: &#39;AWS::Serverless::Function&#39;
    Properties:
      Handler: config.handler
      ...
...
  LogLambda:
    Type: &#39;AWS::Serverless::Function&#39;
    Properties:
      Handler: logs.handler
      ...

but with a docker-based build one has to define an ImageConfig, i.e.

...
  LogLambda:
    Type: &#39;AWS::Serverless::Function&#39;
    Properties:
      PackageType: Image
      ImageUri: !Ref EcrImageUri
      FunctionName: !Sub &quot;${AWS::StackName}-Logs&quot;
      ImageConfig:
        WorkingDirectory: /var/task
        Command: [&#39;logs.py&#39;]
        EntryPoint: [&#39;/var/lang/bin/python3&#39;]
...
  ConfigLambda:
    Type: &#39;AWS::Serverless::Function&#39;
    Properties:
      PackageType: Image
      ImageUri: !Ref EcrImageUri
      FunctionName: !Sub &quot;${AWS::StackName}-Config&quot;
      ImageConfig:
        WorkingDirectory: /var/task
        Command: [&#39;config.py&#39;]
        EntryPoint: [&#39;/var/lang/bin/python3&#39;]

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: &lt;uuid&gt; 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.

huangapple
  • 本文由 发表于 2023年2月23日 21:17:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75545370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定