はじめに
前回、EC2を終了とスナップショットを削除しましたが、ボリュームの消し忘れで課金され続けたため、
有識者にご教示いただき「aws-nuke」ツールでリソースを一括で削除をCloudShellで試してみました。
以下のサイトが大変参考になりました。
実際にツールを試したところ、躓くところがあったため記事にまとめさせていただきました。
参考にしたサイト
https://qiita.com/rapirapi/items/664b61958aabef51df17
実行手順
CloudShellを開く
AWSにログインして右上にあるCloudShellをクリックしてください
※私は東京リージョンでCloudShellを開きました。
aws-nukeツールをダウンロードする
以下のサイトをクリックして開き、最新のバージョンをインストールした。
https://github.com/rebuy-de/aws-nuke/releases
ダウンロードするaws-nukeツールを右クリックしてリンクのアドレスをコピーを選択した。
wgetを実行して上記のURLからaws-nukeをダウンロードしました。cloudshellで以下のコマンドを実行する
$ wget https://github.com/rebuy-de/aws-nuke/releases/download/v2.22.0/aws-nuke-v2.22.0-linux-amd64.tar.gz
補足情報
上記のスクリプトはarm64、amd64があるため、何を選択して良いか迷うと思います。
cloudshellに入ってCPUを確認しました。
x86_64であることを確認しました。
[cloudshell-user@ip-10-2-12-176 ~]$ uname -m
x86_64
[cloudshell-user@ip-10-2-12-176 ~]$
x86_64とad64は事実上同じという記事を見つけたため、amd64をインストールしました。
https://onoredekaiketsu.com/x86-64-x64-amd64-what-is-the-difference-between-these/
誤ったOSのスクリプトを実行した際は以下のエラーが出ます。
darwinはmacのOSのため、cloudshellでは実行するとエラーになります。
例です。代表で一部の処理を記載しています。
[cloudshell-user@ip-10-2-12-176 ~]$ ./aws-nuke-v2.22.0-darwin-amd64 -c ./nuke-config.yml
-bash: ./aws-nuke-v2.22.0-darwin-amd64: cannot execute binary file
[cloudshell-user@ip-10-2-12-176 ~]$
[cloudshell-user@ip-10-2-12-176 ~]$
aws-nukeツールのtarファイルを解凍する
aws-nukeのconfigファイル作成する
・削除対象のリージョンは今回は東京リージョンとグローバルを指定しました。
・account-blocklistは削除対象外のアカウント番号を指定します。
削除対象外のアカウントがないため、適当なアカウント番号を入れました。
・resource-typesのexcludesは削除対象外のサービスを指定します。
SESとFMSはaws-nukeを実行した際に処理中にエラー文が出るため、削除対象外として設定しました。
削除対象外のサービスについては以下のサイトの情報をもとに設定させていただきました。
https://dev.classmethod.jp/articles/aws-nuke-code-series-resource-delete/
新規にaws-nukeスクリプト実行時に指定するconfigファイルを作成する。
vi nuke-config.yml
configファイルの中身を以下です。
regions: ※削除対象のリージョン
- ap-northeast-1
- global
account-blocklist:
- "999999999999" # production ※削除対象外のアカウント番号を指定する
resource-types: ※削除対象外のサービスを指定する
excludes:
- FMSPolicy
- FMSNotificationChannel
- GlobalAccelerator
- GlobalAcceleratorListener
- GlobalAcceleratorEndpointGroup
- WorkLinkFleet
- MobileProject
- SESReceiptRuleSet
- SESReceiptFilter
- IAMUser
- IAMRole
- IAMVirtualMFADevice
- IAMUserPolicyAttachment
- IAMLoginProfile
- SESIdentity
- Route53HostedZone
- Route53ResourceRecordSet
- EC2KeyPair
- EC2VPC
- EC2NetworkACL
- EC2Subnet
- EC2DHCPOption
- EC2SecurityGroup
- EC2DefaultSecurityGroupRule
- EC2InternetGateway
- EC2RouteTable
- Route53ResolverRule
accounts:
"削除対象のアカウント番号":
補足情報
pythonの様に先頭に空白行がある場合はエラーになります。
以下はaccounts:の前に空白行が入っているため、11行でエラーが出力されました。
region:resource-types: accounts:の先頭には空白は不要です。
※viで開き、:set numberと入れると行番号が先頭に表示されます。
[cloudshell-user@ip-10-2-12-176 ~]$ ./aws-nuke-v2.22.0-linux-amd64 -c ./nuke-config.yml
ERRO[0000] Failed to parse config file ./nuke-config.yml
Error: yaml: unmarshal errors:
line 11: field accounts not found in type config.ResourceTypes
アカウントエイリアスを作成する
アカウントエイリアスは任意で且つユニークである必要があります。
既に誰かが使用しているエイリアス名は使用出来ません。
アカウントエイリアスはaws-nukeを実行した際に入力を求められるため、
忘れない様に管理してください。
aws iam create-account-alias --account-alias <アカウントのエイリアス名>
dry-runを実行する
以下のコマンドを実行してdry-runして何のサービスが削除されるか確認しました。
スクリプトを実行したら、「アカウントエイリアス名」を入力してください
$ ./aws-nuke-v2.22.0-linux-amd64 -c ./nuke-config.yml
aws-nuke version v2.22.0 - Thu Mar 23 12:45:51 UTC 2023 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Do you really want to nuke the account with the ID <アカウント番号> and the alias 'アカウントエイリアス名'?
Do you want to continue? Enter account alias to continue.
> ※アカウントエイリアス名を入力する
実行したら、削除されるリソースが表示されます。
Default VPC等は削除されないため、「Cannot delete default 」を表示されます。
削除される予定のリソースは「would remove」と表示されます。
デフォルトの Elasticacheのパラメータグループのため、削除されません。
ap-northeast-1 - ElasticacheCacheParameterGroup - default.redis7.cluster.on - [GroupFamily: "redis7", GroupName: "default.redis7.cluster.on"] - Cannot delete default cache parameter group
※削除されるリソース 例 Elasticacheのパラメータグループです。
ap-northeast-1 - ElasticacheCacheParameterGroup - test - [GroupFamily: "redis2.8", GroupName: "test"] - would remove
no-dry-runを実行する
dry-runで削除対象のリソースを確認して、問題なければno-dry-runを指定して実行しました。
no-dry-runを実行するとリソースが削除されるため、実行する際は削除されるリソースをdry-runで把握してから実行した方が良いです。
$ ./aws-nuke-v2.22.0-linux-amd64 -c ./nuke-config.yml --no-dry-run
aws-nuke version v2.22.0 - Thu Mar 23 12:45:51 UTC 2023 - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Do you really want to nuke the account with the ID <アカウント番号> and the alias 'アカウントエイリアス名'?
Do you want to continue? Enter account alias to continue.
>※アカウントエイリアス名を入力する
実行後、再度削除して良いか確認が画面が表示されます。
入力に「アカウントエイリアス名」以外を入れると「Error aborted」と表示されて処理が中断されます。
Do you really want to nuke these resources on the account with the ID <アカウント番号> and the alias 「xxxxxxxxxxxxx」?
Do you want to continue? Enter account alias to continue.
> アカウントエイリアス名を入力する
翻訳
ID <アカウント番号> とエイリアス「xxxxxxxxxxxxx」のアカウントでこれらのリソースを本当に削除しますか?
続けたいですか? アカウント エイリアスを入力して続行します。
実行が完了後、以下のメッセージが出ます。
例)
Removal requested: 0 waiting, 0 failed, 79 skipped, 72 finished
Nuke complete: 0 failed, 79 skipped, 72 finished.
まとめ
aws-nukeツールを使ってみましたが、参考になる記事はたくさんありましたが、
つまずくポイントも結構ありました。
AWSを個人アカウントでいろいろハンズオンする方は、リソースの消し忘れで気づかないうちに課金される場合があるため、aws-nukeツールで一括削除することをお勧めします。
この記事が参考になれば嬉しいです。