0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

概要

AWS環境の運用で飛んでくるアラート(EventBridge経由のSecurity Hubのイベントなど)はJSON形式で、内容を把握するのに時間がかかります。本記事ではAmazon BedrockでAWSのアラートを自動要約して通知するためのサンプルコードを記載します。

アーキテクチャ

al01.png

  1. Amazon EventBridgeがアラートイベントを検知
  2. AWS Lambdaがイベントを受け取り、Amazon Bedrockへ要約を依頼
  3. Amazon Bedrockがアラート内容を日本語で要約
  4. Amazon SNSで要約結果をメール通知

Lambdaコード

import json
import boto3
import os

SNS_TOPIC_ARN = os.environ["SNS_TOPIC_ARN"]
MODEL_ID = "jp.anthropic.claude-haiku-4-5-20251001-v1:0"

bedrock = boto3.client("bedrock-runtime")
sns = boto3.client("sns")

def lambda_handler(event, context):
    finding_data = json.dumps(event, indent=2)

    prompt = f"""あなたはAWS運用の専門家です。アラート内容の事実を正確に要約し事実のみを回答してください。
<input>
{finding_data}
</input>"""

    response = bedrock.converse(
        modelId=MODEL_ID,
        messages=[{"role": "user", "content": [{"text": prompt}]}],
    )

    summary = response["output"]["message"]["content"][0]["text"]

    sns.publish(
        TopicArn=SNS_TOPIC_ARN,
        Subject="Alert Notification",
        Message=summary,
    )

処理はシンプルで、EventBridgeから受け取ったJSONをBedrockのConverse APIに渡し、返ってきた要約をそのままSNSで通知しています。

環境変数

Lambdaに設定する環境変数は以下の2つです。

環境変数 説明
SNS_TOPIC_ARN 通知先SNSトピックのARN

動作例

セキュリティグループにICMP通信を許可する規則が追加された場合に、アラートを発報する監視設定を行っていた場合の導入前後の例です。

al02.png

al03.png

JSONを読み解く手間なく、「誰が何をしたか」「どう対応すべきか」が一目で分かります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?