2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CloudWatchダッシュボードを作成してみよう!!

Posted at

作成するもの

CloudWatchのダッシュボードでカスタムウィジットという機能があります。Lambda関数を作成後、CloudWatchダッシュボードにLambda関数を登録します。CloudWatchダッシュボードを参照するとLambda関数が実行されて実行結果がダッシュボードに表示される仕組みです。今回は下記の様にSecurity Hubのレコードの件数を表示する機能を作成してみます。機能はSecurity HubのワークフローステータスがNEWでレコード状態がACTIVEなものをフィルタリング後、重要度で集計してレコード件数を表形式に表示するものになります。

cw01.png

作成手順

Lambdaコンソールに移動後、関数の作成をクリックします。下記項目を入力後、関数の作成をクリックします。

関数名:適当な関数名
ランタイム:Python3.12

cw02.png

Lambda関数が作成されます。関数の設定を3か所変更します。最初はタイムアウト値です。一般設定からタイムアウト値を選択して、デフォルトの3秒から1分に変更します。

cw03.png

次はIAMロールへの権限追加を行います。アクセス権限のタブからロール名をクリックします。

cw04.png

IAMロールの頁に移動します。許可を追加⇒ポリシーをアタッチを選択後、AWSSecurityHubFullAccessにチェックして許可を追加をクリックします。

cw05.png

最後にLambdaのコードを記述します。コードタブに移動後、下記コードをコピペしてDeployをクリックします。これでLambda関数の用意は完了です。

cw06.png

import boto3
from collections import Counter, OrderedDict

securityhub = boto3.client('securityhub')

def lambda_handler(event, context):
    securityhub_findings = []
    next_token = None

    while True:
        if next_token:
            response = securityhub.get_findings(NextToken=next_token)
        else:
            response = securityhub.get_findings()

        filtered_findings = [
            finding for finding in response.get('Findings', [])
            if finding.get("WorkflowState") == "NEW" and finding.get("RecordState") == "ACTIVE"
        ]

        securityhub_findings.extend(filtered_findings)
        next_token = response.get('NextToken')

        if not next_token:
            break

    severity_counts = Counter(
        finding.get("Severity", {}).get("Label", "UNKNOWN") for finding in securityhub_findings
    )

    severity_order = ["CRITICAL", "HIGH", "MEDIUM", "LOW", "INFORMATIONAL"]
    sorted_counts = OrderedDict((key, severity_counts.get(key, 0)) for key in severity_order)

    severity_colors = {
        "CRITICAL": "#FFB3B3",
        "HIGH": "#FFFFB3",
        "MEDIUM": "#B3FFB3",
        "LOW": "#D3D3D3",
        "INFORMATIONAL": "#E6E6FA"
    }

    table_html = """
    <table border="1" style="border-collapse: collapse; text-align: center;">
        <tr>
            <th>Severity</th>
            <th>Count</th>
        </tr>
    """

    for label, count in sorted_counts.items():
        color = severity_colors.get(label, "#FFFFFF") 
        table_html += f"""
        <tr style="background-color: {color};">
            <td>{label}</td>
            <td>{count}</td>
        </tr>
        """

    table_html += "</table>"

    return table_html

ここからCloudWatchのダッシュボードを作成していきます。CloudWatchコンソールに移動後、ダッシュボード⇒ダッシュボードの作成の順にクリックします。適当なダッシュボード名を入力後、ダッシュボードの作成をクリックします。

cw07.png

カスタムウィジットを選択します。

cw08.png

何も選択せず次へをクリックします。

cw09.png

Lambda関数のタブで先ほど作成したLambda関数を選択します。

cw10.png

常に許可をクリックします。

cw11.png

ダッシュボードが表示されました。

cw01.png

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?