Event-Driven with Event Bridge.

High level structure

image

let us know bit about the Event-Bridge

Amazon EventBridge is a serverless event bus service that makes it easy to connect your applications with data from a variety of sources. EventBridge delivers a stream of real-time data from your own applications, Software-as-a-Service (SaaS) applications, and AWS services and routes that data to targets such as AWS Lambda. You can set up routing rules to determine where to send your data to build application architectures that react in real time to all of your data sources.

The key concept that are mandatory in understanding the Event Bridges are :

  • Events - An event indicates a change in an environment. As you can see in high level design the Events can be SaaS Apps, Microservices and any of your custom app.

  • Rules - A rule matches incoming events and routes them to targets for processing. A single rule can route to multiple targets, all of which are processed in parallel. Rules aren't processed in a particular order. This enables different parts of an organization to look for and process the events that are of interest to them. A rule can customize the JSON sent to the target, by passing only certain parts or by overwriting it with a constant.

  • Targets - A target processes events. Targets can include Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions state machines, Amazon SNS topics, Amazon SQS queues, and built-in targets. A target receives events in JSON format.

  • Event buses - An event bus receives events. When you create a rule, you associate it with a specific event bus, and the rule is matched only to events received by that event bus. Your account has one default event bus, which receives events from AWS services. You can create custom event buses to receive events from your custom applications. You can also create partner event buses to receive events from SaaS partner applications.

We will use Event Bridge to create an event bus, route events to different targets like SNS, API Gateway and Step Function in our case,and use scheduling expressions to create recurring events.

Let's create our First Event Bridge and Target.

Event Bridge and Target

image

you will create a custom EventBridge event bus, Orders, and an EventBridge rule, OrderDevRule, which matches all events sent to the Orders event bus and sends the events to a CloudWatch Logs log group, /aws/events/orders. See the diagram above:

The technique of logging all events to CloudWatch Logs is useful when implementing EventBridge rules.

Create a Custom Event Bus

  1. Open The AWS Management Console for the Event Bridge.

  2. On the EventBridge homepage, under Events, select Event buses from the left navigation.

    EB1

  1. Click Create event bus.

    EB2

  1. Name the event bus Orders.

  2. Leave Event archive and Schema discovery disabled, Resource-based policy blank.

  3. Click Create.

    EB3

Setup Amazon CloudWatch target

  1. From the left-hand menu, select Rules.

  2. From the Event bus dropdown, select the Orders event bus

  3. Click Create rule

    EB4

  1. Define rule detail:
  • Add OrdersDevRule as the Name of the rule

  • Add Catchall rule for development purposes for Description

  • Select Rule with an event pattern for the Rule type

    EB5

  1. In the next step, Build event pattern.

    EB6

    Under Event pattern, further down the screen, enter the following pattern to catch all events from com.aws.orders:

     {
        "source": ["com.aws.orders"]
     }
    

    Select next.

  2. Select your rule target:

  • From the Target dropdown, select CloudWatch log group

  • Name your log group /aws/events/orders

    EB7

  1. Skip through the configure tags section, review your rule configuration and click Create.

Test your Dev Rule

  1. Select the EventBridge tab in the Event Generator

  2. Make sure that the Event Generator is populated with the following :

  • AWS Region should be to the region in which you are running the workshop

  • Event Bus selected to Orders

  • Source should be com.aws.orders

  • In the Detail Type add Order Notification

  • JSON payload for the Detail Template should be:

      {
       "category": "lab-supplies",
       "value": 415,
       "location": "eu-west"
      }
    

    EB8

  1. Click Publish.

  2. Open the AWS Management Console for Cloud Watch.

  3. Choose Log groups in the left navigation and select the /aws/events/orders log group.

    EB9

  1. Select the Log stream.

    EB10

  1. Toggle the log event to verify that you received the event.

    EB11

Review Event Structure

In the following sections, you will use event data to implement EventBridge custom rules to route events. Due to the OrdersDevRule that you created in this section, all events to the Orders event bus will be sent to CloudWatch Logs, which you can use to view sample data in order to implement and troubleshoot rules matching logic.

For your reference:

{
    "version": "0",
    "id": "c04cc8c1-283c-425e-8cf6-878bbc67a628",
    "detail-type": "Order Notification",
    "source": "com.aws.orders",
    "account": "111111111111",
    "time": "2020-02-20T23:10:29Z",
    "region": "us-west-2",
    "resources": [],
    "detail": {
        "category": "lab-supplies",
        "value": 415,
        "location": "eu-west"
    }
}

Working with EventBridge rules

image

Rules match incoming events and routes them to targets for processing. A single rule can route to multiple targets, all of which are processed in parallel. Rules aren't processed in a particular order. A rule can customize the JSON sent to the target, by passing only certain parts or by overwriting it with a constant. EventBridge supports 28+ AWS service targets!

In this project, you will walk through the steps to create an Orders event bus rule to match an event with a com.aws.orders source and to send the event to an Amazon API Gateway endpoint, invoke a AWS Step Function, and send events to an Amazon Simple Notification Service (Amazon SNS) topic.