Event-Driven with Event Bridge.
High level structure
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
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
On the EventBridge homepage, under Events, select Event buses from the left navigation.
Click Create event bus.
Name the event bus Orders.
Leave Event archive and Schema discovery disabled, Resource-based policy blank.
Click Create.
Setup Amazon CloudWatch target
From the left-hand menu, select Rules.
From the Event bus dropdown, select the Orders event bus
Click Create rule
- 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
In the next step, Build event pattern.
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.
Select your rule target:
From the Target dropdown, select CloudWatch log group
Name your log group /aws/events/orders
- Skip through the configure tags section, review your rule configuration and click Create.
Test your Dev Rule
Select the EventBridge tab in the Event Generator
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" }
Click Publish.
Open the AWS Management Console for Cloud Watch.
Choose Log groups in the left navigation and select the /aws/events/orders log group.
Select the Log stream.
Toggle the log event to verify that you received the event.
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
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.