AWS Compute Services

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

5 min read
Updated June 21, 2025
5,812 characters

This cheat sheet covers the advanced syntax for AWS Lambda event filtering, allowing you to build more complex and precise rules to control which events trigger your function. This is a continuation of the basic event filtering concepts.


Filter Rule Syntax

Lambda event filter rules are defined in a JSON object. The structure of the JSON must match the structure of the event payload you want to filter. The filter engine supports a rich set of comparison operators.

Basic Filtering (Recap)

  • Exact Match:

    • Payload: { "status": "completed" }
    • Filter: { "status": ["completed"] }
  • OR Logic (Any value in the array):

    • Payload: { "priority": "high" }
    • Filter: { "priority": ["high", "critical"] } (Triggers if priority is "high" OR "critical")

Advanced Filtering Rules

Numeric Matching

You can filter on numeric fields using comparison operators.

  • Rule:
    { "price": [{ "numeric": [">", 0, "<=", 150.50] }] }
    
  • Matches:
    • { "price": 100 }
    • { "price": 150.50 }
  • Does NOT Match:
    • { "price": 0 }
    • { "price": 200 }

exists - Checking for Presence

The exists operator checks whether a specific field is present in the payload.

  • Rule:
    { "customer": { "email": [{ "exists": true }] } }
    
  • Matches: { "customer": { "email": "test@example.com" } }
  • Does NOT Match: { "customer": { "phone": "555-1234" } } (The email key is missing)

You can also check for non-existence:

  • Rule:
    { "customer": { "email": [{ "exists": false }] } }
    

prefix - Matching the Beginning of a String

The prefix operator checks if a string value starts with a specific substring.

  • Rule:
    { "eventType": [{ "prefix": "order_" }] }
    
  • Matches:
    • { "eventType": "order_created" }
    • { "eventType": "order_shipped" }
  • Does NOT Match: { "eventType": "payment_processed" }

anything-but - Exclusion Logic

The anything-but operator matches any value except for the ones specified. This is useful for ignoring certain event types.

  • Rule:
    { "state": [{ "anything-but": ["archived", "deleted"] }] }
    
  • Matches:
    • { "state": "pending" }
    • { "state": "active" }
  • Does NOT Match:
    • { "state": "archived" }
    • { "state": "deleted" }

You can also use anything-but with a prefix:

  • Rule: { "source": [{ "anything-but": { "prefix": "internal-" } }] } (Matches any source that does not start with "internal-")

Real-World Example: DynamoDB Streams

Imagine you have a DynamoDB stream capturing item-level changes. You want to trigger a Lambda function only when a new customer is added with a creditScore over 700.

Sample DynamoDB Stream Record (simplified payload):

{
  "eventName": "INSERT",
  "dynamodb": {
    "NewImage": {
      "customerId": { "S": "cust-9876" },
      "status": { "S": "active" },
      "creditScore": { "N": "750" }
    }
  }
}

Advanced Filter Criteria:

This filter combines multiple rules to achieve the desired outcome.

{
  "eventName": ["INSERT"],
  "dynamodb": {
    "NewImage": {
      "creditScore": [{ "numeric": [">", 700] }],
      "status": [{ "anything-but": ["on_hold"] }]
    }
  }
}

How this filter works:

  1. "eventName": ["INSERT"]: Ensures the function only triggers for newly created items, not for updates or deletions.
  2. "creditScore": [{ "numeric": [">", 700] }]: Applies a numeric filter on the creditScore field within the NewImage part of the payload. Note that DynamoDB Stream values are strings, but the filter engine can evaluate them numerically.
  3. "status": [{ "anything-but": ["on_hold"] }]: Adds an extra condition to ignore new customers who might be in an "on_hold" state.

By using these advanced rules, you can create highly specific triggers, drastically reducing unnecessary Lambda invocations and making your serverless architecture more efficient and cost-effective.