AWS Config の全リージョンに展開されているレコーダーや配信チャンネルを AWS CLI から無効化する

2023.04.13

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

こんにちは!丸屋 正志(Maruya Masashi) です。

今日もブロックを掘ったり積み上げたり匠に壊されたりしていますか?

困っていたこと

AWS Control Tower の管理アカウントにて AWS Config の設定をしようとした所、既に AWS Config が有効化されており追加作成ができなかったため、全リージョンでレコーダーと配信チャンネルを無効化してみました。

下記ブログでも言及されておりますが、各リージョンでレコーダー(AWS::Config::ConfigurationRecorder)と配信チャンネル(AWS::Config::DeliveryChannel)は、1つまでしか作成できません。

このブログで実現できること

  • AWS Config の全リージョンのレコーダーと配信チャンネルの取得
  • AWS Config の全リージョンのレコーダーと配信チャンネルの無効化

各種コマンド

実施する手順としては、【リストするコマンド】→【無効化するコマンド】→【リストするコマンド】という順番に実施することで、証跡にも役立つかと思います。

リストするコマンド

以下のコマンドを AWS CloudShell に貼り付けて実行すると、全リージョンのレコーダーと配信チャンネル名を出力できます。

aws ec2 describe-regions --query "Regions[*].[RegionName]" --output text \
| while read region; do
  echo "##### list Config Recorders and Delivery Channels in ${region}"
  echo "{"
  recorders_list=$(aws --region ${region} configservice describe-configuration-recorders --query ConfigurationRecorders[*].[name] --output text)
  if [ ! -z "$recorders_list" ]; then
    echo "  Recorders Name : $recorders_list"
  else
    echo "  No Config Recorders found in ${region}"
  fi
  delivery_channels_list=$(aws --region ${region} configservice describe-delivery-channels --query DeliveryChannels[*].[name] --output text)
  if [ ! -z "$delivery_channels_list" ]; then
    echo "  Deliverys Name : $delivery_channels_list"
  else
    echo "  No Delivery Channels found in ${region}"
  fi
  echo "}"
done

無効化するコマンド

以下のコマンドをAWS CloudShell に貼り付けて実行すると、全リージョンのレコーダーと配信チャンネルを無効化することができます。

※ 注意 : 実行すると全リージョンからレコーダーと配信チャンネルが無効化(削除)されます

aws ec2 describe-regions --query "Regions[*].[RegionName]" --output text \
| while read region; do
  echo "##### Delete Config Recorders and Delivery Channels in ${region}"
  echo "{"
  recorder_names=$(aws --region ${region} configservice describe-configuration-recorders --query "ConfigurationRecorders[*].name" --output text)
  if [ ! -z "$recorder_names" ]; then
    echo "  Recorders Name :"
    for recorder in $recorder_names; do
      echo "    - $recorder"
      aws --region ${region} configservice delete-configuration-recorder --configuration-recorder-name $recorder
    done
  else
    echo "  No Config Recorders found in ${region}"
  fi
  delivery_channel_names=$(aws --region ${region} configservice describe-delivery-channels --query "DeliveryChannels[*].name" --output text)
  if [ ! -z "$delivery_channel_names" ]; then
    echo "  Deliverys Name :"
    for delivery_channel in $delivery_channel_names; do
      echo "    - $delivery_channel"
      aws --region ${region} configservice delete-delivery-channel --delivery-channel-name $delivery_channel
    done
  else
    echo "  No Delivery Channels found in ${region}"
  fi
  echo "}"
done

実行後の結果

リストするコマンドの結果

※ リージョンがバラバラですが抜粋して掲載しているためです

~~略~~
##### list Config Recorders and Delivery Channels in us-west-1
{
  No Config Recorders found in us-west-1
  No Delivery Channels found in us-west-1
}
##### List Config Recorders and Delivery Channels in ap-northeast-3
{
  Recorders Name :
    - aws-controltower-BaselineConfigRecorder
  Deliverys Name :
    - aws-controltower-BaselineConfigDeliveryChannel
}
~~略~~

無効化するコマンドの結果

※ リージョンがバラバラですが抜粋して掲載しているためです

~~略~~
##### Delete Config Recorders and Delivery Channels in us-west-1
{
  No Config Recorders found in us-west-1
  No Delivery Channels found in us-west-1
}
##### Delete Config Recorders and Delivery Channels in ap-northeast-3
{
  Recorders Name :
    - aws-controltower-BaselineConfigRecorder
  Deliverys Name :
    - aws-controltower-BaselineConfigDeliveryChannel
}
~~略~~

さいごに

今回は、AWS Configのレコーダーと配信チャンネルのリスト及び無効化のCLI紹介でした。

このブログが誰かの解決になれば幸いです!