DEV Community

Mohana Priya
Mohana Priya

Posted on

Automating Email Notifications for S3 Object Uploads using SNS

Introduction:

In today's cloud-centric world, automation is key to managing and scaling infrastructure efficiently. Amazon Web Services (AWS) offers a robust suite of services that allow developers to build complex systems with relative ease. One such powerful combination is Amazon Simple Storage Service (S3) and Amazon Simple Notification Service (SNS). Together, they can automate notifications for various events, such as when a new object is uploaded to an S3 bucket. This blog post will guide you through the process of setting up an S3 bucket, creating an SNS topic, subscribing to the topic via email, and configuring the system to send notifications when objects are uploaded to the S3 bucket.

Step-1: Create an S3 Bucket
First, we need an S3 bucket where objects will be stored. Follow these steps to create an S3 bucket:

1.Navigate to the S3 console: Open the AWS Management Console and navigate to the S3 service.
2.Create a new bucket:Click on "Create bucket" and provide a unique name for your bucket. Choose the appropriate region and configure other settings as per your requirements. For this example, we’ll stick with the default settings.
3.Create the bucket: Click on "Create bucket" to finalize the creation.

Image description

Step-2: Create an SNS Topic
Next, we’ll create an SNS topic to which notifications will be sent.

1. Navigate to the SNS console: Open the AWS Management Console and navigate to the SNS service.
2. Create a topic: Click on "Create topic", select "Standard" for the topic type, and provide a name for your topic. Click on "Create topic" to finalize the creation.

Image description

Step-3: Subscribe to the SNS Topic via Email
To receive notifications, we need to subscribe to the SNS topic using an email address.

1. Create a subscription:In the SNS console, select your topic and click on "Create subscription".
2. Set protocol and endpoint: Choose "Email" as the protocol and enter your email address in the endpoint field.
3. Confirm the subscription: AWS will send a confirmation email to the provided address. Check your email and click on the confirmation link to complete the subscription.

Step-4: Update the SNS Topic Access Policy
To allow S3 to publish messages to the SNS topic, we need to modify the topic's access policy.

1. Edit the policy: In the SNS console, select your topic and click on "Edit" under the Access Policy section.
2. Add the policy: Add the following policy to allow S3 to publish notifications:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": "SNS:Publish",
      "Resource": "arn:aws:sns:your-region:your-account-id:your-topic-name"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Step-5: Configure S3 to Send Notifications to SNS
Finally, configure the S3 bucket to send notifications to the SNS topic when an object is uploaded.

1. Navigate to the S3 bucket: In the S3 console, select your bucket and click on the "Properties" tab.
2. Add a notification: Under the "Event notifications" section, click on "Create event notification".
3. Configure the event: Provide a name for the notification, select the "All object create events" event type, and choose SNS topic as the destination. Select your SNS topic from the dropdown and save the configuration.

Image description

Step-6: Upload the object
At last upload the object in the s3 bucket that has been created.

Image description

SNS will send notification to the subscribed email once the object is uploaded in the S3 bucket.

Conclusion:
By following the steps outlined in this blog post, you have successfully set up an automated notification system that sends an email whenever a new object is uploaded to your S3 bucket. This integration between S3 and SNS not only demonstrates the power of AWS services but also showcases how they can be used to build robust and automated workflows. Whether for operational monitoring, security alerts, or just keeping track of changes in your storage, this setup is a valuable tool in any cloud infrastructure arsenal. Happy automating!

Top comments (0)