LoginSignup
15
2

More than 3 years have passed since last update.

AWS アカウントをぶっ壊したいあなたへ

Last updated at Posted at 2019-12-20

フューチャーAdvent Calendar 2019 の20日目です。

はじめに

AWS アカウントのリソースを全てぶっ壊したいと思った経験はないでしょうか?
ぼくはあります。
要はAWS アカウントに存在するリソースを壊すと言うことなんですが、これ結構大変なんですよね。

基本はTerraform で環境を構築して、壊したい時は、destroy コマンドで壊す。
しかし、アカウント用途によっては、野良リソースが増えていくのが現実です。
Terraform で管理しているリソースであればいいのですが、手動作成したリソースなど(Terraform 管理外)はdestroy 対象になりません。

そんなツラミとかも全て吹っ飛ばして、ぶっ壊すツールを紹介したいと思います。

aws-nuke

aws-nukeは、AWS アカウントのリソースを全て削除するというツールです。
すでにnukeと言う名前の時点で物騒なものであることは想像できると思います。

環境

今回テストで使用した環境を以下に記載します。

  • AWS
  • macOS Mojave 10.14.4
  • aws-nuke v2.13.0

事前準備

aws-nuke を使用するために事前準備を行います。

aws-nuke ダウンロード

バイナリをダウンロードして、権限を付与します。

$ curl -OL https://github.com/rebuy-de/aws-nuke/releases/download/v2.13.0/aws-nuke-v2.13.0-darwin-amd64
$ chmod u+x aws-nuke-v2.13.0-darwin-amd64

クレデンシャル設定

AWS リソース操作権限を付与するために、クレデンシャル設定を行います。
以下の設定ファイルにプロファイルを作成します。
プロファイル名をnukeとします。

  • ~/.aws/config
  • ~/.aws/credentials

config ファイルにリージョンと出力形式を記載します。

~/.aws/config
[profile nuke]
region=ap-southeast-2
output=json

credentials にアクセスキーとシークレットキーを記載します。

~/.aws/credentials
[nuke]
aws_access_key_id=xxx
aws_secret_access_key=xxx

これで事前準備が整いました。
次にaws-nuke のconfig ファイルを作成します。

aws-nuke 設定

aws-nuke のconfig は、YAML 形式で記述します。
シンプルにぶっ壊したい場合は、以下となります。

nuke-config.yml
regions:
- ap-southeast-2
- global

account-blacklist:
- "999999999999"

accounts:
  "000000000000": {}

上記のconfig ファイルについて以下に記載します。

  • region: ぶっ壊したいリージョンを指定します
  • account-blacklist: ぶっ壊したくないアカウントを指定します(必須入力なので、壊したくないアカウントがない場合でも適当な値を入れる必要があります)
  • accounts: ぶっ壊したいアカウントを記述します

実行方法

バイナリを指定して実行します。
オプションの-c で先ほど作成したconfig ファイルを指定します。
--profile でプロファイル名を指定します。

$ ./aws-nuke-v2.13.0-darwin-amd64 -c nuke-config.yml --profile nuke

あまり勧めないですが、以下の方法でも実行可能です。

$ ./aws-nuke-v2.13.0-darwin-amd64 -c nuke-config.yml --access-key-id xxx --secret-access-key xxx

クレデンシャルに誤りがある場合は、以下のエラーメッセージが表示されます。

Error: failed get caller identity: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors

ぶっ壊す

コマンドを実行します。
AWS アカウントに付けているエイリアスを入力します(ここでは、nuke としています)

$ ./aws-nuke-v2.13.0-darwin-amd64 -c nuke-config.yml --profile nuke
Do you really want to nuke the account with the ID xxx and the alias 'nuke'?
Do you want to continue? Enter account alias to continue.
> nuke
ap-southeast-2 - EC2NetworkACL - acl-xxx - cannot delete default VPC
ap-southeast-2 - EC2SecurityGroup - sg-xxx - [Name: "launch-wizard-1"] - would remove
ap-southeast-2 - EC2SecurityGroup - sg-d0ecdeb7 - [Name: "default"] - cannot delete group 'default'
ap-southeast-2 - EC2Volume - vol-xxx - [State: "in-use"] - would remove
.
..
The above resources would be deleted with the supplied configuration. Provide --no-dry-run to actually destroy resources.

あんなに壊したかったリソースに対して、なぜか安堵する文章が最後に出てきます。

Readme に記載されていますが、aws-nuke は、デフォルトdry-run です。
本当にぶっ壊したい時は、--no-dry-run を付与してコマンド実行します。

By default aws-nuke only lists all nukeable resources. You need to add --no-dry-run to actually delete resources.

リソースを指定してぶっ壊す

ここでは、EC2 インスタンスを対象にぶっ壊したいと思います。

nuke-config.yml
regions:
- ap-southeast-2

resource-types:
  targets:
  - EC2Instance

account-blacklist:
- "999999999999"

accounts:
  "000000000000": {}

--no-dry-run を付けて実行します。
本当にぶっ壊して良いんだな?と二回聞かれるので、二回エイリアスを入力します。

$ ./aws-nuke-v2.13.0-darwin-amd64 -c nuke-config.yml --profile nuke --no-dry-run
Do you really want to nuke the account with the ID xxx and the alias 'nuke'?
Do you want to continue? Enter account alias to continue.
> nuke

ap-southeast-2 - EC2Instance - i-xxx - [] - would remove
Scan complete: 1 total, 1 nukeable, 0 filtered.

Do you really want to nuke these resources on the account with the ID xxx and the alias 'nuke'?
Do you want to continue? Enter account alias to continue.
> nuke

ap-southeast-2 - EC2Instance - i-xxx - [] - triggered remove

Removal requested: 1 waiting, 0 failed, 0 skipped, 0 finished

ap-southeast-2 - EC2Instance - i-xxx - [] - waiting

Removal requested: 1 waiting, 0 failed, 0 skipped, 0 finished

ap-southeast-2 - EC2Instance - i-xxx - [] - waiting

Removal requested: 1 waiting, 0 failed, 0 skipped, 0 finished

ap-southeast-2 - EC2Instance - i-xxx - [] - waiting

Removal requested: 1 waiting, 0 failed, 0 skipped, 0 finished

ap-southeast-2 - EC2Instance - i-xxx - [] - waiting

Removal requested: 1 waiting, 0 failed, 0 skipped, 0 finished

ap-southeast-2 - EC2Instance - i-xxx - [] - removed

Removal requested: 0 waiting, 0 failed, 0 skipped, 1 finished

Nuke complete: 0 failed, 0 skipped, 1 finished.

これでap-southeast-2 に存在するEC2 は削除されました。
このようにリソースを指定して実行することも可能ですし、以下のようにフィルタをかけて実行することも可能です。
以下の例では、管理者アカウントは消したくないといった時に利用します。

regions:
- eu-west-1

account-blacklist:
- "999999999999"

accounts:
  "000000000000":
    filters:
      IAMUser:
      - "my-user"
      IAMUserPolicyAttachment:
      - "my-user -> AdministratorAccess"
      IAMUserAccessKey:
      - "my-user -> ABCDEFGHIJKLMNOPQRST"

他にもサンプルがaws-nukeのReadme に記載されています。

まとめ

如何でしたか?
aws-nuke を利用することでツラミであったぶっ壊し作業が楽になったのではないでしょうか。
また、このツールを利用すれば、サンドボックス用のアカウントを定期的にキレイにするといったことも可能です。

15
2
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
15
2