Amazon EventBridge 基本概念
EventBridgeはサーバーレスのイベントバスサービス。イベントソースからイベントを受け取り、ルールに基づいてターゲットにルーティングする
出典: Event bus concepts in Amazon EventBridge
An event bus is a router that receives events and delivers them to zero or more destinations, or targets. Use an event bus when you need to route events from many sources to many targets, with optional transformation of events prior to delivery to a target.
全体像
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ イベントソース │ │ イベントバス │ │ ターゲット │
│ │ │ │ │ │
│ - AWSサービス │────▶│ ┌───────────┐ │────▶│ - Lambda │
│ - カスタムアプリ │ │ │ ルール │ │ │ - Step Functions│
│ - SaaSパートナー │ │ │(フィルタ) │ │ │ - SNS/SQS │
│ │ │ └───────────┘ │ │ - API Gateway │
└─────────────────┘ └─────────────────┘ └─────────────────┘
イベント(Event)
環境の変化を示すJSONオブジェクト
出典: Events in Amazon EventBridge
An event indicates a change in an environment such as an AWS environment, a SaaS partner service or application, or one of your applications or services.
イベントの例
- EC2インスタンスの状態変化(pending → running)
- S3バケットへのオブジェクトアップロード
- CloudFormationスタックの作成・更新・削除
- カスタムアプリケーションからの独自イベント
イベントの構造
{
"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"
}
}| フィールド | 説明 |
|---|---|
| version | イベントスキーマのバージョン(常に “0”) |
| id | イベントの一意識別子 |
| detail-type | イベントの種類を示す文字列 |
| source | イベントの発生元(AWSサービスは aws.xxx 形式) |
| account | イベントが発生したAWSアカウントID |
| time | イベント発生時刻(ISO 8601形式) |
| region | イベントが発生したリージョン |
| resources | 関連するリソースのARN |
| detail | イベント固有のデータ(内容はイベントタイプにより異なる) |
イベントバス(Event Bus)
イベントを受信し、ルールに基づいてターゲットに配信するルーター
イベントバスの種類
| 種類 | 説明 |
|---|---|
| デフォルトイベントバス | 各アカウントに1つ存在。AWSサービスからのイベントを自動受信 |
| カスタムイベントバス | 独自に作成。特定のイベントのみを受信するよう設定可能 |
| パートナーイベントバス | SaaSパートナーからのイベントを受信 |
ユースケース
- 異なるワークロード間のブローカーとして使用
- PII(個人情報)を含むイベントと含まないイベントを分離
- 複数のイベントバスから中央のイベントバスにイベントを集約
ルール(Rule)
受信したイベントを評価し、条件に一致したイベントをターゲットに送信する
出典: Event bus concepts in Amazon EventBridge
A rule receives incoming events and sends them as appropriate to targets for processing.
ルールの種類
- イベントパターンルール: イベントの内容に基づいてフィルタリング
- スケジュールルール: 定期的にターゲットを呼び出す(cron式やrate式)
イベントパターンの例
EC2インスタンスが terminated になったイベントのみをマッチさせる:
{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {
"state": ["terminated"]
}
}出典: Creating Amazon EventBridge event patterns
An event pattern defines the data EventBridge uses to determine whether to send the event to the target. If the event pattern matches the event, EventBridge sends the event to the target.
ターゲット(Target)
イベントパターンにマッチしたイベントを受け取るリソースまたはエンドポイント
主なターゲット
- AWS Lambda
- Step Functions
- Amazon SNS
- Amazon SQS
- Amazon Kinesis
- API Gateway
- ECS タスク
- Systems Manager
- API Destinations(外部HTTPエンドポイント)
入力変換(Input Transformer)
イベントをターゲットに送信する前にデータを加工する機能
出典: Amazon EventBridge input transformation
You can customize the text from an event before EventBridge passes the information to the target of a rule.
なぜ必要か
- ターゲット側の処理を簡素化(必要なデータだけを渡す)
- ターゲットが期待する形式に変換(外部API連携など)
- 不要なメタデータを除去してペイロードを軽量化
- 異なるソースからのイベントを統一形式に正規化