EventBridge 実務Tips
EventBridge 実務Tips
Section titled “EventBridge 実務Tips”AWS CLI
Section titled “AWS CLI”# ルール作成(スケジュール)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\"}" } ]'Lambda統合例(Python)
Section titled “Lambda統合例(Python)”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'] }) } ] )ベストプラクティス
Section titled “ベストプラクティス”設計: - イベント駆動設計 - 疎結合アーキテクチャ - スキーマレジストリ活用
パフォーマンス: - イベントパターン最適化 - バッチ処理活用
運用: - CloudWatch Logs統合 - DLQ設定トラブルシューティング
Section titled “トラブルシューティング”# ルール一覧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