AWS Lambda Event Filtering & Scheduling w/Amazon EventBridge

January 17, 2022

Introduction

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.

The entire PoC (proof-of-concept) uses IaC written with Serverless Framework. You can download the template from our website, HERE.

AWS Architecture

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
  
  service: sls-eventbridge
frameworkVersion: '2'
provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221
  stage: dev
  region: ap-south-1

functions:
  scheduledService:
    handler: handler.scheduledService
    events:
      - schedule: rate(1 minute)

  invoiceService:
    handler: handler.invoiceService
    events:
      - cloudwatchEvent:
          event:            
            detail:
              operation:
                - invoice-service

  rewardService:
    handler: handler.rewardService
    events:
      - cloudwatchEvent:
          event:            
            detail:
              operation:
                - reward-service
  

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:

  
  {
	"operation":"invoice-service"
}
  

3. rewardService lambda function will only call whenever custom event pattern matches:

  
  {
	"operation":"reward-service"
}
  
  • This event would trigger invoiceService lambda as it matches the custom event pattern rule.
  
  {
	"operation":"invoice-service",
	"orderDetails":"orderDetailsValue"
}
  
  • This event would trigger rewardService lambda as it matches custom event pattern rules.
  
  {
	"operation":"reward-service",
	"orderDetails":"orderDetailsValue"
}
  
  • 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

Conclusion

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.

Serverless Handbook
Access free book

The dream team

At Serverless Guru, we're a collective of proactive solution finders. We prioritize genuineness, forward-thinking vision, and above all, we commit to diligently serving our members each and every day.

See open positions

Looking for skilled architects & developers?

Join businesses around the globe that trust our services. Let's start your serverless journey. Get in touch today!
Ryan Jones - Founder
Ryan Jones
Founder
Speak to a Guru
arrow
Edu Marcos - CTO
Edu Marcos
Chief Technology Officer
Speak to a Guru
arrow
Mason Toberny
Mason Toberny
Head of Enterprise Accounts
Speak to a Guru
arrow

Join the Community

Gather, share, and learn about AWS and serverless with enthusiasts worldwide in our open and free community.