AWS EventBridge the serverless event bus
Tags: aws, serverless, eventbridge, eventbus
Published: 2022-11-15 22:39:09

Okey so let's talk about Amazon EventBridge!

## What is Amazon EventBridge?
Amazon EventBridge is a serverless, fully managed and scalable event bus that lets you receive, filter, transform, route, and deliver events. It also enables integrations between AWS services, your own services and different SaaS providers.

> EventBridge was formerly called Amazon CloudWatch Events. Amazon CloudWatch Events and EventBridge are the same underlying service and API, however, EventBridge provides more features (source: [AWS](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html)).

## Why Amazon EventBridge?
- It's serverless meaning that you only pay for what you use
- More possibilities than SNS/SQS
- Create your own filters
- Supports over 42 Amazon EventBridge SaaS app integrations
- Easy to get started with!

## Event structure
The following event shows a simple AWS event from Amazon EC2 (source: [AWS](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html)):

```json
{
  "version": "0",
  "id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
  "detail-type": "EC2 Instance State-change Notification",
  "source": "aws.ec2",
  "account": "111122223333",
  "time": "2017-12-22T18:43:48Z",
  "region": "us-west-1",
  "resources": [
    "arn:aws:ec2:us-west-1:123456789012:instance/i-1234567890abcdef0"
  ],
  "detail": {
    "instance-id": "i-1234567890abcdef0",
    "state": "terminated"
  }
}
```

## Rules
Let's have a look of some example rules.

Assume you want to get all events with the source `aws.ec2` only.

```
{
    "source": ["aws.ec2"]
}
```

Or maybe you want to match events from Amazon EC2 or AWS Fargate:

```
{
    "source": ["aws.ec2", "aws.fargate"]
}
```
## Advanced rules
You do also have advanced rules that you can use. Assume the following AWS event for example (source: [AWS](https://aws.amazon.com/blogs/compute/reducing-custom-code-by-using-advanced-rules-in-amazon-eventbridge/)):

```json
{
  "Source": "custom.myATMapp",
  "EventBusName": "default",
  "DetailType": "transaction",
  "Time": "Wed Jan 29 2020 08:03:18 GMT-0500",
  "Detail": {
    "action": "withdrawal",
    "location": "NY-NYC-001",
    "amount": 300,
    "result": "approved",
    "transactionId": "123456",
    "cardPresent": true,
    "partnerBank": "Example Bank",
    "remainingFunds": 722.34
  }
}
```

**Transactions where the amount is over $300:**

```json
{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "amount": [ { "numeric": [ ">", 300 ] } ]
  }
}
```

**All ATMs in New York City:**

```json
{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "location": [ { "prefix": "NY-NYC-" } ]
  }
}
```

**ATM customers using a third-party bank account:**

```json
{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "partnerBank": [ { "exists": true } ]
  }
}
```

**Combined filter:**
You can also use a powerful combine filter to create use-cases that are more complex.

```json
{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "result": [ "approved" ],
    "partnerBank": [ { "exists": false } ],
    "location": [ { "anything-but": "NY-NYC-002" }]
  }
}
```

> A good thing to think about is to use a single rule per subscriber.

## Summary
Amazon EventBridge make it easier for you to develop event-driven systems. There are multiple targets built in such as API destinations, API Gateway, CloudWatch log group, EC2, Kinesis stream, Lambda function, SNS topic, SQS queue and Step Functions state machine. Create your rules for the incoming events and send them to your specific targets for processing in a really smooth way. All in all, really worth a try!

Any comments, questions or discussions are always welcome!