YLam
YLam

Reputation: 451

aws cloudformation lambda python bad handler

I need to create aws Lambda (python) from cloudformation. The lambda function was created, but when I tried to execute the lambda, I keep getting the following error. 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.

  1. Create a simple python hello program that contains print statement (as simple as possible)

Code:

def lambda_handler():
    print('lambda_handler is called...');
    print('Lambda is printing...');
  1. Zip the python and place it in S3. (I have tried both folder and no folder)

  2. Create a cloudformation template with the following resource.

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. Stack was created successfully.

  2. When I Test the lambda using "Hello World" event template. I get the error.

"errorMessage": "Bad handler 'lambda_handler'"

If I look at the CloudWatch Log I see

Bad handler 'lambda_handler': need more than 1 value to unpack.

I am not passing arguments. This is the "Hello World" lambda function in Python. If I create this lambda function manually in the Lambda service, I could execute it without any errors. I only get this error when I create the lambda using Cloudformation.

Please point me to the right direction. Thanks in advance.

Upvotes: 21

Views: 21670

Answers (4)

CodeMed
CodeMed

Reputation: 9191

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:

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!"). 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.

Upvotes: 1

draysams
draysams

Reputation: 1259

Image of AWS Lambda lambda_handler error screen

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.

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.

This just means the default module name assigned to your python lambda function is as you guessed lambda_function.

Image of the fix for the error above

Upvotes: 9

YLam
YLam

Reputation: 451

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. 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". My file name in the zip file is "simple_python_filename.py". My function in the py file is "lambda_hander". Also, make sure you place the .py files in the root of the zip file.

Upvotes: 24

Geoff
Geoff

Reputation: 1287

I think the problem is with your declaration for "Handler".

It should contain the module name as well as the function name, i.e. it should be module_name.lambda_handler, where module_name is the name of the file containing your handler function.

I had the same error when creating lambda functions using boto3 for python - this solved the issue for me.

Upvotes: 14

Related Questions