DEV Community

Cover image for AWS EventBridge the serverless event bus
Rashwan Lazkani
Rashwan Lazkani

Posted on

AWS EventBridge the serverless event bus

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

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):

{
  "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"
  }
}
Enter fullscreen mode Exit fullscreen mode

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"]
}
Enter fullscreen mode Exit fullscreen mode

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

{
    "source": ["aws.ec2", "aws.fargate"]
}
Enter fullscreen mode Exit fullscreen mode

Advanced rules

You do also have advanced rules that you can use. Assume the following AWS event for example (source: AWS):

{
  "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
  }
}
Enter fullscreen mode Exit fullscreen mode

Transactions where the amount is over $300:

{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "amount": [ { "numeric": [ ">", 300 ] } ]
  }
}
Enter fullscreen mode Exit fullscreen mode

All ATMs in New York City:

{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "location": [ { "prefix": "NY-NYC-" } ]
  }
}
Enter fullscreen mode Exit fullscreen mode

ATM customers using a third-party bank account:

{
  "source": [ "custom.myATMapp" ],
  "detail-type": [ "transaction" ],
  "detail": {
    "partnerBank": [ { "exists": true } ]
  }
}
Enter fullscreen mode Exit fullscreen mode

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

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

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!

Top comments (0)