aws-nuke を AWS CloudShell で実行して AWS リソースを削除

記事タイトルとURLをコピーする

こんにちは、エデュケーショナルサービス課 AWS トレーナーの小倉です。

AWS の検証アカウントの掃除や AWS アカウントを別の用途で使い回すときなど、AWS アカウントの大量のリソースを削除するのは結構面倒です。
そこで使うのが aws-nuke です。aws-nuke とは、オープンソースの AWS リソースの削除ツールです。
※ aws-nuke は強力なツールのため、以下のドキュメントをよく読んで、理解してから使いましょう。私は必要な設定を誤って消してしまい困りました。

github.com

aws-nuke.ekristen.dev

今回は aws-nuke を AWS CloudShell で実行する手順を紹介します。
aws-nuke は、外部からでも実行することは可能ですが、AWS リソースを操作するための認証情報が必要です。CloudShell で実行するメリットとして、認証情報を外部にださなくてよいというのがあります。

事前準備

CloudShell で aws-nuke を実行できるように準備をします。
AWS アカウントには、AdministratorAccess のポリシーを付与したユーザーでログインしています。

1.アカウントエイリアスの設定

そのまま aws-nuke を実行すると、実行中にアカウントエイリアスの入力が求められますので、事前に設定しておきます。

IAMのコンソールを開きます。ダッシュボード画面の右に アカウントエイリアス がありますので、作成 をクリックします。

エイリアスの作成画面が表示されるので、 優先エイリアス を入力して エイリアスを作成 をクリックします。

2.aws-nuke のコンフィグファイルの作成

コンフィグファイルを読み込ませることで、削除対象のリージョン、AWS サービスなどを指定することができます。 コンフィグファイルの記載例はこちらのサイトにあります。

aws-nuke.ekristen.dev

私のコンフィグファイルの書き方を紹介します。
あくまで参考なので、ご自身の環境で実行するときは環境に合わせて内容の修正をお願いします。書き方の特徴としては、対象の AWS サービスを excludes で削除しないものを指定するようにしています(削除対象を全部書くのが大変です)。

ファイル名:nuke-config.yml

regions:
- ap-northeast-1
- ap-northeast-3
- us-east-1
- global
account-blocklist:
- "999999999999"
resource-types:
  excludes:
    - IAMUser
    - IAMRole
    - IAMUserPolicyAttachment
    - IAMUserAccessKey
    - IAMLoginProfile
    - IAMGroup
    - IAMRolePolicy
    - IAMRolePolicyAttachment
    - IAMPolicy
    - IAMSAMLProvider
    - CloudFormationStack
accounts:
  "<削除対象のAWSアカウントID>":
    filters:
      IAMUser:
      - "Adminstrator"

こちらのファイルを作業端末で作成してから、CloudShell にアップロードします。
CloudShell の開き方はいくつかありますが、他のAWSサービスと同様に画面上で検索して開くと全画面で操作できるのでおすすめです。

CloudShell の右上にある アクション - ファイルのアップロード をクリックし、作成したコンフィグファイルをアップロードします。

3.aws-nuke のインストール

インストール手順はこちらのサイトにのっていますが、今回は aws-nuke を管理している方が作成した distillery というツールを使用します。

aws-nuke.ekristen.dev

CloudShell で以下のコマンドを実行します。

# distillery のインストール
~ $ curl --proto '=https' --tlsv1.2 -LsSf https://get.dist.sh | sh
 ____  _     _   _ _ _
|  _ \(_)___| |_(_) | | ___ _ __ _   _
| | | | / __| __| | | |/ _ \ '__| | | |
| |_| | \__ \ |_| | | |  __/ |  | |_| |
|____/|_|___/\__|_|_|_|\___|_|   \__, |
                                 |___/

install binaries as fast and as easy as possible. like homebrew but with less fizz.

if you found yourself somehow directly on this page and not programmatically, install using this one-liner:

  curl --proto '=https' --tlsv1.2 -LsSf https://get.dist.sh | sh

for more information checkout the README and our documentation at https://dist.sh

source code -> https://github.com/ekristen/distillery

Please file an issue if you encounter any problems!

===============================================================================
ATTENTION WINDOWS USERS
===============================================================================
If you are looking to install on windows use the following command:

  iwr https://get.dist.sh/install.ps1 -useb | iex

===============================================================================

Fetching installer.sh for version: latest
Downloading distillery v1.8.9...
Verifying checksums...
Checksum verification succeeded.
Could not verify signatures, cosign is not installed.
   • distillery/v1.8.9        
   • source: github           
   • app: ekristen/distillery 
   • os: linux                
   • arch: amd64              
   • version: v1.8.9          
   • downloading assets       
   • checksum verified        
   • signature verified       
   • installation complete in 5.085120998s
Installation Complete!

You still have to export the distillery bin directory to your PATH
For example, add the following to your .bashrc or .zshrc file:

For bash:

  echo "export PATH=$HOME/.distillery/bin:$PATH" >> $HOME/.bashrc

For zsh:

  echo "export PATH=$HOME/.distillery/bin:$PATH" >> $HOME/.zshrc

# パスの追加
~ $ echo "export PATH=$HOME/.distillery/bin:$PATH" >> $HOME/.bashrc

# シェルの再起動
~ $ exec $SHELL -l

# aws-nuke のインストール
~ $ dist install ekristen/aws-nuke
   • distillery/v1.8.9        
   • source: github           
   • app: ekristen/aws-nuke   
   • os: linux                
   • arch: amd64              
   • determining latest version
   • version: 3.63.2          
   • downloading assets       
   • checksum verified        
   • signature verified       
   • installation complete in 5.59373719s

これで事前準備が完了しました。

aws-nuke の実行

あとは aws-nuke を実行して AWS リソースを削除するだけです。
aws-nuke のオプションはこちらのサイトにあります。

aws-nuke.ekristen.dev

安全な方法としては以下のサイトに書かれていますが、1 回目は削除せずに削除対象の AWS リソースを確認し (ドライラン)、2 回目で削除を実施するという方法です。

aws-nuke.ekristen.dev

CloudShell で以下のコマンドを実行します。

1 回目 (ドライラン)

~ $ aws-nuke run -c nuke-config.yml
WARN[0000] deprecated configuration key 'account-blocklist' - please use 'blocklist' instead  component=config
aws-nuke - v3.63.2 - 35cd0228ea0f6f04f3da04d9152f6e12ce1719a8
Do you really want to nuke the account with the ID 000000000000 and the alias 'training-xxxxxxx?
Do you want to continue? Enter account alias to continue.
> training-xxxxxxx  # アカウントエイリアスを入力して Enter

starting scan for resources
ERRO[0020] listing failed:
  :
(省略)
  :
The above resources would be deleted with the supplied configuration. Provide --no-dry-run to actually destroy resources.

2 回目 (ドライランなし)

~ $ aws-nuke run -c nuke-config.yml --no-dry-run
WARN[0000] deprecated configuration key 'account-blocklist' - please use 'blocklist' instead  component=config
aws-nuke - v3.63.2 - 35cd0228ea0f6f04f3da04d9152f6e12ce1719a8
Do you really want to nuke the account with the ID 000000000000 and the alias 'training-xxxxxxx'?
Do you want to continue? Enter account alias to continue.
> training-xxxxxxx  # アカウントエイリアスを入力して Enter

starting scan for resources
ERRO[0012] listing failed:
  :
(省略)
  :
Do you really want to nuke the account with the ID 000000000000 and the alias 'training-account-01'?
Do you want to continue? Enter account alias to continue.
> training-xxxxxxx  # アカウントエイリアスを入力して Enter
  :
(省略)
  :
~ $  # プロンプトが返ってきたら実行完了

これで AWS リソースの削除は完了です。
マネジメントコンソールでもともと存在していた AWS リソースを確認して削除されていることや実行した次の日以降に AWS Cost Explorer で AWS アカウントの利用料金がかかっていないことを確認しましょう。

まとめ

aws-nuke を AWS CloudShell で実行して AWS リソースを削除してみました。強力なツールのため、使い方を誤ると復旧などが大変になりますが、AWS アカウントをきれいにしたいときには便利なツールとなっています。現在はドキュメントが整備されていますので、確認しながら慎重に使うことをおすすめします。

小倉 大(記事一覧)

アプリケーションサービス部エデュケーショナルサービス課 札幌在住

AWSトレーニングの講師をしています。

最近は7歳の息子と遊ぶのが楽しいです!

Twitter: @MasaruOgura