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.
Code:
def lambda_handler():
print('lambda_handler is called...');
print('Lambda is printing...');
Zip the python and place it in S3. (I have tried both folder and no folder)
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"
}
}
}
}
Go to Cloudformation and create a stack using the template. Stack was created successfully.
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.
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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With