[EventBridge]Create a Serverless EventBus Using Amazon EventBridge

overview

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.

  • 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.

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

  • 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

  • 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 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.

--

--

Cloud security engineer https://www.linkedin.com/in/takahiro-oda-881423197/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store