Missing handler error in AWS Lambda
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
.
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