Skip to content

EventBridge 実務Tips

Terminal window
# ルール作成(スケジュール)
aws events put-rule \
--name my-scheduled-rule \
--schedule-expression "cron(0 12 * * ? *)"
# ルール作成(イベントパターン)
aws events put-rule \
--name my-event-rule \
--event-pattern '{
"source": ["aws.ec2"],
"detail-type": ["EC2 Instance State-change Notification"],
"detail": {"state": ["running"]}
}'
# ターゲット追加
aws events put-targets \
--rule my-event-rule \
--targets "Id"="1","Arn"="arn:aws:lambda:ap-northeast-1:123456789012:function:my-function"
# カスタムイベント送信
aws events put-events \
--entries '[
{
"Source": "my.application",
"DetailType": "UserSignup",
"Detail": "{\"userId\":\"123\",\"email\":\"user@example.com\"}"
}
]'
import boto3
client = boto3.client('events')
def lambda_handler(event, context):
# EventBridgeイベント処理
print(f"Event: {event}")
source = event['source']
detail_type = event['detail-type']
detail = event['detail']
# カスタムイベント送信
client.put_events(
Entries=[
{
'Source': 'my.application',
'DetailType': 'ProcessCompleted',
'Detail': json.dumps({
'status': 'success',
'userId': detail['userId']
})
}
]
)
設計:
- イベント駆動設計
- 疎結合アーキテクチャ
- スキーマレジストリ活用
パフォーマンス:
- イベントパターン最適化
- バッチ処理活用
運用:
- CloudWatch Logs統合
- DLQ設定
Terminal window
# ルール一覧
aws events list-rules
# ターゲット確認
aws events list-targets-by-rule --rule my-event-rule
# メトリクス確認
aws cloudwatch get-metric-statistics \
--namespace AWS/Events \
--metric-name Invocations \
--dimensions Name=RuleName,Value=my-event-rule