AWS Lambda event filtering is a powerful feature designed to reduce costs and simplify your function code by processing only the events that matter.
The Problem: Unnecessary Lambda Invocations
When a Lambda function is triggered by an event source like an SQS queue or a Kinesis stream, it is invoked for every message or record.
A common pattern is for the function's first step to be inspecting the event payload to decide if it's relevant. If not, the function simply stops and does nothing.
This is inefficient because:
- You pay for every invocation: Even if the function exits immediately, you are still charged for one invocation and a minimum of 1ms of duration.
- Code is more complex: Your function logic is cluttered with boilerplate
if/else
statements just to filter out irrelevant events. - Log clutter: Your CloudWatch Logs are filled with entries for these "do-nothing" invocations, making debugging harder.
The Solution: Filter Events at the Source
Lambda event filtering allows you to define a filter criteria on the event source mapping itself. The Lambda service inspects the event before invoking your function. If the event payload does not match your filter criteria, the Lambda function is not invoked at all.
This moves the filtering logic from your function code into the event source configuration.
Key Benefits
- Significant Cost Savings: You completely eliminate the cost of unnecessary invocations.
- Simpler Function Code: Your function no longer needs filtering logic. It can be written with the assumption that every event it receives is relevant and requires processing.
- Cleaner Logs: Reduces noise in your CloudWatch Logs, making it easier to find meaningful information.
How Event Filtering Works
You define a filter pattern as a JSON object. This pattern specifies the structure and values that must be present in the event payload for the function to be invoked.
Supported Event Sources
Event filtering is available for polling-based event sources:
- Amazon SQS
- Amazon Kinesis Data Streams
- Amazon DynamoDB Streams
Filter Criteria Syntax
The filter pattern can match on any part of the event payload's JSON structure.
Example Scenario:
Imagine an SQS queue receives order notifications. We only want our Lambda function to process orders that are for "books" and have a "high" priority.
Sample SQS Message Body (Event Payload):
{
"orderId": "1234-abcd",
"category": "books",
"details": {
"priority": "high",
"destination": "123 Main St"
}
}
Filter Criteria (defined in the Lambda trigger configuration):
{
"category": ["books"],
"details": {
"priority": ["high"]
}
}
How the Filter is Applied:
- The Lambda polling service fetches a batch of messages from the SQS queue.
- For each message, it inspects the body (the JSON payload).
- It compares the message body to the defined filter criteria.
- Only messages that match the criteria are included in the event sent to the Lambda function.
- If a message does not match, it is immediately discarded (and removed from the queue) without ever invoking the function.
Practical Example: Filtering SQS Messages
- Navigate to your Lambda function in the AWS Console.
- Select the SQS trigger in the "Function overview" section.
- In the trigger configuration, find the "Filtering" section and click "Add criteria".
- Define your filter pattern in the JSON editor based on the structure of your SQS message body.
- Save the trigger configuration.
From this point forward, your function will only be invoked for SQS messages that match the specified category
and priority
. All other messages will be filtered out at the source, saving you money and simplifying your code.