Manage Amazon S3 Buckets Using Event Notifications With AWS SQS
Tags: aws, architecture, s3
Published: 2022-05-09 05:35:53

Recently I came across a problem where the admin team should get notified when some user deletes any object from S3 object. If you also have a similar requirement where the team should get notification whenever an object is created, deleted, or modified, this is where event notifications comes into the picture.

You can use AWS S3 Event notifications feature to receive notifications to following destinations when certain events happen in the AWS S3 bucket.

- Amazon Simple Notification Service (Amazon SNS) topics
- Amazon Simple Queue Service (Amazon SQS) queues
- AWS Lambda function

In this article, I will be configuring AWS SQL queues as a destination to track delete event from my S3 bucket.

Configure AWS SQL queues to track delete event from S3 bucket
_Step 1_

Login to AWS Console

_Step 2_

Go to S3 bucket and create a new bucket if it does not exist. I have already created a bucket with name ‘mydemobucket198’.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebb4okdbo21vz5rgbrhw.png)
 

_Step 3_

Go to Properties.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xivr3ebqun5nijnywuys.png)
 

_Step 4_

Scroll down to the page until section Event Notification appears. Click on Create event notification button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ab1mi0w97c45ltxd880.png)
 

_Step 5_

A window appears to fill details to create a new event notification. I have put ‘s3_notify’ as the event name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7feqmvsih2l68mbbpcj.png)
 

_Step 6_

A list will appear for events that are currently supported like Object creation, Object removal, etc. I checked Object Removal for this article so whenever any user deletes any S3 object I will get a notification.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b9y51egjvi6fe1cydpj9.png)
 

_Step 7_

Go to Destination section where you can choose a destination to publish the event.

I have chosen SQL queues so notification will be sent to SQS queues that can be read later by a server and perform action accordingly.

Choose SQL queues if it's already created or create a new one. As I don't have any existing SQL queues, I will create a new one.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86k3h1b5688tk7uaibc5.png)
 

_Step 8_

Go to Amazon SQS service and click on Create queue.

_Step 9_

Enter name of SQS queue as ‘s3_delete_notify’ and choose all settings as default.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mbng9hnpnb3zj4hl2e1m.png)
 

_Step 10_

Once the SQS queue is created, go to Access Policy.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nfgc8ggtqqwngl2rft8q.png)
 

_Step 11_

We need to modify the Access Policy to send a message from S3 bucket when any object gets deleted.

_Step 12_

Click on Edit and paste below Access Policy code -

{
"Version": "2012-10-17",
"Id": "Policy1651140347168",
"Statement": [
{
"Sid": "Stmt1651140341677",
"Effect": "Allow",
"Principal": "*",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:ap-south-1:464473132183:s3_delete_notify"
}
]
}

Don't forget to replace the ARN value with your SQL queue.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1hb2g3avtfw7hozc6de.png)
 

_Step 13_

Refresh the page and select the SQS queue that we have just created in the above steps. Click on Save Changes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mygbrcnm593fg4mc8eat.png)
 

_Step 14_

Go to S3 bucket and upload some files.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr359go8ntu65hbjkicv.png)
 

_Step 15_

Select any file and click on Delete button. I have selected photo-1.jpeg and deleted that file.

_Step 16_

Go to SQS queue ‘s3_delete_notify’ and click on Send and receive message button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4nnywx142r0mqqowgu2a.png)
 

_Step 17_

Click on Poll for messages button to pull messages.

_Step 18_

As you can see, selected ID is related to S3 object that has been deleted.

There is another ID which was created by AWS to test initially when queue was created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g4a6moivhyfb19mww5hr.png)
 

_Step 19_

After you click on Message ID, you can see the details in Message Body. In our case, we can see S3 bucket name with S3 object name photo-1.jpeg which was deleted.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wnjh9meqsqhtp1og9b2p.png)
 

**Conclusion**

Using S3 Event notification you can enable notification for events like creating, deleting, and modifying S3 objects using destinations Amazon SNS, Amazon SQS and AWS Lambda.