[EventBridge]Create a Serverless EventBus Using Amazon EventBridge

Takahiro Oda
5 min readJan 11, 2022

overview

you’ll practice creating and configuring a serverless EventBus using Amazon EventBridge. When you’re finished with this lab, you’ll have a good understanding of setting up rules to consume events from an EventBus and configuring targets.

Amazon EventBridge

  • fully managed, pay-as-you-go
  • native integration with SaaS providers
  • 90 AWS services as sources
  • 17 AWS services as targets
  • $1 per million events put into the pus
  • No additional cost for delivery

Create an Eventbus in Amazon EventBridge Rule

You have been tasked to create an EventBridge rule and configure the Lambda function as the target to receive the events.

you have been asked to implement a messaging system that can be used to build decoupled event driven applications, fan-out messages to multiple targets based on filter rules, and process them in a scalable manner without losing any messages.

  • Name: forward-to-lambda.
  • Choose Event pattern as Define pattern.
  • Select Event matching pattern as Pre-defined pattern by service.
  • In the Service provider dropdown, select All Events.
  • This rule captures all the events published onto the event bus.
  • Select event bus: AWS default event bus.
  • ensure Enable the rule on the selected event bus is enabled/active.
  • Under Select targets, choose Target as Lambda Function.
  • Function: test
  • Leave the rest as default and click Create.
  • Note: You can create up to 5 targets per rule. Now, test the rule by sending an event into the EventBridge bus.

Create and Deploy a Lambda Function to Consume Events from Event Bus

  • In the AWS console, type Lambda in the search box at the top and hit enter.
  • Click Create function.
  • Choose Author from scratch.
  • Function name: test.
  • Runtime: Node.js 14.x.
  • Leave the rest as default and click Create function.
  • You will be redirected to the globomantics-consumer-function page.
  • Select the test and replace the existing Function code in the index.js file with the code below: and Click Deploy.

exports.handler = async (event) => {

console.log(JSON.stringify(event))

return "Processed events successfully";

};

Setup Lambda Function as Target

  • In the Amazon EventBridge console, click Event buses on the left navigation pane.
  • In the center, click the Send events button at the top.
  • Select Event bus: default
  • Event source: com.globomantics.orders
  • Detail type: Events related to globomantics orders.
  • Event Detail:
  • {
  • "orderId": 1111,
  • "quantity": 15,
  • "cost": "25",
  • "currency": "USD",
  • "productId": 2222
  • }

Configure an Additional Target with an Advanced EventBridge Rule

you have created a rule to route all events to Lambda function. You have been tasked to route the events to SQS queue in addition to Lambda function, if the quantity field in the event payload is greater than 20.

  • In the AWS console, type SQS in the search box at the top and hit enter.
  • Click Create queue.
  • In the Details page,
  • Choose Type as Standard.
  • Name: test
  • Leave the rest as default and click Create queue.
  • In the AWS Console, type EventBridge in the search box at the top and hit enter.
  • In the left navigation pane, click Rules under Events section.
  • Click Create rule.
  • In the Create rule page, type the following:
  • Name: forward-to-sqs
  • Choose Event pattern as Define pattern.
  • Select Event matching pattern as Custom pattern
  • In the Event pattern, copy and paste the following:
  • {
  • "detail": {
  • "quantity": [{
  • "numeric": [">", 20]
  • }]
  • }
  • }
  • Click Save.
  • Select event bus: Custom or partner event bus.
  • Select test and ensure Enable the rule on the selected event bus is enabled/active.
  • Under Select targets, choose Target as SQS queue.
  • Function: test
  • Leave the rest as default and click Create.

Test the EventBridge Rules

Now that you have configured 2 rules, one that forwards all events to Lambda function and the other rule that forwards the event to SQS queue, only if the quantity field in the event payload is greater than 20, you have been tasked to test the end-to-end flow.

First you will test using the quantity as 45.

  • In the Amazon EventBridge console, click Event buses on the left navigation pane
  • In the center, click the Send events button at the top.
  • In the Send events page,
  • Select Event bus: test
  • Event source: com.globomantics.orders
  • Detail type: Events related to globomantics orders.
  • Event Details:
  • {
  • "orderId": 2222,
  • "quantity": 45,
  • "cost": "30",
  • "currency": "USD",
  • "productId": 444
  • }

Click Send.

  • Click Queues.
  • In the center, click globomantics-queue.
  • In the globomantics-queue page, click Send and receive messages button at the top.
  • In the Send and receive messages page, click Poll for messages.
  • You will see the message in the queue. Click this message and check the body. You can also check the Lambda CloudWatch logs and will see the processed events in the logs.

--

--