S3 Event Trigger

Ajinkya KumbharAjinkya Kumbhar
6 min read

In this blog, we will explore the power of Amazon S3 buckets and how to use S3 event triggers to automate your workflows. With just a single Shell script, you will learn how to configure AWS resources without any manual intervention.

Whether you're a seasoned AWS expert or just getting started with cloud computing, this blog will provide valuable insights and practical tips that you can use in your daily work. From creating an EC2 instance to setting up S3 buckets and configuring event triggers, you will learn step-by-step how to implement this powerful automation process.

By the end of this blog, you will have a deeper understanding of how S3 buckets and event triggers can help streamline your workflows and improve your productivity. You'll also be equipped with the knowledge and skills you need to take your AWS projects to the next level.

Let's dive in and discover the power of S3 buckets and event triggers

What are S3 Buckets and Event Triggers:

S3 bucket is like. It's a big, virtual treasure chest in the cloud where you can store and share all sorts of things like photos, videos, documents, and more. And just like a real treasure chest, you can choose who gets to see what's inside. You can keep some things private. But the best part about an S3 bucket is that it's not just a place to store your stuff - it's a powerful tool that can help to build websites, run applications, and even analyze data. And with features like S3 event triggers, you can set up automated workflows to make all these things happen automatically whenever you add or change something in your bucket.

By configuring S3 event triggers to notify an SNS topic, you can automate the delivery of messages to multiple subscribers based on the changes made to objects in S3 buckets. For example, if you have an S3 bucket containing images or videos that are being uploaded or modified by multiple users, you can use S3 event triggers to notify subscribers (such as team members or clients) via email or mobile push notifications about the changes made to the objects.

Real-life use cases of S3 event triggers:

Imagine you're a baker running a bakery business that delivers freshly baked cakes to customers' doorsteps. You want to make sure that your delivery team is always on top of things and that your customers are notified in real time about the status of their orders.

To achieve this, you can use S3 event triggers in conjunction with Amazon SNS. When a new order is received, a Lambda function can be triggered, which in turn can notify your delivery team via SMS or email using SNS. Once the cake is baked and ready for delivery, S3 can be configured to notify SNS again, which can send a message to the customer with the delivery status and estimated delivery time. This way, you can keep your customers happy and informed while streamlining your bakery operations.

Similarly, imagine you're an online retailer that sells fashion accessories. You want to ensure that your inventory is always up-to-date and that your customers are notified in real-time about any new products added to your catalog.

To achieve this, you can use S3 event triggers in conjunction with AWS Lambda and Amazon SNS. When a new product is added to your inventory, a Lambda function can be triggered, which can update your catalog database and notify your customers via email or SMS using SNS. This way, your customers are always informed about the latest products, and you can keep your inventory updated automatically.

If we happen to take similar examples of popular companies we can see that companies like Netflix, Airbnb, and Lyft use S3 triggers to notify their users regarding new content being uploaded, new properties in nearby locations, and about availability of rides in nearby locations respectively.

Working of S3 Event Triggers:

To set up the S3 event trigger we will be taking the help of this GitHub repository.

Initialization:

1. AWS EC2 instance setup:

To create an EC2 instance on the AWS console, we need to ensure that the AWS CLI is configured on the instance. We can verify this by running the command aws on the instance's CLI. If we receive a response, it means that the AWS CLI is properly configured.

and if not we can easily install it using the following command

sudo apt-get install awscli

2. Other Requirements:

  • jq

      sudo apt-get install jq
    
  • zip

      sudo apt-get install zip
    

3. Cloning GitHub Code to EC2 instance:

Run the following command to clone the GitHub repository to get the Shell script and lambda function code

git clone <https://github.com/ajinkyak423/shell-scripting-projects.git>

After cloning the repository to the EC2 instance, navigate to the s3-notification-triggers.sh file using the cd command. Ensure to update the email address in the shell script to receive email notifications on your desired email address. Additionally, modify the Lambda function code and add your own AWS account number.

4. About the script

AWS environment for triggering an SNS notification when a new object is uploaded to an S3 bucket

  1. It gets the AWS account ID and sets variables such as the AWS region, bucket name, lambda function name, role name, and email address.

  2. It creates an IAM Role and attaches the policies for AWS Lambda and Amazon SNS.

  3. It creates an S3 bucket, uploads a file to the bucket, and sets up the S3 event trigger for the Lambda function.

  4. It creates a Lambda function, attaches the IAM role to it, and sets up the S3 event trigger for the function.

  5. It creates an SNS topic, adds permissions to publish messages to the Lambda function, and publishes a message to the topic.

5. Results:

After the successful execution of the shell script, check your configured email ID for a subscription message and subscribe to the given link. On your AWS console, you can see that the following things are being created without any intervention.

  1. S3 bucket:

  2. Lambda Function:

  3. SNS topic:

  4. IAM role:

When any image or file is uploaded to the S3 bucket named "ajinkya-ultimate-bucket" in this case, we will receive a notification on the configured email address.

Conclusion

In conclusion, we have explored the powerful capabilities of AWS when it comes to event-triggered automation. By leveraging the S3 bucket notifications and Lambda functions, we were able to set up a system that automatically processes new uploads to our S3 bucket and sends email notifications to our inbox. This can be incredibly useful for a variety of use cases, from monitoring changes to critical files to automating routine data processing tasks.

We hope that this tutorial has given you a taste of the possibilities that AWS offers and that you are now inspired to explore more on your own. With the power of cloud computing and the flexibility of serverless architecture, the sky truly is the limit when it comes to what you can achieve with AWS.

More resources

YouTube

GitHub

1
Subscribe to my newsletter

Read articles from Ajinkya Kumbhar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ajinkya Kumbhar
Ajinkya Kumbhar

Enthusiastically exploring the world of DevOps