AWS Lambda Event Filtering & Scheduling Using Amazon EventBridge
In this article, we will take a look at a use-case that allows us to schedule lambda function calls and custom event pattern matching based on lambda function calls using EventBridge.
Above is an AWS Serverless architecture diagram showing EventBridge calling Lambda, using scheduled rules, and filtering lambda calls using custom even pattern matching rules. Later on in the article, we will talk in more detail about how this architecture works including a Serverless Framework IaC template.
What is EventBridge?
Amazon EventBridge is a serverless event bus that makes it easier to build event-driven applications at scale using events generated from your applications, integrated Software-as-a-Service (SaaS) applications, and AWS services.
How does EventBridge work?
EventBridge connects different applications using events. EventBridge is able to receive events from different sources and route that event to a specific target based on pre-defined/custom event pattern matching rules on the serverless event bus. Apart from event routing, you are also able to set up scheduling rules on the default bus.
Why use EventBridge vs other serverless architecture options?
- Pre-defined and custom event pattern matching
- Decoupling and simplified event routing
- Multiple third-party SaaS provider support
- 90+ AWS services as a source
- 15+ AWS services as a target
- Invoke your targets on a schedule
- Set up routing rules for different Serverless Event Bus
- Build event-driven applications at scale
Provisioning AWS Lambda With EventBridge rule as Event Source
If you look below, you can see that the Serverless Framework IaC will create these resources:
- 3 Lambda functions with EventBridge rule as Event Source Trigger
- 3 rules for default event bus in EventBridge
EventBridge to Lambda functions call Use-Cases
1. scheduledService lambda function scheduled to call every 1 minute
2. invoiceService lambda function will only call whenever custom event pattern matches:
3. rewardService lambda function will only call whenever custom event pattern matches:
- This event would trigger invoiceService lambda as it matches the custom event pattern rule.
- This event would trigger rewardService lambda as it matches custom event pattern rules.
- In case EventBridge bus rules do not match with the event pattern, then our Lambda functions will not be invoked.
EventBridge Event Bus Rules and Lambda Console Screenshots
- EventBridge Default Bus Scheduling Rule
- EventBridge Default Bus Custom Event Pattern Matching Rule #1
- EventBridge Default Bus Custom Event Pattern Matching Rule #2
- Lambda function List
- Scheduled Lambda with EventBridge Scheduled Rule as Trigger
- Invoice service Lambda with EventBridge Custom Event Pattern Rule as Trigger
- Reward service Lambda with EventBridge Custom Event Pattern Rule as Trigger
Simple, flexible, and fully managed, the EventBridge event bus connects applications together by ingesting and processing event data across your application, AWS services, and SaaS applications. You can set up pre-defined/custom even pattern matching rules to filter and route events to targets.
Furthermore, with the EventBridge scheduling rule feature, we are able to schedule tasks using a Lambda function.
EventBridge custom event pattern matching rule will help to decouple multiple micro-services and help us to call specific micro-services only when the pattern matches instead of calling all other relevant micro-services every time.