一時的に全リージョンのAWS Configを停止したい

2023.11.22

こんにちはカスタマーソリューション部のこーへいです!

AWS Config(以下Config)とは、AWSアカウントにあるAWSリソースを記録するサービスで、AWSリソースを記録(評価)する回数が多ければ多いほど、料金が上がるサービスです。
参考:AWS Config の料金

そこで料金が跳ね上がる作業をする場合、事前にConfigの記録を全リージョン一時的に停止するコマンドが欲しかったので今回作成しました。

グローバルリソースの記録を全リージョンに有効化していて、大量のIAMリソースを作って料金が高くなる経験のある方は多いはず。※グローバルリソースは東京リージョンのみ有効化するなどにしましょう。

作成コマンド

以下のコマンドを実行すると、全リージョンのConfigの記録を停止できます。

for region in $(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text); do
    for recorder in $(aws configservice describe-configuration-recorders --query 'ConfigurationRecorders[].name' --output text --region $region); do
        aws configservice stop-configuration-recorder --configuration-recorder-name $recorder --region $region
        echo "Stopped configuration recorder $recorder in region $region"
    done
done

実行するとコンソール画面から記録が停止されているのを確認できます。

該当作業終了後、以下のコマンドを実行すると、全リージョンのConfigの記録を開始できます。

for region in $(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text); do
    for recorder in $(aws configservice describe-configuration-recorders --query 'ConfigurationRecorders[].name' --output text --region $region); do
        aws configservice start-configuration-recorder --configuration-recorder-name $recorder --region $region
        echo "Started configuration recorder $recorder in region $region"
    done
done

まとめ

事前に発生する料金が高く、一時的にConfigを停止したい方向けの記事として執筆しました。

Configは基本的には常に有効化すべきなので、作業後は忘れず再起動しましょう。

参考