chpsam
chpsam

Reputation: 263

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'"

Upvotes: 26

Views: 70797

Answers (3)

ajith633
ajith633

Reputation: 191

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.

Upvotes: 7

ashutosh singh
ashutosh singh

Reputation: 185

Kishna_mee2004 is right you need to define lambda_handler without this it never work but if you getting below error:

Handler 'py' missing on module 'jobdata_rdcmedia_s3_Etl_job_scheduler_lambda': 'module' object has no attribute 'py'

Then you need to check handler info whether you have mention lambda_function_name.lambda_handler or not.

Upvotes: -6

krishna_mee2004
krishna_mee2004

Reputation: 7356

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

Upvotes: 48

Related Questions