# Amazon EventBridge 簡介
###### tags: `AWS`,`EventBridge`
## 簡介
- Serverless Event Router

## EventBridge 架構
根據 **Event Patterns** 設定的規則,Event 中特定的參數將會被 Event Bus 送往特定的目的地
```graphviz
digraph hierarchy {
nodesep=1.0 // increases the separation between nodes
node [color=Red,fontname=Courier,shape=box] //All nodes will this shape and colour
edge [color=Blue, style=dashed] //All the lines look like this
Event->{EventBus}
EventBus->{ExportLambda, TransactionLambda, XXXLambda}
}
```
### Event Patterns
- `source`, `detail-type`, `detail` 組成,設定 Rule 將符合 Patterns 的 evnet 指定特定目的地,event bus 將會根據 Rule 發送
```json
{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {
"state": ["terminated"]
}
}
```
Event:
```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"
}
}
```
- [Best practices](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-patterns-best-practices.html)
### Event retry policy
若未成功傳送至 target 時,EventBridge 預設在 24 hr 內重新發送,最多重送 185 次,重送間隔參考[ exponential back off and jitter](https://aws.amazon.com/tw/blogs/architecture/exponential-backoff-and-jitter/),若仍失敗,event 將被丟棄
如果是很重要的 event,可設定 DLQ(dead-letter queue) 來收集被丟棄的 event
### Quotas
- 每個帳號預設可建 100 個 event buses
- 每個 event bus rule 上限預設 300
- event pattern 預設最多 2048 characters
- event 內容最多 256KB
- PutEvents API 每秒的最大請求數
- 東京(ap-northeast-1): 1200
- 新加坡(ap-southeast-1): 1200
- 其他可參考 [EventBridge quotas](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html)
## EventBridge V.S. Queue
Queue 也可以做到類似 EventBridge 的效果,差異在於 EventBridge 是依據設定的 Patterns 作分流,分流部分由 EventBridge 完成。而 Queue 必須自行實作這部分,下面例子是 Queue 會 Trigger 自行實作的 RouterLambda,RouterLambda 再根據 Event 參數做分流:
```graphviz
digraph hierarchy {
nodesep=1.0 // increases the separation between nodes
node [color=Red,fontname=Courier,shape=box] //All nodes will this shape and colour
edge [color=Blue, style=dashed] //All the lines look like this
Event->{Queue}
Queue->{RouterLambda}
RouterLambda->{ExportLambda, TransactionLambda, XXXLambda}
}
```