26

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

2

3 Answers 3

48

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
Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

-6

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.