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