1
0

More than 1 year has passed since last update.

AWS CLIからAWS Configルールの一括削除する

Last updated at Posted at 2021-10-25

背景

画面からだと1つずつしか削除できずめんどくさかったから

AWS OrganizationのマスターアカウントからAWS Configを適応されている場合は「OrgConfigRule-ABCDEFG」みたいなルールがあったり、SecurityHubが作成したルール(「securityhub-access-keys-rotated-xxxxx」みたいな名前)があるがそれらは削除できずエラーになるので特に問題はない(本当は分岐したりするべきだけど時間がなかったので省略)

各種バージョン

$ aws --version
aws-cli/2.1.30 Python/3.8.8 Darwin/20.6.0 exe/x86_64 prompt/off

スクリプト

#!/bin/bash

PROFILE="profile-1234"

RULES=$(aws configservice describe-config-rules \
    --query 'ConfigRules[].ConfigRuleName' \
    --profile ${PROFILE} --output text)

echo ""
for RULE in ${RULES}
do

    # 削除できなければExceptionに入るのでエラーは捨てる
    POLICY=$(aws configservice delete-config-rule \
        --config-rule-name ${RULE} \
        --profile ${PROFILE} 2> /dev/null)
    if [ $? -gt 0 ]; then
        echo "cannot be deleted ${RULE}"
    else
        echo "remove complete ${RULE}"
    fi
done

出力結果

cannot be deleted aws-config-rule01
remove complete aws-config-rule02
remove complete aws-config-rule03
:
:
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0