AWSリソースのエビデンス取得用スクリプトを作成してみた: EventBridge編

1 min read

はじめに

AWSリソースのエビデンス取得用スクリプトを作成してみたの第三弾となります。 EventBridgeでは複数リージョンで有効化するケースが存在すると思います。 その際に、各リージョンでのエビデンスを取得するのは大変なので、以下のようなスクリプトを作成しました。

※本スクリプトはCloudShell上でのみ動作確認をしています。

スクリプト内容

#!/bin/bash

# 全リージョンのリストを取得
regions=$(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)

for region in $regions; do
    echo -e "\nリージョン: $region"
    
    # 各リージョンのフォルダを作成
    mkdir -p "$region"
    
    # 各リージョンのEventBridgeルールを取得
    rules=$(aws events list-rules --region $region --query 'Rules[]' --output json)
    
    if [ -z "$rules" ]; then
        echo "  EventBridgeルールが存在しません"
    else
        # ルールごとに詳細情報を取得
        echo "$rules" | jq -c '.[]' | while read -r rule; do
            name=$(echo $rule | jq -r '.Name')
            description=$(echo $rule | jq -r '.Description')
            event_bus=$(echo $rule | jq -r '.EventBusName')
            state=$(echo $rule | jq -r '.State')
            event_pattern=$(echo $rule | jq -r '.EventPattern')
            rule_arn=$(echo $rule | jq -r '.Arn')
            targets=$(aws events list-targets-by-rule --rule $name --region $region --query 'Targets[]' --output json)
            tags=$(aws events list-tags-for-resource --resource-arn $rule_arn --region $region --query 'Tags[]' --output json)
            
            echo "  ルール名: $name"
            echo "  説明: $description"
            echo "  イベントバス: $event_bus"
            echo "  ルールの状態: $state"
            echo "  イベントパターン: $event_pattern"
            
            if [ -z "$targets" ]; then
                echo "  ターゲットが存在しません"
            else
                echo "$targets" | jq -c '.[]' | while read -r target; do
                    target_id=$(echo $target | jq -r '.Id')
                    target_arn=$(echo $target | jq -r '.Arn')
                    input_path=$(echo $target | jq -r '.InputTransformer.InputPathsMap')
                    input_transformer=$(echo $target | jq -r '.InputTransformer.InputTemplate')
                    
                    echo "  ターゲットID: $target_id"
                    echo "  ターゲットARN: $target_arn"
                    echo "  入力パス: $input_path"
                    echo "  入力トランスフォーマー: $input_transformer"
                done
            fi
            
            if [ -z "$tags" ]; then
                echo "  タグが存在しません"
            else
                echo "  タグ:"
                echo "$tags" | jq -c '.[]' | while read -r tag; do
                    key=$(echo $tag | jq -r '.Key')
                    value=$(echo $tag | jq -r '.Value')
                    echo "    $key: $value"
                done
            fi

            echo -e "\n"
            
            # ファイル出力
            echo "$rule" > "$region/list-rules_$name.json"
            echo "$targets" > "$region/list-targets-by-rule_$name.json"
            echo "$tags" > "$region/list-tags-for-resource_$name.json"
        done
    fi
done

使用方法

以下のコマンドを実行し、スクリプトを実行してください。

sh get-eventbridge.sh

スクリプト実行ログをファイルとして保存する場合はリダイレクトを使用してください。

sh get-eventbridge.sh > get-eventbridge.log

出力結果

標準出力は以下のように表示されます。一目で設定値がわかるようになっています。

リージョン: ap-northeast-1
  ルール名: XXXXXXXX-XXXXXXXXXXXXX
  説明: XXXXXXXXXXXXXXXXXXXXX
  イベントバス: default
  ルールの状態: ENABLED
  イベントパターン: { "source": ["aws.cloudwatch"], "detail-type": ["CloudWatch Alarm State Change"], "resources": ["arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:XXXXXXXX-XXXXXXXXXXXXX"], "detail": { "state": { "value": ["ALARM"] } } }
  ターゲットID: XXXXXXXX-XXXXXXXXXXXXX
  ターゲットARN: arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:XXXXXXXX-XXXXXXXXXXXXX
  入力パス: {
  "Account": "$.account",
  "AlarmName": "$.detail.alarmName",
  "MetricsName": "$.detail.configuration.metrics[0].metricStat.metric.name",
  "Reason": "$.detail.state.reason",
  "Time": "$.time",
  "source": "$.source",
  "version": "$.version"
}
  入力トランスフォーマー: "以下アラームを発行しました。"

"AWS Account ID : <Account> "
"発生時間 : <Time>"
"アラーム名 : <AlarmName>"
"発生メトリクス : <MetricsName>"
"理由 : <Reason>"
  タグ:
    Name: XXXXXXXX-XXXXXXXXXXXXX

 ~~~省略~~~

以下のようにリージョン毎にディレクトリが作成されます。

[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$ ls -l
total 112
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ap-northeast-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ap-northeast-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ap-northeast-3
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ap-south-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ap-southeast-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ap-southeast-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 ca-central-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 eu-central-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 eu-north-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 eu-west-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 eu-west-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 eu-west-3
-rw-r--r--. 1 cloudshell-user cloudshell-user 13037 Jun  4 08:01 get-eventbridge.log
-rw-r--r--. 1 cloudshell-user cloudshell-user  3008 Jun  4 08:01 get-eventbridge.sh
-rw-r--r--. 1 cloudshell-user cloudshell-user 21999 Jun  4 08:02 get-eventbridge.zip
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 sa-east-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 us-east-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 us-east-2
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 us-west-1
drwxr-xr-x. 2 cloudshell-user cloudshell-user  4096 Jun  4 08:01 us-west-2
[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$ 

以下のようにリージョンディレクトリ内にJSONが保存されます。

[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$ ls -l ap-northeast-1/
total 60
-rw-r--r--. 1 cloudshell-user cloudshell-user 574 Jun  4 08:01 list-rules_XXXXXXXX-XXXXXXXXXXXXX.json
-rw-r--r--. 1 cloudshell-user cloudshell-user 228 Jun  4 08:01 list-tags-for-resource_XXXXXXXX-XXXXXXXXXXXXX.json
-rw-r--r--. 1 cloudshell-user cloudshell-user 850 Jun  4 08:01 list-targets-by-rule_XXXXXXXX-XXXXXXXXXXXXX.json
[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$