LibraryEvent Patterns and Rules

Event Patterns and Rules

Learn about Event Patterns and Rules as part of Serverless Architecture with AWS Lambda

Understanding Event Patterns and Rules in AWS

In serverless architectures, especially those leveraging AWS Lambda, understanding how events trigger functions is crucial. Event patterns and rules act as the intelligent dispatch system, ensuring the right Lambda function is invoked based on specific event characteristics. This module explores how to define these patterns and rules to build robust, event-driven applications.

What are Event Patterns?

An event pattern is a JSON structure that defines the criteria for matching events. When an event occurs in an AWS service (like an S3 object creation or an SNS message), it's sent to a service like Amazon EventBridge (formerly CloudWatch Events). EventBridge then compares the event against defined rules. If the event matches a rule's pattern, the rule's target (e.g., a Lambda function) is invoked.

Event patterns filter events based on their content.

Event patterns are JSON objects that specify which fields and values within an event must match for a rule to be triggered. This allows for granular control over which events invoke your serverless functions.

Event patterns are essentially JSON objects that mirror the structure of the events they are designed to match. You specify key-value pairs that must exist and match within the incoming event. For example

How Rules Work with Event Patterns

Rules are the mechanism that connects event patterns to actions. A rule consists of:

  1. An Event Pattern: The criteria for matching events.
  2. A Target: The AWS service or resource to be invoked (e.g., an AWS Lambda function, an SQS queue, an SNS topic).
  3. Optional Input Transformation: A way to modify the event data before sending it to the target.

Think of EventBridge as a smart mail sorter. The event is the letter, the event pattern is the address and recipient criteria, and the rule is the instruction to deliver that letter to a specific mailbox (the target).

Common Event Sources and Pattern Examples

AWS services generate various types of events. Here are a few common ones and how their patterns might be structured:

Event SourceEvent Type ExampleEvent Pattern Snippet
Amazon S3Object Created"source": ["aws.s3"], "detail-type": ["AWS API Call via CloudTrail"], "detail.eventSource": ["s3.amazonaws.com"], "detail.eventName": ["PutObject"]
Amazon SNSMessage Published"source": ["aws.sns"], "detail-type": ["SNS Message Published"], "detail.subject": ["Order Confirmation"]
Amazon CloudWatch AlarmsAlarm State Change"source": ["aws.cloudwatch"], "detail-type": ["CloudWatch Alarm State Change"], "detail.alarmName": ["MyHighCPUAlarm"]

Advanced Pattern Matching

Event patterns support various operators for more sophisticated matching:

  • String matching: Exact matches, prefixes, suffixes, and CIDR blocks.
  • Numeric matching: Greater than, less than, greater than or equal to, less than or equal to.
  • Boolean matching: True/False.
  • Array matching: Checking for the presence of specific values within an array.
  • Null checks: Ensuring a field is absent or null.

EventBridge uses a JSON-based syntax for event patterns. This syntax allows you to specify conditions on various fields within an event payload. For example

📚

Text-based content

Library pages focus on text content

Best Practices for Event Patterns and Rules

To build efficient and maintainable event-driven systems:

  • Be specific: Define patterns that are as precise as possible to avoid unintended invocations.
  • Use multiple rules: For different actions or logic based on the same event source, create separate rules.
  • Leverage input transformation: If your Lambda function only needs a subset of the event data, use input transformation to send only the relevant information, reducing processing overhead.
  • Test thoroughly: Use the EventBridge console's event testing feature to validate your patterns against sample events.
What is the primary purpose of an event pattern in AWS EventBridge?

To define the criteria for matching incoming events.

What are the two main components of an AWS EventBridge rule?

An event pattern and a target.

Learning Resources

Amazon EventBridge User Guide(documentation)

The official AWS documentation providing a comprehensive overview of Amazon EventBridge, including its core concepts like events, rules, and targets.

EventBridge Event Patterns(documentation)

Detailed documentation on how to construct event patterns, including syntax, operators, and examples for various AWS services.

AWS Lambda Event Sources(documentation)

Learn about the various AWS services that can trigger AWS Lambda functions, and the structure of the events they generate.

Building Event-Driven Applications with Amazon EventBridge(blog)

A blog post from AWS that walks through the process of designing and implementing event-driven architectures using EventBridge.

AWS CloudTrail User Guide(documentation)

Understand how CloudTrail logs API calls, which are often used as the source for events in EventBridge for AWS API activity.

Amazon SNS Developer Guide(documentation)

Information on Amazon Simple Notification Service (SNS), a common source for events that can be routed via EventBridge.

EventBridge Input Transformation(documentation)

Learn how to use input transformations to customize the data sent to your Lambda functions or other targets from EventBridge.

AWS Lambda Best Practices(documentation)

General best practices for developing and deploying AWS Lambda functions, relevant for event-driven architectures.

EventBridge Schema Registry(documentation)

Discover how the Schema Registry can help you discover, create, and validate event schemas, aiding in pattern creation.

Serverless Architectures on AWS(blog)

An overview of serverless architectures on AWS, providing context for how EventBridge and Lambda fit into the broader ecosystem.