1.png

Trigger AWS Lambda Function Based on an Event

Trigger AWS Lambda Function Based on an Event

There are multiple use cases for the AWS Lambda function. This AWS service enables you to run custom code without having to provision or manage servers. Which is great when you have a limited number of calls or don’t want to manage the servers. The AWS lambda function allows you to add custom logic to AWS resources such as Amazon S3 buckets and Amazon DynamoDb Tables etc


Here are some use cases:

● Use case 1: Trigger Lambda function to perform specific a task when an object is uploaded to an S3 bucket. For example, when a new video is uploaded to the bucket, the Lambda function can be configured to automatically compress the video or change its orientation from portrait to landscape. This usecase we discussed in one of our earlier post https://speckyfox.com/post/building-scalable-social-media-applications-by-leveraging-aws-services


● Use case 2: Another common use case of AWS is to back up and store data securely using the Amazon Simple Storage Service (S3). For example, suppose you have a website that generates critical data such as user data, logs, and configurations. In that case, you can configure an S3 bucket to automatically back up this data periodically using the AWS Lambda function. The Lambda function can be triggered on a schedule, such as every day or every week, and can automatically back up the data to the S3 bucket. 

Steps to set up the AWS Lambda function

● Step 1: We go to AWS Console and search Lambda


● Step 2: Then we click on Create function 

● Step 3:  Then we fill in the following properties:


● Step 4: After Creating the function our screen looks like the below:

○ Screen Part 1 - we add a trigger for the s3 bucket if the lambda function is not connected with any s3 bucket. We can even connect multiple existing s3 buckets with a single lambda function. In our case, we have to create a new AWS S3 bucket.

○ Screen Part 2 - we can write the code for whatever operation we want to perform 


● Step 5: 

○ Goto search and type s3 and click on the S3

○ Click on “Create bucket” 


○ Enter the bucket name and select the region you want the bucket to be created. (Note: S3 bucket region and lambda function region should be in the same region) and click on Create. 



● Step 6:

○ After creating the S3 bucket as mentioned in the above step, we have to go to the Lambda function screen. Click on the Add trigger button.

○ After Clicking our screen looks like this 


○ Select source and search s3 and select s3 bucket

○ Select Bucket name

○ Add prefix folder name and suffix file name extension

○ Select event type (which operation you want to perform on the s3 bucket like POST, DELETE, PUT)

○ Click on add button


● Step 7: After that, we have to add permission in the role and policies

○ Go to search and add the IAM role

○ Select the role that is the name of your lambda function

 

○ After that, we click on add permission and attach policies

○ After that, we search s3 and select AmazonS3FullAccess and click on Add permission



Our lambda function is ready to trigger whenever anything is pushed to the S3 bucket. This is how it will look.


Leave a comment