Amazon EventBridge is a serverless event bus service that makes it easy to connect your applications with data from a variety of sources. It helps you to build event-driven architectures that are loosely coupled and distributed.
Serverless Event Router – Amazon EventBridge – Amazon Web Services
Features
- EventBridge delivers a stream of real-time data from your applications or AWS services and routes that data to targets such as AWS Lambda using the routing rules.
- EventBridge is the updated version of CloudWatch Events.
Schema Registry
- Schema of an events can be inferred.
- Schema can be versioned.
Components
Events
- Changes in your resources and sent to EventBridge
- Event Structure
- source
- detail
- detail-type
{
"version": "0",
"id": "ac51d123-45e1-460a-a1fa-e15d188c17e1",
"detail-type": "EC2 Instance State-change Notification",
"source": "aws.ec2",
"account": "123456789101",
"time": "2022-12-25T13:11:28Z",
"region": "us-east-1",
"resources": [
"arn:aws:ec2:us-east-1:123456789101:instance/i-1234567890abcdef0"
],
"detail": {
"instance-id": "i-1234567890abcdef0",
"state": "stopped"
}
}
Destinations
- Targets
- Resources or endpoints to which EventBridge sends events after matching a rule
- You can configure up to 5 destinations per rule.
- JSON data will be sent to the destination.
Rules
- Rules are the glue in the pipeline.
- A Rule matches an event and sends it to its designated (single or multiple) targets.
- Events can be matched to rules by:
- Rate-based or cron-based schedule
- Event pattern matching
- prefix/suffix matching
# example 1
# matching rule - state is "stopping" or "stopped"
{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {
"state": ["stopping", "stopped"]
}
}
# example 2
# matching rule - S3 Object Create event
# prefix, suffix, ip matching
{
"source": ["aws.s3"],
"detail-type": "Object Created",
"detail": {
"bucket": {
"name": : [ { "prefix", "mybucket" }]
},
"object": {
"key": : [ { "suffix", ".jpg" }]
},
"source-ip-address": : [ { "cidr", "10.0.0.0/24" }]
}
}
Replay
- Events are saved (archived), and we can replay them later
Event Bus
- “Event Bus” is a serverless pipeline that can deliver events from different sources.
- Every account has a default event bus.
- The default event bus is used for the events from AWS services.
- You can create a custom event bus for custom applications.
- “Rules” are associated with buses and are applied when matching events arrive.
- There is a limit: 300 rules per event bus!
Input Transformation
- You can transform input data to another using the transformation document.
- You can refer the values in the input data using “$“
{
"type": "$.detail.type"
"time-stamp": "$.time",
"instance": "$.detail.instance-id",
"state": "$.detail.state",
"resource": "$.detail.resources[0]"
}
Security
Resource-based Policy
- You can define which event can be allowed or denied to a specific event bus.
- Use cases
- aggregate all events from the accounts in your AWS Organization in a single account (single region)
"Statement": [
{
"Effect": "Allow"
"Action": "events:PutEvents",
"Principal": { "AWS:<account>" },
"Resource": "arn:aws:events:<region>:<central-account>:event-bus:<bus-name>"
}
]
Working with Lambda
- EventBridge can invoke Lambda functions asynchronously.
