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

🚀 25% OFF All Practice Exams, Video Courses, & eBooks – Cyber Week Blowout Deals!

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

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

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

Last updated on April 27, 2023

Problem statement

You are primarily billed based on how long your Lambda functions run and how often it is invoked. This is why AWS Lambda is great for scheduled jobs, short-duration tasks, and event-based processes. But does this mean you shouldn’t use them for high-volume traffic applications? Well, the short answer is… it depends.

Whether or not to use AWS Lambda for high-volume traffic workloads ultimately depends on the requirements of your application and the cost trade-off you’re willing to make. Regardless, if you ever find yourself wanting to use Lambda in a high-activity application like stream processing, it’s good to know that there are methods available to offset the cost of running functions.

Event Filtering to the rescue 

AWS Lambda’s event filtering enables you to select specific events from an event source to trigger your Lambda function. With event filtering, you can filter out events that are irrelevant to your application, reducing the number of unnecessary invocations and minimizing the operating cost of your Lambda functions. Event filtering is an optional feature of event source mapping — an AWS Lambda resource that reads from an event source (e.g., DynamoDB streams, SQS queues, Kinesis Data Streams) and invokes a function.

Reduce The Cost Of Your Lambda functions With Event Filters

Scenario

Let’s say you need to write a Lambda function that reacts to voltage drops and temperature changes in a site in order to detect faulty components. To do this, multiple sensors send readings to a Kinesis Data stream. In turn, the Lambda function must consume and process these readings to determine if any issues are present.

Tutorials dojo strip

Your initial thought might be to include a filtering logic within the function code, like the one below:

def lambda_handler(event, context):
    temperature = event["temperature"]
    voltage = event["voltage"]
    if temperature > 100 or voltage < 5:
        #do something
    else:
         #do nothing

While it may seem reasonable, it is inefficient from a cost standpoint. This is due to the fact that the Lambda function will be triggered needlessly even if sensor data doesn’t require any action. For example, a batch size of 1 and batching window of 0 can result in 60 invocations per minute, given that sensor data is sent every second. If none of the readings indicate a failure, you’d still end up paying for those 60 invocations. Imagine if this goes on for a week or even months!

Rather than doing conditional checks for each event at the function level, you can filter the events before they are passed down to your function. Going back to the scenario, since you’re only interested in specific voltage and temperature values, you can specify a filter pattern in the filter-criteria option of your event source mapping.

Reduce The Cost Of Your Lambda functions With Event Filters

A single event source can have up to five unique filters (you may refer to this link for the full list of filter syntax). If an event matches any of the filters, Lambda routes it to your function. Otherwise, the event is discarded.

You can configure event filtering on either a new event source or an existing one. This can be done in the AWS Lambda Console or programmatically using the CreateEventSourceMapping or UpdateEventSourceMapping APIs. 

In part 2 of this article, I’ll walk you through a simple demo of using Event filtering in AWS Lambda.

 

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.

 

🚀 25% OFF All Practice Exams, Video Courses, & eBooks – Cyber Week Blowout Deals!

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

FREE AWS, Azure, GCP Practice Test Samplers

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

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?