AWS Configのすべてのリソース記録を全リージョンで有効化するシェルスクリプト書いた

AWS Configのすべてのリソース記録を全リージョンで有効化する機会があったのでシェルスクリプトを書きました。
2020.01.20

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

AWSを愛する皆さま、こんにちは。コンサルティング部の西野(@xiyegen)です。

AWS Configのすべてのリソース記録を全リージョンで有効化する機会があったのでインスパイア系シェルスクリプトを書きました。
※boto3を愛してやまない皆さまは下記のブログをご参照ください。
AWS Configのすべてのリソース記録を全リージョンで有効化するPythonスクリプト書いた

スクリプト

本スクリプトを使用するにあたっての前提条件は下記の通りです。

  • AWS Configを全リージョンで有効化済み
  • 各リージョンのAWS Configで利用しているIAM RoleおよびS3バケットは共通
  • IAMなどのグローバルリソースを記録するメインリージョンをap-northeast-1(東京リージョン)とする。

有効化

role_arn_for_aws_config=<IAM Role ARN for AWS CONFIG>
s3_bucket_name=<S3 Bucket Name>
main_region=ap-northeast-1

REGIONS=( \
ap-northeast-1 \
ap-northeast-2 \
ap-south-1 \
ap-southeast-1 \
ap-southeast-2 \
ca-central-1 \
eu-central-1 \
eu-north-1 \
eu-west-1 \
eu-west-2 \
eu-west-3 \
sa-east-1 \
us-east-1 \
us-east-2 \
us-west-1 \
us-west-2 \
)

# サポートされているすべてのサービスにつき記録を有効化(allSupported=true)
# IAMなどのグローバルリソースはメインのリージョンにおいてのみ有効化(includeGlobalResourceType=true)
for region in ${REGIONS[@]}; do
    echo "Processing ${region} configuration for recorder..."
    if [ ${region} = ${main_region} ]; then
        aws configservice put-configuration-recorder --region ${region} --configuration-recorder name=default,roleARN=${role_arn_for_aws_config} --recording-group allSupported=true,includeGlobalResourceTypes=true
    else
        aws configservice put-configuration-recorder --region ${region} --configuration-recorder name=default,roleARN=${role_arn_for_aws_config} --recording-group allSupported=true,includeGlobalResourceTypes=false
    fi
done
 
# 記録先バケットの設定
for region in ${REGIONS[@]}; do
    echo "Processing ${region} configuration for delivery channels..."
    aws configservice put-delivery-channel --region ${region} --delivery-channel name=default,s3BucketName=${s3_bucket_name}
done

設定確認

REGIONS=( \
ap-northeast-1 \
ap-northeast-2 \
ap-south-1 \
ap-southeast-1 \
ap-southeast-2 \
ca-central-1 \
eu-central-1 \
eu-north-1 \
eu-west-1 \
eu-west-2 \
eu-west-3 \
sa-east-1 \
us-east-1 \
us-east-2 \
us-west-1 \
us-west-2 \
)

# 記録有効化のチェック
echo 'region allSupported includeGlobalResourceTypes'
for region in ${REGIONS[@]}; do
    echo ${region} $(aws configservice describe-configuration-recorders --region ${region} --query 'ConfigurationRecorders[*].recordingGroup.[allSupported,includeGlobalResourceTypes]' --output text)
done

# バケット設定のチェック
echo -e 'region s3BucketName'
for region in ${REGIONS[@]}; do
    echo ${region} $(aws configservice describe-delivery-channels --region ${region} --query 'DeliveryChannels[*].s3BucketName' --output text)
done

おまけ

アカウント内で有効な全リージョンの一覧は下記のコマンドで入手できます。

$ aws ec2 describe-regions --query 'Regions[].[RegionName]' --output text | sort
ap-northeast-1
ap-northeast-2
ap-south-1
ap-southeast-1
ap-southeast-2
ca-central-1
eu-central-1
eu-north-1
eu-west-1
eu-west-2
eu-west-3
sa-east-1
us-east-1
us-east-2
us-west-1
us-west-2

終わりに

このブログがほんの少しでも世界を良くできれば嬉しいです。
埋伏智歯を抜歯して晴れ晴れとした気分になっているコンサルティング部の西野(@xiyegen)がお送りしました。