Logo

dev-resources.site

for different kinds of informations.

Lambda Scheduling & Event Filtering with EventBridge using Serverless Framework

Published at
12/24/2023
Categories
aws
serverless
eventbridge
lambda
Author
jaymitb
Categories
4 categories in total
aws
open
serverless
open
eventbridge
open
lambda
open
Author
7 person written this
jaymitb
open
Lambda Scheduling & Event Filtering with EventBridge using Serverless Framework

Lambda Scheduling & Event Filtering with EventBridge using Serverless Framework

Application Stack:
  • Serverless Framework
  • AWS EventBridge
  • AWS Lambda
  • NodeJs

In this post, We will take a look at a use-case that allows us to schedule lambda function call and custom event pattern matching based lambda function call with EventBridge using Serverless Framework.

AWS Architecture:

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.

1. Install Serverless Framework



npm install -g serverless


Enter fullscreen mode Exit fullscreen mode

2. Create basic NodeJs type template project using Serverless Framework



serverless create --template aws-nodejs


Enter fullscreen mode Exit fullscreen mode

By default it will generate a folder structure like this:

  • handler.js
  • serverless.yml

Default serverless.yml will have below IaC code:



service: default-aws-nodejs
frameworkVersion: '2'
provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: 20201221

functions:
  hello:
    handler: handler.hello


Enter fullscreen mode Exit fullscreen mode

Default handler.js will have below NodeJs code:



'use strict';

module.exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
};
};

Enter fullscreen mode Exit fullscreen mode



  1. Modify serverless.yml Infrastructure as code to provision needful Lambda functions with EventBridge as Event Source




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

Enter fullscreen mode Exit fullscreen mode



  1. Modify handler.js with below sample demo code




'use strict';

const scheduledService = async (event) => {
console.log(event);
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'scheduledService lambda function call through EventBridge scheduling rule!',
input: event,
},
null,
2
),
};
};

const invoiceService = async (event) => {
console.log(event);
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'invoiceService lambda function call through EventBridge custom pattern matching!',
input: event,
},
null,
2
),
};
};

const rewardService = async (event) => {
console.log(event);
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'rewardService lambda function call through EventBridge custom pattern matching!',
input: event,
},
null,
2
),
};
};

module.exports = {
scheduledService,
invoiceService,
rewardService
};

Enter fullscreen mode Exit fullscreen mode



  1. Create AWS IAM user with programmatic access and give needful permission that allows Serverless Framework to provision resources

The Serverless Framework needs access to your cloud provider account so that it can create and manage resources on your behalf.

During Creating AWS IAM user copy API Key & Secret.

6. Configure AWS Credentials for Serverless Framework Deployment



serverless config credentials --provider aws --key xxxxx --secret xxxxx

Enter fullscreen mode Exit fullscreen mode



  1. Deploy using Serverless Framework




serverless deploy

Enter fullscreen mode Exit fullscreen mode


> serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Service files not changed. Skipping deployment...
Service Information
service: sls-eventbridge
stage: dev
region: ap-south-1
stack: sls-eventbridge-dev
resources: 18
api keys:
None
endpoints:
None
functions:
scheduledService: sls-eventbridge-dev-scheduledService
invoiceService: sls-eventbridge-dev-invoiceService
rewardService: sls-eventbridge-dev-rewardService
layers:
None

Enter fullscreen mode Exit fullscreen mode



  1. Amazon EventBridge and AWS Lambda Screenshots

AWS Lambda Functions List

AWS Lambda Functions List

AWS Lambda Functions Event Source Configuration

AWS Lambda Functions List

Amazon EventBridge Default Bus Scheduling Rules

Amazon EventBridge Default Bus Rules

Amazon EventBridge Default Bus Custom Event Pattern Rule

Amazon EventBridge Default Bus Custom Event Pattern Rule

Amazon EventBridge Default Bus Custom Event Pattern Rule

Amazon EventBridge Default Bus Custom Event Pattern Rule

9. Remove already provisioned resources



serverless remove

Enter fullscreen mode Exit fullscreen mode




Relevant Resources Link

eventbridge Article's
30 articles in total
Favicon
API Destinations with Amazon EventBridge
Favicon
How to Leverage EventBridge for Building Decoupled Event-Driven Systems
Favicon
Creating Serverless Webhooks on AWS CDK
Favicon
Building Faster Event-Driven Architectures: Exploring Amazon EventBridge’s New Latency Gains
Favicon
Disaster recovery for AWS Aurora
Favicon
Building a Scalable Job Queue System with AWS and Laravel
Favicon
AWS Serverless: How to Stop EC2 using Event Bridge and Lambda
Favicon
Monitoring AWS ECS Deployment failures
Favicon
Amazon EventBridge Pipes now supports customer managed KMS keys
Favicon
This stranger EventBus Mesh
Favicon
An Alternative to Batch Jobs: Scheduling Events with EventBridge Scheduler
Favicon
Momento added as an Amazon EventBridge API destination!
Favicon
EventBridge: working around API Destination 5s maximum client timeout constraint, using Lambda PowerTools idempotency
Favicon
Event-Driven Magic: Exploring AWS EventBridge
Favicon
Event-Driven Architecture: reconcile Notification and Event-Carried State Transfer patterns
Favicon
Architecture orientée événement : réconcilier Notifications et Evénements "Complets"
Favicon
Executing long running tasks with AppSync
Favicon
How To Run A Serverless Scheduled Function Using AWS Lambda & EventBridge
Favicon
Automate AWS Cost & Usage report using Event Bridge, Lambda, SES, S3 & AWS Cost Explorer API
Favicon
Leveraging the SDK to Publish an Event to EventBridge with Lambda and Rust
Favicon
How Epilot Builds a Powerful Webhook Feature with AWS
Favicon
Setting up AppSync subscriptions for out-of-band updates with Eventbridge pipes
Favicon
How CloudWatch Network Monitor Performs Connectivity Test to EC2 Instances
Favicon
How to Get Custom Email Notification for EC2 State Changes Using EventBridge & Lambda