(小ネタ)AWS Config が全リージョンで有効か無効か確認するスクリプトを CloudShell で実行する

(小ネタ)AWS Config が全リージョンで有効か無効か確認するスクリプトを CloudShell で実行する

AWS Config が全リージョンで有効か無効かを確認するスクリプトを CloudShell で実行してみました。
Clock Icon2025.03.22

コーヒーが好きな emi です。

スタンドアロンの AWS アカウントを Control Tower に参加させたいシーンがあったのですが、AWS Config が有効になっていると登録に失敗するらしく、確認する必要がありました。
AWS Config はリージョンごとに有効化する必要があり、すべてのリージョンの Config コンソールを開いて確認するのは手間です。そこで、生成 AI の力も借りて Config が有効になっているかどうか確認するスクリプトを作成し CloudShell で実行してみました。
過去何回も擦られたネタかもしれませんが、読むのと自分でやるのとでは理解に差があると思うので改めて記事に残しておきます。

作成したスクリプト

check-config-status.sh
#!/bin/bash

# Get all available AWS regions
regionAry=($(aws ec2 describe-regions --all-regions --query "Regions[].RegionName" --output text))

# Print table header
printf "| %-18s | %-24s | %-10s | %-30s |\n" "Region" "Recorder Name" "Recording" "Last Status Change Time"
printf "|--------------------|--------------------------|------------|-------------------------------|\n"

# Loop through each region
for region in "${regionAry[@]}"; do
    # Get Config recorder status for the region
    result=$(aws configservice describe-configuration-recorder-status \
        --region "$region" \
        --query "ConfigurationRecordersStatus[0].[name,recording,lastStatusChangeTime]" \
        --output text 2>/dev/null)

    if [ -z "$result" ]; then
        # If there's no result, Config is not set up in this region
        printf "| %-18s | %-24s | %-10s | %-30s |\n" "$region" "-" "-" "-"
    else
        # Parse the values using awk
        recorderName=$(echo "$result" | awk '{print $1}')
        recording=$(echo "$result" | awk '{print $2}')
        lastStatusTime=$(echo "$result" | awk '{print $3, $4}')

        # Print the result in table format
        printf "| %-18s | %-24s | %-10s | %-30s |\n" "$region" "$recorderName" "$recording" "$lastStatusTime"
    fi
done

スクリプトの説明

AWS Configのレコーダーステータスを全リージョンに渡って確認し、表形式で出力するものです。

1. 利用可能な AWS リージョン一覧の取得

regionAry=($(aws ec2 describe-regions --all-regions --query "Regions[].RegionName" --output text))
  • aws ec2 describe-regions で全リージョンのリージョン名を取得
  • regionAry はリージョン名の配列
  • --all-regions を指定すると全リージョンを取得できる

2. 表形式のヘッダー出力

printf "| %-18s | %-24s | %-10s | %-30s |\n" "Region" "Recorder Name" "Recording" "Last Status Change Time"
printf "|--------------------|--------------------------|------------|-------------------------------|\n"
  • 出力結果をテーブル形式にするために、ヘッダー(見出し)と罫線を表示
    • --output table だとリージョンの数だけテーブルが出力されるようになってしまったためこのように人力で成形
  • %-NsN 文字幅で左寄せする printf のフォーマット

3. 各リージョンに対してループ処理

for region in "${regionAry[@]}"; do
  • 全リージョンに対して、1 つずつ処理をループ
  • ${regionAry[@]} は配列の中身を1つずつ取り出す

4. 指定リージョンで Config のステータスを取得

result=$(aws configservice describe-configuration-recorder-status \
    --region "$region" \
    --query "ConfigurationRecordersStatus[0].[name,recording,lastStatusChangeTime]" \
    --output text 2>/dev/null)
  • aws configservice describe-configuration-recorder-status コマンドで Configレコーダーのステータスを取得
  • --query で必要な項目(名前、録画状態、最終変更時刻)のみに絞る
  • --output text で 1 行のテキスト形式として出力
  • 2>/dev/null でエラー出力を無視する

5. 結果が空の場合(Config 未設定)

if [ -z "$result" ]; then
    printf "| %-18s | %-24s | %-10s | %-30s |\n" "$region" "-" "-" "-"
  • result が空の場合、そのリージョンでは Config が設定されていない
  • 「-」で表記して空欄であることを示す

6. 結果がある場合(Config 設定済)

else
    recorderName=$(echo "$result" | awk '{print $1}')
    recording=$(echo "$result" | awk '{print $2}')
    lastStatusTime=$(echo "$result" | awk '{print $3, $4}')
  • 出力された 1 行のテキストを awk で分割して、それぞれの値を取り出す
    • $1: レコーダー名(例:default
    • $2: 録画中かどうか(true/false
    • $3 $4: 最終ステータス変更時刻(UTC)
      • 「最終ステータス送信時間(lastStatusChangeTime)」は Config レコーダーの状態が最後に変化した時刻のこと
        • たとえば recording が true → false になった、start-configuration-recorder を実行したなど

7. 表形式で結果を出力

    printf "| %-18s | %-24s | %-10s | %-30s |\n" "$region" "$recorderName" "$recording" "$lastStatusTime"
  • 整形された結果を表形式で出力

CloudShell で実行

CloudShell で check-config-status.sh を保存します。

  • vim check-config-status.sh と入力し vim エディタを開く
  • :set paste + Enter で貼り付けモードにする
  • i を押下し編集モードにする
  • スクリプトの中身を貼り付ける
  • esc を押下し編集モードを抜ける
  • :wq + Enter で保存し閉じる
  • 保存できたか cat check-config-status.sh で確認する

では実行してみます。

./check-config-status.sh

▼実行結果

~ $ ./check-config-status.sh
bash: ./check-config-status.sh: Permission denied
~ $ 

権限がありませんでした。権限を確認します。

ls -la | grep check-config-status.sh

▼実行結果

~ $ ls -la | grep check-config-status.sh
-rw-r--r--. 1 cloudshell-user cloudshell-user 1068 Mar 22 07:47 check-config-status.sh
~ $ 

実行権限がありませんね。権限を付与します。

chmod +x check-config-status.sh

▼実行結果

~ $ chmod +x check-config-status.sh
~ $ 

再度権限を確認すると、実行権限が付与できています。

~ $ ls -la | grep check-config-status.sh
-rwxr-xr-x. 1 cloudshell-user cloudshell-user 1068 Mar 22 07:47 check-config-status.sh
~ $ 

再度実行してみます。

▼実行結果

~ $ ./check-config-status.sh 
| Region             | Recorder Name            | Recording  | Last Status Change Time        |
|--------------------|--------------------------|------------|-------------------------------|
| ap-south-2         | -                        | -          | -                              |
| ap-south-1         | None                     |            |                                |
| eu-south-1         | -                        | -          | -                              |
| eu-south-2         | -                        | -          | -                              |
| me-central-1       | -                        | -          | -                              |
| il-central-1       | -                        | -          | -                              |
| ca-central-1       | None                     |            |                                |
| mx-central-1       | -                        | -          | -                              |
| eu-central-1       | None                     |            |                                |
| eu-central-2       | -                        | -          | -                              |
| us-west-1          | None                     |            |                                |
| us-west-2          | None                     |            |                                |
| af-south-1         | -                        | -          | -                              |
| eu-north-1         | None                     |            |                                |
| eu-west-3          | None                     |            |                                |
| eu-west-2          | None                     |            |                                |
| eu-west-1          | None                     |            |                                |
| ap-northeast-3     | None                     |            |                                |
| ap-northeast-2     | None                     |            |                                |
| me-south-1         | -                        | -          | -                              |
| ap-northeast-1     | None                     |            |                                |
| sa-east-1          | None                     |            |                                |
| ap-east-1          | -                        | -          | -                              |
| ca-west-1          | -                        | -          | -                              |
| ap-southeast-1     | None                     |            |                                |
| ap-southeast-2     | None                     |            |                                |
| ap-southeast-3     | -                        | -          | -                              |
| ap-southeast-4     | -                        | -          | -                              |
| us-east-1          | None                     |            |                                |
| ap-southeast-5     | -                        | -          | -                              |
| us-east-2          | None                     |            |                                |
| ap-southeast-7     | -                        | -          | -                              |
~ $ 

いい感じですね。

Recorder NameNone のリージョンは、Config Recorder が未設定です。
Recorder Name- のリージョンは、リージョン自体が無効(オプトインされていない、not-opted-in)です。

これで、Config は有効化されていないことが確認できました。

おわりに

本記事への質問やご要望については画面下部のお問い合わせ「DevelopersIO について」からご連絡ください。記事に関してお問い合わせいただけます。

参考

https://dev.classmethod.jp/articles/tsnote-awsconfig-awscli-describe-configuration-recorder-status/

https://dev.classmethod.jp/articles/registering-an-existing-account-in-control-tower/

https://aws.amazon.com/jp/blogs/mt/aws-config-best-practices/

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.