Lambda triggers define how and when an AWS Lambda function executes. While event sources are specific services that generate events for Lambda (e.g., S3, DynamoDB, Kinesis, Kafka), triggers encompass all possible mechanisms for invoking a Lambda function. These include event sources, scheduled executions using Amazon EventBridge (formerly CloudWatch Events), manual invocations via the AWS Console, CLI, or SDKs, and integrations with other AWS services like Step Functions and API Gateway:

  • Event Sources: Automatically invoke Lambda when events occur in services like S3 or DynamoDB.
  • Scheduled Triggers: Run Lambda functions on a defined schedule, such as every hour.
  • Manual Triggers: Invoke Lambda directly for testing or custom workflows.
  • Service-Specific Integrations: Use Step Functions or API Gateway to trigger Lambda during workflows or API calls.

Key Features of Lambda Triggers

Lambda triggers define how Lambda functions interact with event sources and other invocation mechanisms. They control parameters such as how events are batched, filtered, and retried. For event-driven invocations, you can configure batching settings like batch size and batch window, ensuring that events are processed efficiently. Triggers also support error handling mechanisms, such as retries, Dead Letter Queues (DLQs), and Lambda Destinations, to manage failed invocations effectively.

Event Source Filters

Event source filters allow fine-grained control over which events a Lambda function processes. They use a custom JSON-based filtering language that matches attributes in the event payload. This helps reduce unnecessary invocations, saving costs and improving performance. Filters are applied at the event source mapping level, making them highly flexible for various use cases.

Filtering Language Overview

The filtering language is based on JSON object patterns and supports simple match criteria, logical operations, and array handling. You can define filters that match specific fields or structures in the event payload.

Example: Kafka Event Filter

Suppose a Kafka topic contains messages with the following payload:

{
  "type": "important-event",
  "user": "john_doe",
  "data": {
    "action": "update",
    "timestamp": "2025-01-06T12:34:56Z"
  }
}

You can create a filter to invoke the Lambda function only for messages with a "type" of "important-event":

{
  "eventSource": "kafka",
  "filterCriteria": {
    "filters": [
      {
        "type": {
          "equals": "important-event"
        }
      }
    ]
  }
}

Features of the Filtering Language

The filtering language supports:

  1. Field Matching: Match specific fields in the payload using equals, prefix, or suffix.
  2. Logical Operations: Combine filters with and, or, and not for complex conditions.
  3. Array Handling: Filter based on array elements, ensuring that any or all elements match criteria.

Event Source Mapping and Invocation Modes

Event source mapping connects an event source to a Lambda function and determines how events are retrieved and processed. For streaming sources like Kafka or DynamoDB Streams, Lambda uses polling to fetch events. Key parameters include batch size, which specifies the number of events retrieved per batch, and batch window, which defines how long Lambda waits to accumulate a batch before processing. Lambda supports two invocation modes:

  1. Polling: For sources like Kafka or Kinesis, Lambda continuously polls for new events.
  2. Push-based: For sources like S3 or API Gateway, events are pushed directly to Lambda without polling.