Hey guys, in today’s blog we will see how we can use an AWS S3 trigger to invoke a Lambda function. So without any further due, let’s dive into it…
Step 1 – Create an Amazon S3 Bucket.
- Open your AWS Console and search for S3.
- When you open S3, click on Create Bucket.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-1-1024x257.png)
- In the next step, simply name your S3 bucket, scroll down and click on Create Bucket. Keep everything else on default.
- As you can see below I named my S3 bucket demo-bucket-v3.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-2-1024x444.png)
Step 2 – Create a Lambda function.
- Open your AWS Console and search for Lambda.
- When you open Lambda, click on Create Function.
- In the next step, name your Lambda, and choose a runtime (I selected Python 3.9).
- Scroll down and click on Create function.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-4-1024x459.png)
Step 3 – Adding Amazon S3 trigger to invoke a Lambda function.
- When you have successfully created a Lambda function, you will see a screen like this.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-5-1024x663.png)
- Click on Add trigger.
- On the next screen search for S3, and select the bucket you created in Step 1.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-6-1024x460.png)
- Keep everything else on default, scroll down and click on Add.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-7-1024x546.png)
Step 4 – Give appropriate permissions to Lambda IAM Role.
Although this process is not mandatory but to be on the safer side, do this step.
- On your Lambda page click on Configuration and then go to Permissions.
- Then click on the Role name.
- In my case it’s test-lambda-role-9etv0r1m.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-8-1024x337.png)
- Click on Add Policies, and Attach Policies.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-10-1024x346.png)
- On the next screen search for these 3 policies and attach them.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-11-1024x165.png)
Step 5 – Testing our Trigger.
- Simply upload some files to your bucket.
- Then open your Lambda function’s Cloudwatch logs (Monitor -> View Logs in Cloudwatch) and see if logs are created.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-12-1024x225.png)
- And here’s my log file.
![image 13](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-13-1024x42.png)
- And when I open it I see my logs.
- As I had uploaded 3 images to my bucket, I can see 3 runtimes.
![Amazon S3 trigger to invoke a Lambda function](https://machinelearningprojects.net/wp-content/uploads/2022/07/image-14-1024x404.png)
And this is how you use Amazon S3 trigger to invoke a Lambda function…
Do let me know if there’s any query using the Amazon S3 trigger to invoke a Lambda function by contacting me via email or LinkedIn.
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…
Read my previous post: How to Deploy a Lambda function as a Component on an Edge Machine using AWS Greengrass v2
Check out my other machine learning projects, deep learning projects, computer vision projects, NLP projects, Flask projects at machinelearningprojects.net