Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing handler error in AWS Lambda

My apologies for basic question. I am completely new to AWS as well as Python. I am trying to do sample code given in https://boto3.readthedocs.io/en/latest/guide/migrations3.html#accessing-a-bucket but facing a error.

import botocore
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('bucketname')
exists = True


try:
    s3.meta.client.head_bucket(Bucket='bucketname')
except botocore.exceptions.ClientError as e:
    # If a client error is thrown, then check that it was a 404 error.
    # If it was a 404 error, then the bucket does not exist.
    error_code = int(e.response['Error']['Code'])
    if error_code == 404:
        exists = False 

Error in logs is

"errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'"

like image 562
chpsam Avatar asked Jan 15 '18 15:01

chpsam


People also ask

How do I specify a 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.

How do I troubleshoot failures in an AWS Lambda function?

To troubleshoot Lambda code errors You can use CloudWatch to view all logs generated by your function's code and identify potential issues. For more information, see Accessing Amazon CloudWatch Logs for AWS Lambda.

Can a Lambda have 2 handlers?

AWS only allows you to define one handler per Lambda function. To implement the services pattern you need to implement a router inside your handler. If you want to see how I'm doing it I've written a blog post about Routing API Gateway Traffic Through One Lamda Function with example code.


2 Answers

You need to define a function in your code. The code is missing the function named lambda_handler. Your code should look like:

    import botocore
    import boto3

    def lambda_handler(event, context):
        s3 = boto3.resource('s3')
        bucket = s3.Bucket('bucketname')
        exists = True

        try:
            s3.meta.client.head_bucket(Bucket='bucketname')
        except botocore.exceptions.ClientError as e:
            # If a client error is thrown, then check that it was a 404 error.
            # If it was a 404 error, then the bucket does not exist.
            error_code = int(e.response['Error']['Code'])
            if error_code == 404:
                exists = False
like image 151
krishna_mee2004 Avatar answered Oct 23 '22 12:10

krishna_mee2004


Move your code inside a python function. You can give it any name but that will become your handler. Go to lambda function basic settings and change the default handler to <yourfilename>_<yourfunctionname>. By default when you create a lambda function, the file name will be lambda_function_name.py (you can change it) and your handler method will be lambda_handler (you can change it), so the entry point is lambda_function_name.lambda_handler.

like image 6
ajith633 Avatar answered Oct 23 '22 11:10

ajith633