🚽
aws-nuke で AWS リソースを一掃する
AWS アカウントのリソースを一掃する際には aws-nuke が便利に使えます。
私は個人用サンドボックスアカウントが気が付くと月々数百円程度の課金が発生しているため、ゴミ掃除の方法として利用しました。
業務用のアカウントに対して利用する場合には注意して利用してください。
Install
Mac の場合は homebrew で入ります。
$ brew install aws-nuke
Prepare
実行前の準備事項です。
nuke config
aws-nuke を実行する際に指定する設定ファイルを作成します。
私は、~/.config/nuke-config.yml
に配置しています。
sample;nuke-config.yml
regions:
- global
- us-east-1
- us-east-2
- us-west-1
- us-west-2
- us-gov-west-1
- ca-central-1
- eu-central-1
- eu-north-1
- eu-west-1
- eu-west-2
- eu-south-1
- eu-west-3
- ap-northeast-1
- ap-northeast-2
- ap-northeast-3
- ap-southeast-1
- ap-southeast-2
- ap-south-1
- ap-east-1
- af-south-1
- cn-north-1
- cn-northwest-1
- me-south-1
- sa-east-1
account-blocklist:
-
accounts:
[AWS_ACCOUNT_ID]:
filters:
IAMUser:
- "admin"
IAMUserPolicyAttachment:
- property: UserName
value: "admin"
IAMUserAccessKey:
- property: UserName
value: "admin"
- [AWS_ACCOUNT_ID] は実行対象の AWS アカウント ID に置き換えます。
- アカウントのフィルタには、aws-nuke を実行する IAM ユーザ(例では"admin")とそのアタッチポリシー、アクセスキーを削除しないためのフィルターを記載しています。
Account Alias
事前に実行対象の AWS アカウントへアカウントエイリアスを設定する必要があります。
account alias
❯ aws iam create-account-alias --account-alias hogehoge
❯ aws iam list-account-aliases
{
"AccountAliases": [
"hogehoge"
]
}
Execution
Dry-Run
まずは、dry-runを行い、削除対象リソースを確認します。
force オプションを指定しない場合は、実行後に入力待ちとなるためエイリアスを入力します。
dry run
❯ aws-nuke --config ~/.config/nuke-config.yml --quiet --force --force-sleep 3
aws-nuke version 2.20.0 - 2022-10-11 - 8062fc7d7a4b62772f6d0835aa6b0e46322fa288
Do you really want to nuke the account with the ID 000000000000 and the alias 'hogehoge'?
Waiting 3s before continuing.
ap-northeast-1 - S3Bucket - s3://test2-20221110 - [CreationDate: "2022-11-10 13:33:24 +0000 UTC", Name: "test2-20221110"] - would remove
Scan complete: 147 total, 1 nukeable, 146 filtered.
The above resources would be deleted with the supplied configuration. Provide --no-dry-run to actually destroy resources.
削除対象のリソースに対しては would remove
というメッセージで出力され、最後の Scan complete に実行計画が表示されます。 1 nukeable
が削除対象リソースの数を示します。
Run
実際に削除を行う場合は、 --no-dry-run
オプションを指定して実行します。
run
❯ aws-nuke --config ~/.config/nuke-config.yml --quiet --force --force-sleep 3 --no-dry-run
aws-nuke version 2.20.0 - 2022-10-11 - 8062fc7d7a4b62772f6d0835aa6b0e46322fa288
Do you really want to nuke the account with the ID 000000000000 and the alias 'hogehoge'?
Waiting 3s before continuing.
ap-northeast-1 - S3Bucket - s3://test2-20221110 - [CreationDate: "2022-11-10 13:33:24 +0000 UTC", Name: "test2-20221110"] - would remove
Scan complete: 147 total, 1 nukeable, 146 filtered.
Do you really want to nuke these resources on the account with the ID 000000000000 and the alias 'hogehoge'?
Waiting 3s before continuing.
ap-northeast-1 - S3Bucket - s3://test2-20221110 - [CreationDate: "2022-11-10 13:33:24 +0000 UTC", Name: "test2-20221110"] - triggered remove
Removal requested: 1 waiting, 0 failed, 146 skipped, 0 finished
ap-northeast-1 - S3Bucket - s3://test2-20221110 - [CreationDate: "2022-11-10 13:33:24 +0000 UTC", Name: "test2-20221110"] - waiting
Removal requested: 1 waiting, 0 failed, 146 skipped, 0 finished
ap-northeast-1 - S3Bucket - s3://test2-20221110 - [CreationDate: "2022-11-10 13:33:24 +0000 UTC", Name: "test2-20221110"] - removed
Removal requested: 0 waiting, 0 failed, 146 skipped, 1 finished
Nuke complete: 0 failed, 146 skipped, 1 finished.
これで作成したリソースがまとめて削除できます。
Memo
- フィルタなどを指定しない場合、アカウントのデフォルト VPC や security group などのリソースも削除されるため、アカウント作成時点の状態とは少々異なります。
- AWS アカウントで最初にやるべきことにあるような設定も削除されますので、必要に応じて設定するようにしましょう。
Discussion