How to configure AWS SNS Notification for S3 event

How to configure AWS SNS Notification for S3 event

This tutorial will teach how to configure and send SNS notifications for S3 events like uploading new objects, deleting objects, lifecycle events, etc. We will use AWS SNS Service to send the email notification to the subscribers for any Amazon S3 bucket events.

Configure SNS notifications for S3 events

What is Amazon S3?

Amazon S3 is a fully managed object storage service that offers unlimited storage with industry-leading scalability, data availability, security, and performance.

Amazon S3 offers event notification features that enable you to receive events when certain operations are performed on the S3 bucket.

For example, you can configure S3 to send events to AWS SNS topics, AWS SQS, or AWS Lambda whenever the user uploads any file to the bucket.

S3 offers below event lists:-

  • The event for the object created, deleted, and restored in the AWS S3 bucket.
  • Replication Events
  • AWS S3 Lifecycle Events
  • S3 Intelligent-Tiering automatic archival events
  • Object tagging and ACL PUT events

What is Amazon SNS Service?

Amazon SNS (Simple Notification Service) is a fully managed messaging service to publish the messages published to AWS services or subscribers.

Under the hood, AWS SNS uses a pub-sub paradigm modal to push the notifications to the attached subscribers.

Amazon SNS can receive event-driven notifications from many AWS sources, such as AWS Lambda, S3, etc., and fan out the messages to application-to-application (A2A) and application-to-person (A2P) destinations.

Let’s walk through the steps to configure AWS SNS to send email notifications to subscribers for any S3 object events.

Create an AWS S3 bucket at AWS Console

  • Create an AWS S3 bucket at AWS Management Console
AWS S3 bucket to send notifications for object events using SNS Topic
  • Copy the S3 bucket name, which we will use later in the tutorial

Create an AWS SNS Topic

  • Move to AWS SNS Console, and select Create Topic.
Create AWS SNS Topic to receive S3 events
  • Under Details
    • Select Topic type as “Standard” AWS S3 notification did not support the FIFO topic type.
    • Provide Topic Name and Description
    • Leave all details and select Create Topic.
Provide SNS configurations to create a topic

Create a subscription to AWS SNS Topic

  • Under Details:
    • Select the AWS SNS Topic you created from the drop-down Topic ARN
    • Select “Email” as a Protocol
    • Provide the Email Address in Endpoint
    • Leave all options as default and select “Create Subscription.”

Add subscriber to the SNS topic who receives SNS notifications for S3 bucket events

  • You will receive an email to confirm the subscription after adding your email as a subscriber.
AWS SNS Topic Subscription Confirmation Email Notification

  • Once you confirm the subscription, AWS Console will display your subscription status as “Confirmed.”
Subscription confirmed for AWS SNS Topic

Attach access policy with AWS SNS Topic to read S3 bucket events

  • At AWS SNS Console, copy the SNS Topic ARN and click on the Edit Topic option from the options.
  • Replace the below Access Policy with the attached access policy with the Topic and select Save Changes
    • In the policy document, we provide access to the AWS S3 service to publish the events to SNS Topic mentioned in the policy.
    • Replace the SNS ARN with your SNS Topic ARN
    • Replace the bucket name with your created bucket name
    • Replace the account ID with your Account ID where you have made the AWS S3 bucket
{
    "Version": "2012-10-17",
    "Id": "aws-sns-intgeration-demo",
    "Statement": [
        {
            "Sid": "SNS topic policy",
            "Effect": "Allow",
            "Principal": {
                "Service": "s3.amazonaws.com"
            },
            "Action": [
                "SNS:Publish"
            ],
            "Resource": "<Your-SNS-Topic_ARN>",
            "Condition": {
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:s3:*:*:<your-bucket-name>"
                },
                "StringEquals": {
                    "aws:SourceAccount": "<Bucket_Owner_Account_ID>"
                }
            }
        }
    ]
} 

Create Event Notification Rule at S3 Bucket and attach AWS SNS Topic as a destination

  • Move to the AWS S3 console and select the bucket you have created.
  • Select Properties Tab and scroll down to “Event notifications.” Select Create notification 
  • Under General Configuration
    • Provide Event Name
    • You can provide Prefix and Suffix from your bucket for which you want to create this event. However, we will keep it as default.
  • Under Event Type, select the event type for which you want the notifications. For example, I hold “Object Creation” and “Object Removal” Events for this tutorial.
Create AWS S3 Bucket Event Notification

  • Under Destination, select SNS Topic as an event destination service, and from the drop-down, select the AWS SNS topic created earlier and select Save Changes.
Select AWS SNS Topic as a S3 event destination

Test the Functionality

Congratulation, your setup is complete!!

You can now test the configuration by uploading the objects to the bucket or deleting the object from the bucket.

You will receive a notification from AWS SNS Topic to your connected Email Endpoint for every event.

Conclusion

This article teaches you how to set up a configuration to trigger SNS Notifications for S3 Events.

Furthermore, it provides a brief introduction of various concepts related to it & helps the users understand them better to set up the notifications.

Feel free to provide feedback at [email protected].

Do not forget to subscribe to our newsletter for upcoming posts and hands-on exercises.

Appendix

Resources Used:

Amazon S3 Documentation

Amazon SNS Documentation

Back to Top