1. Setting up the required resources : A. Lambda Fucntion

image.png

B. S3 Bucket

image.png

C. Create Topic (SNS)

image.png

  1. Configurations - A. Add Bucket as source for lambda trigger -

image.png

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

image.png

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

image.png

image.png

  1. Time to trigger the lambda and hence the Topic -

image.png

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')
        }

  1. Upload file/image to S3 and you’ll get email in your inbox.

image.png