Skip to content

Create a Lambda Function Triggered by S3 Event, send the event as email using SNS

0

Create a Lambda Function Triggered by S3 Event, send the event as email using SNS

4 Answers
1
Accepted Answer

Step 1: Set Up the SNS Topic

Create an SNS Topic:

Go to the Amazon SNS console.

Click on Create topic.

Choose the Standard type.

Name your topic (e.g., S3EventNotification).

Click Create topic.

Subscribe an Email Endpoint:

After creating the topic, click on the topic's ARN.

Under the Subscriptions tab, click Create subscription.

Set Protocol to Email and enter the email address where you want to receive notifications.

Click Create subscription.

Confirm the subscription by clicking on the confirmation link sent to your email.

Step 2: Create the Lambda Function

Create a New Lambda Function:

Go to the AWS Lambda console.

Click Create function.

Choose Author from scratch.

Name your function (e.g., S3EventToSNS).

Set the Runtime to Python 3.8 or later.

Choose or create a role with the necessary permissions (e.g., AWSLambdaBasicExecutionRole and SNSPublishPolicy).

Click Create function.

Add the Lambda Function Code:

Replace the default code with the following:

import json
import boto3

sns_client = boto3.client('sns')

def lambda_handler(event, context):
    # Extract bucket name and object key from the event
    bucket_name = event['Records'][0]['s3']['bucket']['name']
    object_key = event['Records'][0]['s3']['object']['key']
    
    # Construct the message
    message = f"S3 Event Triggered:\n\nBucket: {bucket_name}\nKey: {object_key}"
    
    # Send the message to SNS topic
    response = sns_client.publish(
        TopicArn='arn:aws:sns:your-region:your-account-id:S3EventNotification',
        Message=message,
        Subject='S3 Event Notification'
    )
    
    return {
        'statusCode': 200,
        'body': json.dumps('SNS Notification Sent')
    }

Replace arn:aws:sns:your-region:your-account-id:S3EventNotification with the ARN of the SNS topic you created.

Configure the Function Permissions:

Ensure your Lambda function's IAM role has the necessary permissions to publish to the SNS topic.

Attach the following policy to your Lambda execution role:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sns:Publish",
            "Resource": "arn:aws:sns:your-region:your-account-id:S3EventNotification"
        }
    ]
}

Step 4: Test the Setup

Upload a File to the S3 Bucket:

Upload a file to the bucket to trigger the event.

Check Your Email:

You should receive an email with the event details.

https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventNotifications.html

EXPERT
answered a year ago
EXPERT
reviewed a year ago
EXPERT
reviewed a year ago
1

Hello.

S3 event triggers can directly set Amazon SNS topics, so if you just want to send event messages via email, you don't need to create a Lambda.
If you need to process the message you want to send, use Lambda.
https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventNotifications.html

I think the code introduced in the blog below will be helpful as a sample for sending messages from Lambda on SNS.
https://medium.com/@piyush.paudelaws/s3-event-trigger-with-sqs-sns-and-lambda-function-f89607b39d7
https://blog.devops.dev/s3-event-triggering-to-invoke-a-lambda-function-and-publish-the-details-via-sns-beginners-guide-585f93264f17

EXPERT
answered a year ago
EXPERT
reviewed a year ago
EXPERT
reviewed a year ago
0

Choose or create a role with the necessary permissions (e.g., AWSLambdaBasicExecutionRole and SNSPublishPolicy).

what does this mean do I need to create an IAM role and then attach the above policies and where to attach this

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sns:Publish", "Resource": "arn:aws:sns:your-region:your-account-id:S3EventNotification" } ] }

answered a year ago
0

Attach it to Lambda's IAM role.
You need to configure Lambda to be able to publish to the SNS topic.

EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.