How to Send an Email for Every File Uploaded to an AWS S3 Bucket

Introduction

We have a secured bucket location in AWS S3 where customers are able to easily upload their diagnostic and other files to us. Instead of us having to poll the AWS S3 bucket manually for new uploads, we wanted a way to be notified by email every time a file landed.

The solution was a combination of an SNS Topic used by an SNS Subscription, and called by an S3 bucket Event notification.

Another advantage of the solution is that the cost is very low for us, because the base pricing for SNS email notifications is $2.00 per 100,000 notifications, and we get under 1,000 files per month. Visit https://aws.amazon.com/sns/pricing/ for more information.

In this blog post we will explore the procedure for getting an email notification every time a file is uploaded to an AWS S3 bucket.

Note
For all examples, the demo AWS account ID 123456789012 is used. Please substitute your own AWS Account ID for it.

STEP 1: Create the SNS Topic

AWS SNS → Topics → Click New Topic

Details section → Name → NewFileUploadedToS3Demo

Details section → Type → Standard

Details section → Display Name → New File Uploaded to S3 Demo

Encryption section → Disable encryption

Access Policy section → Advanced → JSON Editor:

{
  "Version": "2012-10-17",
  "Id": "example-ID",
  "Statement": [
    {
      "Sid": "example-statement-ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "SNS:Publish",
      "Resource": "arn:aws:sns:us-east-1:123456789012:NewFileUploadedToS3Demo",
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "123456789012"
        },
        "ArnLike": {
          "aws:SourceArn": "arn:aws:s3:*:*:demosourcebucket"
        }
      }
    }
  ]
}

In the above JSON, please replace both the example AWS account ID 123456789012 and the source bucket name `demosourcebucket` with your own.

Please visit the AWS documentation for more information: https://docs.aws.amazon.com/sns/latest/dg/sns-access-policy-use-cases.html

Delivery retry policy (HTTP/S) section → check the box for “Use the default delivery retry policy”

Delivery status logging section → Uncheck all “Log delivery status for these protocols”

Delivery status logging section → Success sample rate → 0 (zero)

Delivery status logging section → Success sample rate → 0 (zero)

Delivery status logging section → Service Role → Use existing service role

Delivery status logging section → IAM role for successful deliveries → NULL

Delivery status logging section → IAM role for failed deliveries → NULL

Click the `Create Topic` button at the bottom

Finally, click the `Create Subscription` button at the bottom of the Create Topic Confirmation page

STEP 2: Create the SNS Subscription

AWS SNS → Subscriptions

Details section → Topic → arn:aws:sns:us-east-1:123456789012:NewFileUploadedToS3Demo

Details section → Protocol → Email

Details section → Endpoint → your email address here

Subscription filter policy section → use defaults

Redrive policy (dead-letter queue) → disabled

Click the `Create Subscription` button at the bottom page

Next, check for a confirmation email sent to the email address used above and click on the `Confirm subscription` link in the body:

You have chosen to subscribe to the topic: 
arn:aws:sns:us-east-1:123456789012:NewFileUploadedToS3Demo

To confirm this subscription, click or visit the link below (If this was in error no action is necessary):
 
Confirm subscription

Please do not reply directly to this email. If you wish to remove yourself from receiving all future SNS subscription confirmation requests please send an email to sns-opt-out

You should be brought to a confirmation web page like this:

Simple Notification Service

Subscription confirmed!

You have successfully subscribed.

Your subscription's id is: 
arn:aws:sns:us-east-1:123456789012:NewFileUploadedToS3Demo:9211ea22-acae-4dda-be8c-9fd25f912ac8

If it was not your intention to subscribe, click here to unsubscribe.

At this point your SNS Subscription is all set up and all you have to do is tell the bucket to use it.

STEP 3: Create the S3 Bucket Event Notification

AWS S3 → Buckets → Click on the bucket name link (i.e. `demosourcebucket`) → Properties tab

General configuration section → Event Name → NewFileUploadedToS3Bucketdemosourcebucket

Event Types section → check the box to the left of All object create events

Destination section → SNS Topic → Specify SNS Topic → Enter SNS topic ARN → arn:aws:sns:us-east-1:123456789012:NewFileUploadedToS3Demo

Click the `Save Changes` button to create the new Event Notification for the demosourcebucket

Wrap-Up

Once the new event notification is created, test by uploading a file into `demosourcebucket` and check for an email from SNS - all should be working at this point!

In this blog post we explored the procedure for getting an email notification every time a file is uploaded to an AWS S3 bucket.

Please reach out to us if you have any questions.

About the Author

Eric M. Stone
COO and VP of Product Management

Eric is a veteran of fast-paced, large-scale enterprise environments with 35 years of Information Technology experience. With a focus on HA/DR, from building data centers and trading floors to world-wide deployments, Eric has architected, coded, deployed and administered systems for a wide variety of disparate customers, from Fortune 500 financial institutions to SMB’s.

Add new comment