
B. S3 Bucket

C. Create Topic (SNS)


B. Make sure the Role attached to Lambda (in permissions) has SNS Permissions attached -

C. Create Email Subscription for the Topic (make sure to confirm subscription via email)



import json
import boto3
import os
SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:967277549983:MySNSviaS3Topic'
sns_client = boto3.client('sns')
def lambda_handler(event, context):
try:
message = 'This is a test email triggered by an S3 event.'
subject = '📩 New File Added to S3 Bucket'
response = sns_client.publish(
TopicArn=SNS_TOPIC_ARN,
Message=message,
Subject=subject
)
print(f"Message published. Message ID: {response['MessageId']}")
return {
'statusCode': 200,
'body': json.dumps('Message published to SNS topic with custom subject')
}
except Exception as e:
print(f"Error publishing message: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps('Error publishing message')
}
