Let’s take a look into AWS EventBridge, a powerful service designed to connect applications using data from our own apps, integrated Software as a Service (SaaS) apps, and AWS services. In particular, we’ll focus on the two main features: EventBridge Rules and the relatively new EventBridge Scheduler. These features overlap in many ways but also offer distinct functionalities that can significantly impact how we manage event-driven applications. Let’s explore what each of these features brings to the table and how to determine which one is right for our needs.
What is AWS EventBridge?
AWS EventBridge is a serverless event bus that makes it easy to connect applications using data from our applications, integrated SaaS applications, and AWS services. EventBridge simplifies the process of building event-driven architectures by routing events from various sources to targets such as AWS Lambda functions, Amazon SQS queues, and more. With EventBridge, we can set up rules to determine how events are routed based on their content.
EventBridge Rules
Overview
EventBridge Rules allow you to define how events are routed to targets based on their content. Rules enable you to match incoming events and send them to the appropriate target. There are two primary types of invocations:
- Event Pattern-Based Invocation
- Timer-Based Invocation
Event Pattern-Based Invocation
This feature lets us create rules that match specific patterns in event payloads. Events can come from various sources, such as AWS services (e.g., EC2 state changes), partner services (e.g., Datadog), or custom applications. Rules are written in JSON and can match events based on specific attributes.
Example:
Suppose we have an e-commerce application, and we want to trigger a Lambda function whenever an order’s status changes to “pending.” We would set up a rule that matches events where the orderState attribute is pending and routes these events to the Lambda function.
{
"detail": {
"orderState": ["pending"]
}
}
This rule ensures that only events with an orderState of pending invoke the Lambda function, ignoring other states like delivered or shipped.
Timer-Based Invocation
EventBridge Rules also support timer-based invocations, allowing you to trigger events at specific intervals using either rate expressions or cron expressions.
- Rate Expressions: Trigger events at regular intervals (e.g., every 5 minutes, every hour).
- Cron Expressions: Provide more flexibility, enabling us to specify exact times for event triggers (e.g., every day at noon).
Example:
To trigger a Lambda function every day at noon, we would use a cron expression like this:
{
"scheduleExpression": "cron(0 12 * * ? *)"
}
Limitations of EventBridge Rules
- Fixed Event Payload: The payload passed to the target is static and cannot be changed dynamically between invocations.
- Requires an Event Bus: All rule-based invocations require an event bus, adding an extra layer of configuration.
EventBridge Scheduler
Overview
The EventBridge Scheduler is a recent addition to the AWS arsenal, designed to simplify and enhance the scheduling of events. It supports many of the same scheduling capabilities as EventBridge Rules but adds new features and improvements.
Key Features
- Rate and Cron Expressions: Like EventBridge Rules, the Scheduler supports both rate and cron expressions for defining event schedules.
- One-Time Events: A unique feature of the Scheduler is the ability to create one-time events that trigger a single event at a specified time.
- Flexible Time Windows: Allows us to define a time window within which the event can be triggered, helping to stagger event delivery and avoid spikes in load.
- Automatic Retries: We can configure automatic retries for failed event deliveries, specifying the number of retries and the time interval between them.
- Dead Letter Queues (DLQs): Events that fail to be delivered even after retries can be sent to a DLQ for further analysis and handling.
Example of One-Time Events
Imagine we want to send a follow-up email to customers 21 days after they place an order. Using the Scheduler, we can create a one-time event scheduled for 21 days from the order date. When the event triggers, it invokes a Lambda function that sends the email, using the context provided when the event was created.
{
"scheduleExpression": "at(2023-06-01T00:00:00)",
"target": {
"arn": "arn:aws:lambda:region:account-id:function:sendFollowUpEmail",
"input": "{\"customerId\":\"123\",\"email\":\"customer@example.com\"}"
}
}
Comparing EventBridge Rules and Scheduler
When to Use EventBridge Rules
- Pattern-Based Event Routing: If we need to route events to different targets based on the event content, EventBridge Rules are ideal. For example, routing different order statuses to different Lambda functions.
- Complex Event Patterns: When we have complex patterns that require matching against multiple attributes, EventBridge Rules provide the necessary flexibility.
When to Use EventBridge Scheduler
- Timer-Based Invocations: For any time-based scheduling (rate or cron), the Scheduler is preferred due to its additional features like start and end times, flexible time windows, and automatic retries.
- One-Time Events: If you need to schedule events to occur at a specific time in the future, the Scheduler’s one-time event capability is invaluable.
- Simpler Configuration: The Scheduler offers a more straightforward setup for time-based events without the need for an event bus.
AWS Push Towards Scheduler
AWS seems to be steering users towards the Scheduler for timer-based invocations. In the AWS Console, when creating a new scheduled rule, you’ll often see prompts suggesting the use of the EventBridge Scheduler instead. This indicates a shift in focus, suggesting that AWS may continue to invest more heavily in the Scheduler, potentially making some of the timer-based functionalities of EventBridge Rules redundant in the future.
Summing It Up
AWS EventBridge Rules and EventBridge Scheduler are powerful tools for building event-driven architectures. Understanding their capabilities and limitations will help us choose the right tool for our needs. EventBridge Rules excel in dynamic, pattern-based event routing, while EventBridge Scheduler offers enhanced features for time-based scheduling and one-time events. As AWS continues to develop these services, keeping an eye on new features and updates will ensure that we leverage the best tools for our applications.