Hello Readers! In this blog we will see How to Create S3 Event Notification using AWS Lambda Function. Before this we will get familiar with AWS S3 Event Notification and shortly explain about AWS Lambda Function.
So, we will see how it works and after that we will see its demo.
Let’s get started.
The Amazon S3 notification is a feature that enables.
You to receive notifications when certain events(put, delete, modify, etc.) happen in your bucket.
In this case there can be three types of target that can be added as a Destination – SNS topic, SQS queue or Lambda function.
AWS S3 Event Notification ?
- S3:ObjectCreated, S3:ObjectRemoved,
- S3:object:Restore, S3:Replcation
- Object name filtering possible (*.jpg)
- Use case: generate thumbnails of images uploaded to S3
- Can create as many “S3 Events” as desired
- S3 event notification typically deliver events in seconds. But sometimes take a minute or longer
- So, if two writes are made to single non-versioned object at the same time.
- It is possible that only a single event notification will be sent
- If you want to ensure that an event notifications is sent for every successful write, you can enable versioning on your bucket.
AWS Lambda Function
- Run functions on demand without the servers
- Supports many languages (Python, Java, Go, Ruby etc.)
- Adhoc task or completely serverless high TPS Applications
- Pay per innvocation, duration, memory
- Built in metrics with AWS Cloudwatch
Creating S3 Event Notification using AWS Lambda Function
Step1: Firstly Create a S3 bucket. For this, Move to your AWS management console. Also click on S3 service. Interfaces like this will open.
So, click on create bucket.
Before start Give here the Bucket name. Lets give it to a unique name awsbucket1. Leave other inputs as default.
So, click on Create Bucket.
So, my bucket is ready now.
Step2: Now We will create a AWS Lambda function.Click on Lambda service.
So, click on Create Function.
For the creating the function name. Select runtime as Use a blueprint option. Select the S3-get-object-python radiobutton.
and Click on configure.
And now give here the function name with s3triggerfunction. For the Execution role, I will choose first one.
In short, if i come down I’m not going to add the S3 bucket as a trigger. Avoid that and removing it.
this is the piece of code that is available. And you can change it according your requirements. Click on Create function.
My function is ready now.
You can see the code here. Certain changes that i would like to do. Because whenever s3 GetObject code is executed here. Click on deploy button.
Now my function is ready. Let’s go to the corresponding S3 bucket here.
Step3: Also, I’m going to create an event notification. Let’s Give it to the name of event with event1 and here we have different type of events.
Any s3 events that’s taking the place object removal , object restoration, object ACL, tagging or event creation you can set according your requirements.
Lastly at the destination has three option. So. let’s leave as default and let me select the s3 function trigger, Save changes it.
and now you can see an event notification.
So, let’s go to the corresponding S3 bucket and upload the file name sample .txt and let’s uploaded and see it.
Let’s open the Cloudwatch console.We are waiting for certain logs. Let’s see what all other things are available here. I will click on View as text.
And you can see all the events. We have printed events we are getting an access denied.
For this we have to provide certain permissions privileges to our S3 bucket.Our function can easily get hold the object.
Edit Block all public access
Let’s go to the s3 console again. And click on permission section.So, I would be removing this block public access and I’m unchecking this. That means it’s allowing the all public access.
And write confirm and click on confirm button.
Step4: Add a bucket policy here. Let’s add bucket policy and the action here. So, you can add this as well as resources and principles.
So, I’m going to edit block public access.We will tick block all public access. And write confirm and click on save changes.
Now Again re-upload my sample.txt file and click on save it.
So, Click on this to see the log events.
So, We have successfully create the s3 event notification and you can see the contents of my files over here. Finally this is all about the function done.
Congrats! 👏 We are done now!
Thank you for sticking to the end. In this blog we have seen How to Create S3 Event Notification using AWS Lambda Function. Also we are familiar with Amazon Notification.
Hey, readers! Thank you for sticking up till the end. If you have any questions/feedbacks regarding this blog, I am reachable at email@example.com.