Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 757
YLam Avatar asked Mar 02 '16 15:03

YLam


People also ask

What happens when AWS Lambda throws exception?

If Lambda encounters an error, it returns an exception type, message, and HTTP status code that indicates the cause of the error. The client or service that invoked the Lambda function can handle the error programmatically, or pass it along to an end user.

Can CloudFormation interact with Lambda?

All in all, CloudFormation makes deploying AWS Lambda functions incredibly simple. Start by creating the template file that will define your resources. This will be your working folder for your code. Next, create your function in the appropriate file for your desired Lambda runtime.

What is Handler in AWS Lambda?

The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. When the handler exits or returns a response, it becomes available to handle another event.


3 Answers

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.

like image 50
YLam Avatar answered Sep 18 '22 14:09

YLam


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.

like image 38
Geoff Avatar answered Sep 18 '22 14:09

Geoff


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

like image 40
draysams Avatar answered Sep 19 '22 14:09

draysams