SNS notification for S3 static website
Tags: aws
Published: 2021-07-19 07:55:23

# About
With S3 event notification we can configure our S3 bucket to easily send out event notification mail, on every visit to our static website. We need to setup our cloudfront distribution for standard logging. Once the log data is PUT into our S3 bucket, an notification is sent out via SNS topic to the subscription of our choice.
# Cloudfront
First we need to setup our Cloudfront distribution for access logging.
* Go the the distribution.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1t7x0kgna23er17fjy5.PNG)
* Edit settings
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0rn1t0d8jentkw7b2ob.PNG)
* Enable Standard logging
Mention the bucket name and optional prefix.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4gz51n9rqqsndn0mda9.PNG)
* Once standard logging is setup we can see the log data periodically being PUT in the specified S3 bucket.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3sgv93le9ucjq1zknro.PNG)

# SNS
Now we can configure our SNS topic to send nofication mail on every log data PUT to our S3 bucket.
* Create a SNS topic 
The SNS topic must be in the same region as the bucket.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/298dn46wj8bsdr06n403.PNG)
Mention name and display name and create
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yrk2b5nnx4l8zctvbjy.PNG)
Once the topic is created, add a subscription with protocol email and destination mail ID.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouyzcckrb3tvi8ppqp15.PNG)
You will be receiving a confirmation mail, go ahead and confirm it. Once it is confirmed it should look like this.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ihid0tmtp9ilypjtyz23.PNG)

# Configure SNS access policy
Now we have to configure the SNS topic access policy to access the S3 bucket.
* Edit the SNS topic 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ect6x1zvqvzccsbqvw7v.PNG)
* Replace the access policy JSON with the below policy after replacing SNS-topic-ARN, bucket-name, bucket-owner-account-id with the required ones. [Official guide](https://docs.aws.amazon.com/AmazonS3/latest/userguide/ways-to-add-notification-config-to-bucket.html).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqd3kdj0vgh3u9h4ds90.PNG)
# S3
Go to properties tabs from inside your destination S3 bucket and create event notification.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0p8q8iky6iwn0nu9vprn.PNG)
Give an event name, and mention the optional prefix where Cloudfront log data is stored.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xshno4dtji1y1h8yjxll.PNG)
Check event type as `PUT`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q71bib5af66gker3jeqm.PNG)
Check SNS as your destination, your topic should show up in the drop down automatically, if the topic region matches the bucket region and the SNS policy is configured correctly.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/igir2qybp0j2vvr43bw3.PNG)
Click finish.
We are now configured to receive notifications!

Sample notification mail showing event time and log file of type `gz`
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szamz2hzyh1lejq15ovv.PNG)