I am trying to create a lambda function with the below code and when I try to deploy and test the function I get errors. the idea I need for the function is that lambda should monitor a folder within s3 to see if the objects are in it for more than 1 hours and notify me.
import boto3
from datetime import datetime, timedelta
from dateutil.tz import tzutc, UTC
BUCKET = 'my-bucket'
TOPIC_ARN = 'arn:aws:sns:ap-southeast-2:123456789012:Old-File-Warning'
s3_resource = boto3.resource('s3')
sns_resource = boto3.resource('sns')
sns_topic = sns_resource.Topic(TOPIC_ARN)
for object in s3_resource.Bucket(BUCKET).objects.filter(Prefix='folder1/folder2/'):
if object.last_modified > datetime.now(tzutc()) - timedelta(hours = 1):
message = f"Object {object.key} is more than 1 hour old!"
sns_topic.publish(Message=message)
I am getting this error { "errorMessage": "Handler 'lambda_handler' missing on module 'lambda_function'", "errorType": "Runtime.HandlerNotFound", "stackTrace": [] }
when I try below
def handler_name(event, context):
s3_resource = boto3.resource('s3')
sns_resource = boto3.resource('sns')
sns_topic = sns_resource.Topic(TOPIC_ARN)
return
s3_resource
sns_topic
sns_resource
I get [ERROR] NameError: name 's3_resource' is not defined
What am I doing wrong?
CodePudding user response:
The default name is lambda_handler
, not handler_name
. So to address your error it should be:
def lambda_handler(event, context):
s3_resource = boto3.resource('s3')
sns_resource = boto3.resource('sns')
sns_topic = sns_resource.Topic(TOPIC_ARN)
return
Please note that there are many more issues with your code. The answer addresses the error you reported only.