はじめに
AWSリソースのエビデンス取得用スクリプトを作成してみたの第二弾となります。 AWS Configでは複数リージョンで有効化するケースが存在すると思います。 その際に、各リージョンでのエビデンスを取得するのは大変なので、以下のようなスクリプトを作成しました。
※本スクリプトは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"
# 各リージョンの設定レコーダーの詳細を取得
config_recorders=$(aws configservice describe-configuration-recorders --region $region)
if [ -z "$config_recorders" ]; then
echo " 設定レコーダーが存在しません"
else
# 全リソースの記録設定を取得
record_all_resources=$(echo $config_recorders | jq -r '.ConfigurationRecorders[].recordingGroup.allSupported')
echo " 全リソースの記録設定: $record_all_resources"
# 設定レコーダーの記録頻度を取得
recording_frequency=$(echo $config_recorders | jq -r '.ConfigurationRecorders[].recordingMode.recordingFrequency')
echo " デフォルトの記録頻度: $recording_frequency"
# グローバルリソースの設定変更の記録設定の取得
override_status=$(echo $config_recorders | jq -r '.ConfigurationRecorders[].recordingGroup.includeGlobalResourceTypes')
echo " グローバルリソースの設定変更の記録: $override_status"
# 記録戦略の設定を取得
recording_strategy=$(echo $config_recorders | jq -r '.ConfigurationRecorders[].recordingGroup.recordingStrategy.useOnly')
echo " 記録戦略: $recording_strategy"
# 記録戦略が"EXCLUSION_BY_RESOURCE_TYPES"の場合
if [ "$recording_strategy" = "EXCLUSION_BY_RESOURCE_TYPES" ]; then
# 除外されたリソース一覧を取得
exclusion_resources=$(echo $config_recorders | jq -r '.ConfigurationRecorders[].recordingGroup.exclusionByResourceTypes.resourceTypes')
echo " 除外されたリソース一覧: $exclusion_resources"
fi
fi
# AWS Config データの保持期間を設定
retention_period=$(aws configservice describe-retention-configurations --region $region | jq -r '.RetentionConfigurations[].RetentionPeriodInDays')
if [ -z "$retention_period" ]; then
retention_period=2557 # デフォルトの7年間 (2557日)
fi
echo " AWS Config データを $retention_period 日間保持する"
# IAMロールのARNを取得
iam_role_arn=$(echo $config_recorders | jq -r '.ConfigurationRecorders[].roleARN')
echo " IAMロールのARN: $iam_role_arn"
# 各リージョンの配信チャネルの詳細を取得
delivery_channels=$(aws configservice describe-delivery-channels --region $region)
if [ -z "$delivery_channels" ]; then
echo " 配信チャネルが存在しません"
else
# S3バケット名を取得
s3_bucket_name=$(echo $delivery_channels | jq -r '.DeliveryChannels[].s3BucketName')
echo " S3バケット名: $s3_bucket_name"
# S3バケットのプレフィックスを取得
s3_key_prefix=$(echo $delivery_channels | jq -r '.DeliveryChannels[].s3KeyPrefix')
echo " S3バケットのプレフィックス: $s3_key_prefix"
# SNSトピックのARNを取得
sns_topic_arn=$(echo $delivery_channels | jq -r '.DeliveryChannels[].snsTopicARN')
echo " SNSトピックのARN: $sns_topic_arn"
fi
# テンプレートの詳細を取得
template_details=$(aws cloudformation describe-stacks --region $region)
if [ -z "$template_details" ]; then
echo " テンプレートが存在しません"
else
# 適合パック名を取得
compliance_pack_name=$(echo $template_details | jq -r '.Stacks[].StackName')
echo " 適合パック名: $compliance_pack_name"
# テンプレートのステータスを取得
template_status=$(echo $template_details | jq -r '.Stacks[].StackStatus')
echo " テンプレートのステータス: $template_status"
fi
# ファイル出力
echo "$config_recorders" > "$region/describe-configuration-recorders_$region.json"
echo "$delivery_channels" > "$region/describe-delivery-channels_$region.json"
echo "$template_details" > "$region/describe-stacks_$region.json"
done
使用方法
以下のコマンドを実行し、スクリプトを実行してください。
sh get-awsconfig.sh
スクリプト実行ログをファイルとして保存する場合はリダイレクトを使用してください。
sh get-awsconfig.sh > get-awsconfig.log
出力結果
標準出力は以下のように表示されます。一目で設定値がわかるようになっています。
リージョン: ap-south-1
全リソースの記録設定: false
デフォルトの記録頻度: DAILY
グローバルリソースの設定変更の記録: false
記録戦略: EXCLUSION_BY_RESOURCE_TYPES
除外されたリソース一覧: [
"AWS::IAM::Policy",
"AWS::IAM::User",
"AWS::IAM::Role",
"AWS::IAM::Group"
]
AWS Config データを 2557 日間保持する
IAMロールのARN: arn:aws:iam::XXXXXXXXXXXXX:role/aws-service-role/config.amazonaws.com/AWSServiceRoleForConfig
S3バケット名: XXXXXXXXXXXXX-XXXXXXXXXXXXX
S3バケットのプレフィックス: Logs/Config
SNSトピックのARN: null
適合パック名: XXXXXXXXXXXXXXXXXXXXXXXXXX
テンプレートのステータス: CREATE_COMPLETE
~~~省略~~~
以下のようにリージョン毎にディレクトリが作成されます。
[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-awsconfig.log
-rw-r--r--. 1 cloudshell-user cloudshell-user 3008 Jun 4 08:01 get-awsconfig.sh
-rw-r--r--. 1 cloudshell-user cloudshell-user 21999 Jun 4 08:02 get-awsconfig.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 describe-configuration-recorders_ap-northeast-1.json
-rw-r--r--. 1 cloudshell-user cloudshell-user 228 Jun 4 08:01 describe-delivery-channels_ap-northeast-1.json
-rw-r--r--. 1 cloudshell-user cloudshell-user 850 Jun 4 08:01 describe-stacks_ap-northeast-1.json
[cloudshell-user@ip-XX-XXX-XXX-XXX ~]$