python 脚本是否必须在 AWS Lambda 中命名为 handler.py

Does the python script have to be named as handler.py in AWS Lambda

python 脚本是否必须在 AWS Lambda 中命名为 handler.py?

我不记得我是从哪里读到这个的,它说 lambda 配置为查找特定文件,通常名为 'handler.py',只是想知道我们可以在哪里配置这个或者它是否必须 'handler.py'?谢谢

不,不一定要命名为 handler.py。

你可以随意命名,但在 lambda 处理程序中给出

file_name.function_name

就像导入一样

如果您的文件夹中有处理程序文件,请包含

init.py

在所有文件夹中并执行如下所示的正常导入

 src/
      __init__.py
      app.py

您可以在处理程序配置中以 src.app.function_name 的形式导入

您可以随意命名此 Python 脚本。请务必正确引用它。

You can tell the Lambda runtime which handler method to invoke by setting the handler parameter on your function's configuration.

When you configure a function in Python, the value of the handler setting is the file name and the name of the handler module, separated by a dot. For example, main.Handler calls the Handler method defined in main.py.

Does the python script have to be named as handler.py in AWS Lambda

不久 不,您可以指定任何可以处理 lambda 事件的方法。 (通常组合是事件+上下文(事件,上下文)

Just wondering where we can configure this or does it have to be 'handler.py'?

这实际上取决于您构建和部署 lambda 的方式,但很快处理程序 属性 会告诉您要调用哪个方法,您可以将其指定为部署包的相对路径 - https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-handler

  • template.yml
LambdaFunction:
  Type: AWS::Lambda::Function
  Properties:
    Handler: index.handler