Amazon EventBridge

  • Topics

Part of the AWS Serverless ecosystem, Amazon EventBridge is a serverless event bus that was built on top of the existing AWS CloudWatch Events API. AWS CloudWatch Events enables developers to integrate many of the AWS services through events. For example, if there is a change in the state of an EC2 instance, CloudWatch Event can trigger an event to AWS Lambda to take an action. However, this service was limited to AWS services only.

As more and more SaaS partners, and even custom applications, started to integrate with the AWS cloud, there was a need for a service that can enable this integration seamlessly with little code configuration.

EventBridge provides a bus that can deliver real-time data in the form of events from registered Software-as-a-Service (SaaS) applications, your own applications, and AWS services — and routes the events to the configured AWS target services, such as SNS, SQS, Lambda, and many more.

AWS EventBridge Building Blocks

Before we go deep into how EventBridge works, let us first understand the components this service contains:

Events – An event is a real-time change in a system, data, or environment. This change can be either in your application or in an AWS service or a SaaS partner service.

Event sources – An event source is used to ingest events from a SaaS partner– or your own app — to an AWS customer account.

Event buses – To receive an event from event sources, you need to configure an event bus in an AWS account. These event buses can be of several types:

  • Default event bus – Created by default and receives events from any AWS service.
  • Custom event bus – Receives events from custom applications you create and must be created manually.
  • Partner event bus – Receives events from SaaS partner applications and must be created manually.

Rules – A rule is created and associated with an event bus from which it will scan the events to be matched. When incoming events match with the rule, it routes them to targets for processing. You can enable parallel processing also by routing to multiple targets. A rule can also parse the JSON and filter/customize it to send only certain parts of JSON or overwrite the text.

Targets – A target processes events. It supports JSON format for events. Targets can include several AWS services such as Amazon EC2 instances, Lambda functions, Kinesis streams, Amazon ECS tasks, Step Functions, Amazon SNS topics, and Amazon SQS.

How EventBridge Works

In layman’s terms, Amazon EventBridge is a serverless event bus that supports the publish/subscribe model. SaaS applications publish events to this event bus and it fans out the events to one or multiple AWS target services.

The first step is to create a rule. A rule has to be created in the same region where the target is. A rule requires an event pattern. It uses the pattern to match the incoming events. Below is an example that processes all EC2 instance-termination events:

{
  "source": ["aws.ec2"],
  "detail-type": ["EC2 Instance State-change Notification"],
  "detail": {
    "state": ["terminated"]
  }
}

Each rule has to be associated with an event bus. You can select one of the event buses based on your requirements discussed above. Now, select a target to which the events will be routed once matched with the event pattern by the rule.

The next step is to create an event bus. every AWS account creates one default event bus, which receives events emitted by AWS services. You can also configure your custom applications to send events to the default event bus. If you need to integrate with a SaaS partner application, you can create a partner event bus.

Once the basic flow is set up, you can enable additional features such as content-based event filtering, transforming target inputs, schema registry, and so on.

Content-based event filtering

Content-based event filtering is used to write complex rules in which allow events to be processed when filtering conditions are met.

For instance, you might want to pass the event only if it is missing a specific field in it or if events are coming from a specific IP address.

It supports the following types of pattern matching:

  • Prefix Matching
  • Anything-but Matching
  • Numeric Matching
  • IP Address Matching
  • Exists Matching
  • Complex Example with Multiple Matching

Transforming Target Inputs

In specific use cases, you might want to customize the text of event fields before passing them to a target so that these fields provide more meaningful information to process them further.

Let’s assume this is the event being processed:

{
  "version": "0",
  "id": "6bf42112-1422-2cf3-a730-92db853d1542",
  "detail-type": "EC2 Instance State-change Notification",
  "source": "aws.ec2",
  "account": "91204489383",
  "time": "2021-02-05T11:13:14Z",
  "region": "ap-southeast-1",
  "resources": [
    "arn:aws:ec2:ap-southeast-1:91204489383:instance/i-zysa0111"
  ],
  "detail": {
    "instance": "i-999999999",
    "state": "RUNNING"
  }
}

When you create a rule, you can select the Input Transformer option under Configure input. It provides two text boxes: one for the Input Path and one for the Input Template.

The Input Path can be defined with the variables instance and state:

{
  "instance" : "$.detail.instance",
  "state" : "$.detail.state"
}

The Input Template is used to transform the information to pass to your target. You can create a template that passes either a string or JSON to the target. For example:

Template

"instance <instance> is in <state>"

Output

"instance i-999999999 is in RUNNING"

EventBridge Schema Registry

So far we have seen that events get published and consumed by targets. But now, from a developer perspective, if you need to read these events at the target end and process them, you need to manually write code matching up with the event structure being received.

To solve this problem, AWS launched Schema Registry to centralize and share the event structure which can be downloaded as objects with popular programming languages such as Java and Python. You can generate events from these schemas or generate code binding for these schemas.

Summary

As more and more SaaS providers are building solutions around the AWS serverless ecosystem, especially for monitoring, analytics, and error management, the EventBridge service is going to make this integration between SaaS applications and AWS services seamless. The earlier solution that was used for the same use case had drawbacks. The EventBridge solution requires nearly no-code configuration, enables retry policies in case of failure, and also provides security with little work.