简体   繁体   English

aws cloudformation lambda python 错误处理程序

[英]aws cloudformation lambda python bad handler

I need to create aws Lambda (python) from cloudformation.我需要从 cloudformation 创建 aws Lambda (python)。 The lambda function was created, but when I tried to execute the lambda, I keep getting the following error. lambda 函数已创建,但当我尝试执行 lambda 时,我不断收到以下错误。 I have tried many ways and I just couldn't get it working.我尝试了很多方法,但就是无法正常工作。

{
  "errorMessage": "Bad handler 'lambda_handler'"
}

This is how I created the lambda from cloudformation.这就是我从 cloudformation 创建 lambda 的方式。

  1. Create a simple python hello program that contains print statement (as simple as possible)创建一个包含 print 语句的简单 python hello 程序(尽可能简单)

Code:代码:

def lambda_handler():
    print('lambda_handler is called...');
    print('Lambda is printing...');
  1. Zip the python and place it in S3.压缩 python 并将其放在 S3 中。 (I have tried both folder and no folder) (我试过文件夹和没有文件夹)

  2. Create a cloudformation template with the following resource.使用以下资源创建一个 cloudformation 模板。

JSON: JSON:

"Resources": {
  "LF1ZOLJ": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
      "Handler": "lambda_handler",
      "Code": {
        "S3Bucket": "mybuckname",
        "S3Key": "simplepython.zip"
      },
      "Description": "cfn-create-lambda",
      "Role": "arn:aws:iam::305760000000:role/lambda_basic_execution",
      "Runtime": "python2.7",
      "Timeout": 60
    },
    "Metadata": {
      "AWS::CloudFormation::Designer": {
        "id": "xxxxxxxxxxxxxxxxxxxxxxxx"
      }
    }
  }
}
  1. Go to Cloudformation and create a stack using the template.转到 Cloudformation 并使用模板创建堆栈。 Stack was created successfully.堆栈创建成功。

  2. When I Test the lambda using "Hello World" event template.当我使用“Hello World”事件模板测试 lambda 时。 I get the error.我得到了错误。

"errorMessage": "Bad handler 'lambda_handler'" “errorMessage”:“错误的处理程序‘lambda_handler’”

If I look at the CloudWatch Log I see如果我查看 CloudWatch 日志,我会看到

Bad handler 'lambda_handler': need more than 1 value to unpack.错误的处理程序“lambda_handler”:需要超过 1 个值才能解包。

I am not passing arguments.我不是在传递论点。 This is the "Hello World" lambda function in Python.这是 Python 中的“Hello World”lambda 函数。 If I create this lambda function manually in the Lambda service, I could execute it without any errors.如果我在 Lambda 服务中手动创建此 lambda 函数,我可以毫无错误地执行它。 I only get this error when I create the lambda using Cloudformation.当我使用 Cloudformation 创建 lambda 时,我只会收到此错误。

Please point me to the right direction.请指出正确的方向。 Thanks in advance.提前致谢。

Yes, thank you for your helps.是的,谢谢你的帮助。 That fixed it.那修复了它。 I think AWS should have this in their documentation so other people can see it clearly.我认为 AWS 应该在他们的文档中包含这个,以便其他人可以清楚地看到它。 This is what I did.这就是我所做的。

    "Handler": "simple_python_filename.lambda_handler",
    "Code": {
      "S3Bucket": "mybuckname",
      "S3Key": "simple_python.zip"

where my zip file is "simple_python.zip".我的 zip 文件是“simple_python.zip”。 My file name in the zip file is "simple_python_filename.py".我在 zip 文件中的文件名是“simple_python_filename.py”。 My function in the py file is "lambda_hander".我在 py 文件中的函数是“lambda_hander”。 Also, make sure you place the .py files in the root of the zip file.另外,请确保将 .py 文件放在 zip 文件的根目录中。

I think the problem is with your declaration for "Handler".我认为问题在于您对“处理程序”的声明。

It should contain the module name as well as the function name, ie it should be module_name.lambda_handler, where module_name is the name of the file containing your handler function.它应该包含模块名称和函数名称,即它应该是module_name.lambda_handler,其中module_name 是包含处理函数的文件的名称。

I had the same error when creating lambda functions using boto3 for python - this solved the issue for me.使用 boto3 for python 创建 lambda 函数时,我遇到了同样的错误 - 这为我解决了这个问题。

AWS Lambda lambda_handler 错误屏幕的图像

If you are coming here because you saw the error in the image I've posted, the fix is to prepend lambda_function to the name of your handler in the Handler field of the AWS Lambda code screen.如果您来到这里是因为在我发布的图像中看到了错误,则解决方法是在 AWS Lambda 代码屏幕的 Handler 字段lambda_function到您的处理程序名称之前。

For instance if your handler name in your code is lambda_handler , You have to use lambda_function.lambda_handler in the Handler field on your code screen.例如,如果代码中的处理程序名称是lambda_handler ,则必须在代码屏幕的 Handler 字段中使用lambda_function.lambda_handler

This just means the default module name assigned to your python lambda function is as you guessed lambda_function .这只是意味着分配给您的 python lambda 函数的默认模块名称与您猜测的lambda_function

修复上述错误的图片

Posting a new reply to this very old posting for people like us who found this using the search engines.为像我们这样使用搜索引擎找到它的人发布对这个非常旧的帖子的新回复。

The solution that worked for us included the following cloudformation:对我们有用的解决方案包括以下 cloudformation:

LambdaFunction:
  Type: AWS::Lambda::Function
  Properties:
    Runtime: python3.8
    Timeout: 5
    Handler: index.lambda_handler
    Role: !GetAtt LambdaFunctionRole.Arn
    Code:
      ZipFile:
        !Sub
          - |-
            def lambda_handler(event, context):
              print("Inside handler!")
              #Previous first line which was breaking the code

              return { 
                'message' : 'some value populated by code we are redacting for simplicity'
              }

As you can see, our previous line that was throwing the error that led us to this post needed to be placed underneath a new first line print("Inside handler!") .正如您所看到的,我们之前抛出导致我们发布这篇文章的错误的行需要放置在新的第一行print("Inside handler!")之下。 After we lowered the error-causing line to a lower line in the function code, the lambda service was able to return a more meaningful error message, which in turn enabled us to fix the root cause error.在我们将导致错误的行降低到函数代码中的较低行之后,lambda 服务能够返回更有意义的错误消息,这反过来使我们能够修复错误的根本原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM