19
votes

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.

3

3 Answers

22
votes

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.

13
votes

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.

7
votes

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