Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Reduce The Cost Of Your Lambda Functions With Event Filters (Part 2 of 2)

Home » Others » Reduce The Cost Of Your Lambda Functions With Event Filters (Part 2 of 2)

Reduce The Cost Of Your Lambda Functions With Event Filters (Part 2 of 2)

Last updated on April 27, 2023

Introduction

In the first part of this post, we covered the concept behind event filtering in AWS Lambda and learned how it could help you save money by invoking functions only when necessary. Now it’s time to put that knowledge into practice. In this part 2, we’ll see event filtering in action using Amazon Simple Queue Service (SQS) as the event source.

Overview

Reduce The Cost Of Your Lambda functions With Event Filters

  1. A user sends a JSON message to the SQS queue.
  2. Event source mapping picks up the message from the queue and evaluates it against the specified filtering criteria.
  3. Event source mapping invokes the Lambda function synchronously when a filtering criterion is met.

Setting up

I will give you two options for setting up the resources needed in this tutorial. The first option is the easier route, where you simply deploy the provided CloudFormation template, and it’ll provision everything for you. The second one is for those who prefer a hands-on approach and involves setting up the resources manually in the AWS Console.

Via AWS CloudFormation

  1. Download the CloudFormation template here. Save it as a .yaml file on your local computer.
  2. Go to the CloudFormation console. Click Create stack.

     Reduce The Cost Of Your Lambda functions With Event Filters

      3. Click on Upload a template file. Click Choose file, then select the template that you’ve just downloaded. Finally, hit Next.

      Reduce The Cost Of Your Lambda functions With Event Filters

      4. Enter a stack name. Then, click Next.

           Reduce The Cost Of Your Lambda functions With Event Filters

     5. Click Next until you reach the final step. Scroll down and tick the box that says I acknowledge that AWS CloudFormation might create IAM resources. Hit Submit.

Reduce The Cost Of Your Lambda functions With Event Filters

       6. Wait for CloudFormation to finish deploying the resources. You may click the refresh button to see updates without reloading the webpage on your browser.

      7. Once finished, head over to the Testing section for testing.

Manual method

  1. Create a Lambda function and choose Python3.9 as the Runtime. Leave everything else as is.

Reduce The Cost Of Your Lambda functions With Event Filters

      2. Copy the code below and paste it into your Lambda code editor. Then, deploy the changes.

      3. Under the Configuration tab, select Permissions and click the execution role that was created for your function. You will be redirected to the IAM Role page. Copy the ARN of your function’s execution role and put it in a text editor. You’ll need this later.

     Reduce The Cost Of Your Lambda functions With Event Filters

      4. Go to the SQS Console and create a queue.

      5. Scroll down to the Access policy section. In the Define who can receive messages from the queue setting, select the Only the specified AWS accounts, IAM users, and roles option. In the text box, paste the ARN of your function’s execution role.

Reduce The Cost Of Your Lambda functions With Event Filters

**Note that SQS will generate three permissions for you by default. You have to include the sqs:GetQueueAttributes to successfully configure the SQS queue as an event source for your Lambda function later.

      6. Go back to your Lambda function and click the Add trigger button.

      7. Select SQS as the source, then choose the SQS queue you’ve created. Set the Batch size to 1.

Reduce The Cost Of Your Lambda functions With Event Filters

      8. Add filter criteria and paste the following pattern:

     {"body":{"price":[ { "numeric": [ ">", 50 ] } ]}}

       9. Click Add

Testing

  1. On the webpage of your SQS queue, click Send and receive messages.

Reduce The Cost Of Your Lambda functions With Event Filters

       2. Enter the following message and click Send message

       {"price":55}

       3. Try sending multiple messages with varying prices ( < 50 or > 50). 

       4. Head over to your Lambda function. Under the Monitor tab, click View CloudWatch Logs.

        5. You should only see prices that are greater than 50. This means that messages containing prices under 50 were discarded by the event source mapping.

 Reduce The Cost Of Your Lambda functions With Event Filters

Conclusion

That’s it! I’ve shown you a basic example of how you can use event filtering in your workflow. You can try exploring other filter criteria to suit your needs.

 

To learn more about AWS Lambda, check out this FREE AWS Lambda Foundations course. And if you’re preparing for an AWS Certification, our high-quality practice exam samplers can help you get ready for test day. Try them out for FREE to get a feel for the exam format and level of difficulty.

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Tutorials Dojo portal

Learn AWS with our PlayCloud Hands-On Labs

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE AWS, Azure, GCP Practice Test Samplers

Follow Us On Linkedin

Recent Posts

Written by: Carlo Acebedo

Carlo is a cloud engineer and a content creator at Tutorials Dojo. He's also a member of the AWS Community builder and holds 5 AWS Certifications. Carlo specializes in building and automating solutions in the Amazon Web Services Cloud.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?