ぶろとよ

ネットワーク系クラウドエンジニア(AWS)の技術ブログ。自動化に興味があるためAWS CLIを勉強&アウトプット中。

全リージョンのデフォルトVPC系リソースを闇に葬ってみる

AWSアカウントを作成すると「デフォルトVPC」が作成されるのですが、いままで一度も使用したことがありません。
自分でVPCから作成しちゃった方が勉強になるし、デフォルトVPCのCIDRも変更できないので。

AWSサポートの記事を見ると、「デフォルトVPCを使用していない」を条件に、基本的には削除しちゃっても問題ないみたいです。

aws.amazon.com

※WEBで少し見た感じだと、「TransitGateway」と「Elastic Beanstalk」は少し注意が必要かもしれない・・・。


自分はデフォルトVPCをまったく使用していないし、何か邪魔なので、、、闇に葬ろうと思います!


目次

検証環境

  • 検証日: 2022/★
  • 実行環境
    • PC
      • Windows10 Home Ver21H1
        • Google Chrome: バージョン: 105.0.5195.102(Official Build) (64 ビット)
    • AWS操作
      • AWSマネジメントコンソール
        • IAMユーザー: GUI操作の管理者権限
      • AWS CloudShell
        • AWS CLI: aws-cli/2.7.29 Python/3.9.11 Linux/4.14.287-215.504.amzn2.x86_64 exec-env/CloudShell exe/x86_64.amzn.2 prompt/off

注意事項

  • 本記事の内容はAWSマネジメントコンソールAWS CloudShellで検証しています。
  • AWS CLIはバージョン2を使用しています(CloudShellのデフォルトがバージョン2)。
  • コマンド内の変数やパラメータやは検証で使用したものを記載しています。ご自身の環境に合わせ、書き換えて使用してください。
  • 個人で検証しているため実行結果に責任は持てません。必ずご自身でも検証してから使用してください。
  • ページ内のリソースIDは、既に削除済みリソースなのでそのまま記載しています。


1. アカウント作成初期状態で、デフォルトVPC系リソースを探す

今回、デフォルトVPC系のリソースを削除しようと思うのですが、「系」の部分にどんなリソースがあるのかを先に洗い出してみます。
※サブネット、セキュリティグループ等です

1.1. AWSドキュメントを見てみる

docs.aws.amazon.com

「デフォルト VPC のコンポーネント」を読んでみると、デフォルトVPC作成時に以下リソースが作成されているみたいです。
これが、リージョン数分です。

種別 個数 備考
VPC 1 CIDR: 172.31.0.0/16
サブネット AZ数分 CIDR: xx.xx.xx.xx/20
インターネットゲートウェイ 1
ルートテーブル 1 静的ルート: 0.0.0.0/0をインターネットゲートウェイ向け
セキュリティグループ 1
ネットワークACL 1
DHCP オプションセット 1


1.2. EC2 Global Viewでリソース数を見てみる

EC2 Global View とは
AWS リージョン全域で、インスタンス、VPC、サブネット、セキュリティグループ、ボリュームなどの AWS リソースを閲覧できる機能。

■ 確認方法

  1. EC2 Global View へアクセスする
  2. 「リソースの概要」「リージョンあたりのリソース数」が表示される


「リソースの概要」を見てみると、17リージョンでリソースが作成されています。


1.3. TagEditorでリソースを洗い出す

EC2 Global Viewだと以下リソースが表示されないため、TagEditorでも検索してみました。

  • インターネットゲートウェイ
  • ルートテーブル
  • ネットワークACL
  • DHCP オプションセット

※TagEditorの使い方は過去記事で書いているので、こちらを参照してください。

blog.toyokky.com


■ 確認方法

  1. TagEditorへアクセスする ※リージョン指定はありません
  2. 「タグ付けするリソースを検索」へ必要事項を入力して「リソースを検索」する
    • リージョン: All regions
    • リソースタイプ: All supported resource types
    • タグ - オプション: 空白のまま
  3. 待つ ※出力には5分ほど時間がかかる
  4. 検索が終わったら「xx resources を CSV にエクスポート」からCSVをダウンロードする
    • ※検証時は「152」
  5. CSVは文字コードがUTF-8なので、Excelで開く場合はShift-JISにする


全リージョン分のリソースが出力されています。
試しに東京リージョン(ap-northeast-1)でフィルタするとこんな感じで、AWSドキュメントの内容と一致します。


2. デフォルトVPCを削除する

2.1. 削除一覧

先にデフォルトVPC系リソース削除をする時の一覧です。
・GUIはVPC削除のみで依存関係のある他リソースも消せます。
・AWS CLIは依存関係を考慮して順番に削除していきます。
・VPC削除により ルートテーブル/セキュリティグループ/ネットワークACL は消えます。
・DHCPオプションセットは削除後も残りますが、新規VPCでも使用するのでそのままにします。

GUI AWS CLI 対象リソース 備考
一括削除 削除3番目 VPC AWS CLIでは先にサブネットとインターネットゲートウェイを削除
VPC削除で消える 削除2番目 サブネット
VPC削除で消える 削除1番目 インターネットゲートウェイ VPCからデタッチして削除
VPC削除で消える VPC削除で消える ルートテーブル
VPC削除で消える VPC削除で消える セキュリティグループ
VPC削除で消える VPC削除で消える ネットワークACL
残る 残る DHCP オプションセット 他の新規VPCでも使用するので消さない


2.2. 削除方法(GUI)

以下の方法は、AWSマネジメントコンソールで削除対象リージョン数分のVPC削除作業を繰り返します。
また、残数が見えるように「EC2 Global View」から作業しています。

  1. EC2 Global View へアクセスする
  2. 「リージョンあたりのリソース数」から、削除したいリージョンの「VPC数」をクリックする
    • ※サンプルとして「米国西部(オレゴン)us-west-2」で作業
  3. 「グローバル検索」内に対象VPCの「リソースID」が表示されるのでクリックする
  4. 対象VPCの詳細ページが開くので、「アクション > VPCの削除」を選択する
  5. 「VPCの削除」ページが表示されるので、以下入力して「削除」をクリックする
    • デフォルトVPCの削除を希望~: ■ チェックする
    • 削除を希望するには~: デフォルト VPC の削除
  6. 正常に削除された旨が表示されたら、「EC2 Global View」へ戻る
  7. 対象リージョンでVPC系が「0」になっていることを確認する
    • ※画面表示が遅れることがあるので、画面更新して待つ


2.3. 削除方法(AWS CLI)

削除対象のリソースが、全リージョンだと結構な数になります。軽く数えたら「148」くらい。
大変なのでAWS CLIで一気に消そうと思っ・・・・・たのですが、既にクラスメソッドさんに素敵な記事がありました。

dev.classmethod.jp


ただ記事を紹介するだけだと寂しいので、実際に実行してみました。

  1. CloudShellを開く
  2. URL先の「2. デフォルトVPCを削除する」に書いてあるコマンドをコピー
  3. CloudShellへコマンドをペーストして実行する
    • ※CloudShellでは ctrl + C, Ctrl + v が使える
  4. 待つ:実行時間は「4分」ほど
  5. AWSのVPC環境がキレイになる(´ω`)

参考:出力ログをクリックで展開

Preparing your terminal...
Try these commands to get started:
aws help  or  aws <command> help  or  aws <command> --cli-auto-prompt
[cloudshell-user@ip-10-0-77-140 ~]$ aws --output text ec2 describe-regions --query "Regions[].[RegionName]" \
> | while read region; do
>   aws --region ${region} --output text \
>     ec2 describe-vpcs --query "Vpcs[?IsDefault].[VpcId]" \
>   | while read vpc; do
>     echo "# deleting vpc: ${vpc} in ${region}"
>    
>     ### IGW
>     aws --region ${region} --output text \
>       ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values=${vpc} \
>       --query "InternetGateways[].[InternetGatewayId]" \
>     | while read igw; do
>       echo "## deleting igw: ${igw} in ${vpc}, ${region}"
>       echo "--> detatching"
>       aws --region ${region} --output json \
>         ec2 detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
>       echo "--> deleteing"
>       aws --region ${region} --output json \
>         ec2 delete-internet-gateway --internet-gateway-id ${igw}
>     done
>    
>     ### Subnet
>     aws --region ${region} --output text \
>       ec2 describe-subnets  --filters Name=vpc-id,Values=${vpc} \
>       --query "Subnets[].[SubnetId]" \
>     | while read subnet; do
>       echo "## deleting subnet: ${subnet} in ${vpc}, ${region}"
>       aws --region ${region} --output json \
>         ec2 delete-subnet --subnet-id ${subnet}
>     done
>    
>     ### VPC
>     echo "## finally, deleting vpc: ${vpc} in ${region}"
>     aws --region ${region} --output json \
>       ec2 delete-vpc --vpc-id ${vpc}  
>   done
> done
# deleting vpc: vpc-03da8d2717f74bbd6 in eu-north-1
## deleting igw: igw-0d75702fe4a7012e9 in vpc-03da8d2717f74bbd6, eu-north-1
--> detatching
--> deleteing
## deleting subnet: subnet-0930e7f1924d8de4c in vpc-03da8d2717f74bbd6, eu-north-1
## deleting subnet: subnet-0cb201e4cf78ae47f in vpc-03da8d2717f74bbd6, eu-north-1
## deleting subnet: subnet-0131379f1d6917cb1 in vpc-03da8d2717f74bbd6, eu-north-1
## finally, deleting vpc: vpc-03da8d2717f74bbd6 in eu-north-1
# deleting vpc: vpc-0a1274b6e2ce54fa6 in ap-south-1
## deleting igw: igw-05c707895c4a4f8c9 in vpc-0a1274b6e2ce54fa6, ap-south-1
--> detatching
--> deleteing
## deleting subnet: subnet-0cdf17e0731c9959b in vpc-0a1274b6e2ce54fa6, ap-south-1
## deleting subnet: subnet-096a31c11ba17dea7 in vpc-0a1274b6e2ce54fa6, ap-south-1
## deleting subnet: subnet-0c010a91263d604b4 in vpc-0a1274b6e2ce54fa6, ap-south-1
## finally, deleting vpc: vpc-0a1274b6e2ce54fa6 in ap-south-1
# deleting vpc: vpc-0b924ea784314720b in eu-west-3
## deleting igw: igw-0b8af5cbd1219ff17 in vpc-0b924ea784314720b, eu-west-3
--> detatching
--> deleteing
## deleting subnet: subnet-0073a3e79aefe26d4 in vpc-0b924ea784314720b, eu-west-3
## deleting subnet: subnet-03794e052bf66938d in vpc-0b924ea784314720b, eu-west-3
## deleting subnet: subnet-068101d9db62d7631 in vpc-0b924ea784314720b, eu-west-3
## finally, deleting vpc: vpc-0b924ea784314720b in eu-west-3
# deleting vpc: vpc-09b73d28640d11cfe in eu-west-2
## deleting igw: igw-011523493be4d7b91 in vpc-09b73d28640d11cfe, eu-west-2
--> detatching
--> deleteing
## deleting subnet: subnet-03c05ee9db69ef952 in vpc-09b73d28640d11cfe, eu-west-2
## deleting subnet: subnet-0054226ac9268f6b8 in vpc-09b73d28640d11cfe, eu-west-2
## deleting subnet: subnet-067d836269327892d in vpc-09b73d28640d11cfe, eu-west-2
## finally, deleting vpc: vpc-09b73d28640d11cfe in eu-west-2
# deleting vpc: vpc-00893fd5cecf690b6 in eu-west-1
## deleting igw: igw-04cb66cfccf4205cf in vpc-00893fd5cecf690b6, eu-west-1
--> detatching
--> deleteing
## deleting subnet: subnet-0e000e5caa73b3c43 in vpc-00893fd5cecf690b6, eu-west-1
## deleting subnet: subnet-0938a8bd38a31afb5 in vpc-00893fd5cecf690b6, eu-west-1
## deleting subnet: subnet-0742af315ad78fc9e in vpc-00893fd5cecf690b6, eu-west-1
## finally, deleting vpc: vpc-00893fd5cecf690b6 in eu-west-1
# deleting vpc: vpc-0870eacfc678d6844 in ap-northeast-3
## deleting igw: igw-07ea4ccdad0df2e77 in vpc-0870eacfc678d6844, ap-northeast-3
--> detatching
--> deleteing
## deleting subnet: subnet-0e52d208ac7714619 in vpc-0870eacfc678d6844, ap-northeast-3
## deleting subnet: subnet-057dda54892056ce7 in vpc-0870eacfc678d6844, ap-northeast-3
## deleting subnet: subnet-011c1177d5ff0499d in vpc-0870eacfc678d6844, ap-northeast-3
## finally, deleting vpc: vpc-0870eacfc678d6844 in ap-northeast-3
# deleting vpc: vpc-00f3ca9902f3b1f42 in ap-northeast-2
## deleting igw: igw-06e681bdc36f4d40c in vpc-00f3ca9902f3b1f42, ap-northeast-2
--> detatching
--> deleteing
## deleting subnet: subnet-0b048aacb6ce1a6f0 in vpc-00f3ca9902f3b1f42, ap-northeast-2
## deleting subnet: subnet-059c7b0dcacb660c2 in vpc-00f3ca9902f3b1f42, ap-northeast-2
## deleting subnet: subnet-03456c60222b3ccf4 in vpc-00f3ca9902f3b1f42, ap-northeast-2
## deleting subnet: subnet-083296c69cef32918 in vpc-00f3ca9902f3b1f42, ap-northeast-2
## finally, deleting vpc: vpc-00f3ca9902f3b1f42 in ap-northeast-2
# deleting vpc: vpc-0310232b5cfb7b5b0 in ap-northeast-1
## deleting igw: igw-0027be714b77afca3 in vpc-0310232b5cfb7b5b0, ap-northeast-1
--> detatching
--> deleteing
## deleting subnet: subnet-0862dc3f09ac5a56d in vpc-0310232b5cfb7b5b0, ap-northeast-1
## deleting subnet: subnet-032ca937ad16759d6 in vpc-0310232b5cfb7b5b0, ap-northeast-1
## deleting subnet: subnet-0c61a7e53005cf8f4 in vpc-0310232b5cfb7b5b0, ap-northeast-1
## finally, deleting vpc: vpc-0310232b5cfb7b5b0 in ap-northeast-1
# deleting vpc: vpc-0a81286d3f2772702 in sa-east-1
## deleting igw: igw-0bd80f931b342f07e in vpc-0a81286d3f2772702, sa-east-1
--> detatching
--> deleteing
## deleting subnet: subnet-0b0a79cfc42028d93 in vpc-0a81286d3f2772702, sa-east-1
## deleting subnet: subnet-0ca984de69f9a3446 in vpc-0a81286d3f2772702, sa-east-1
## deleting subnet: subnet-0e1c572334bc35adc in vpc-0a81286d3f2772702, sa-east-1
## finally, deleting vpc: vpc-0a81286d3f2772702 in sa-east-1
# deleting vpc: vpc-08f2b36181aa7f9b5 in ca-central-1
## deleting igw: igw-06810e851b8b4c360 in vpc-08f2b36181aa7f9b5, ca-central-1
--> detatching
--> deleteing
## deleting subnet: subnet-0ca8484018013ee91 in vpc-08f2b36181aa7f9b5, ca-central-1
## deleting subnet: subnet-05d622e140431ef1f in vpc-08f2b36181aa7f9b5, ca-central-1
## deleting subnet: subnet-098946d396ae27ef9 in vpc-08f2b36181aa7f9b5, ca-central-1
## finally, deleting vpc: vpc-08f2b36181aa7f9b5 in ca-central-1
# deleting vpc: vpc-056c07eefccbabaf1 in ap-southeast-1
## deleting igw: igw-0e9e3d1cb6b95d169 in vpc-056c07eefccbabaf1, ap-southeast-1
--> detatching
--> deleteing
## deleting subnet: subnet-04fb8aca1d62c373f in vpc-056c07eefccbabaf1, ap-southeast-1
## deleting subnet: subnet-00218a6234c018fa1 in vpc-056c07eefccbabaf1, ap-southeast-1
## deleting subnet: subnet-00c0b26f30cdcf658 in vpc-056c07eefccbabaf1, ap-southeast-1
## finally, deleting vpc: vpc-056c07eefccbabaf1 in ap-southeast-1
# deleting vpc: vpc-07923ceeb002d7002 in ap-southeast-2
## deleting igw: igw-0d317fd34a911fea1 in vpc-07923ceeb002d7002, ap-southeast-2
--> detatching
--> deleteing
## deleting subnet: subnet-0079c005d87aa2a6f in vpc-07923ceeb002d7002, ap-southeast-2
## deleting subnet: subnet-011908aeb4c5c25d3 in vpc-07923ceeb002d7002, ap-southeast-2
## deleting subnet: subnet-0b676f4c793f5d070 in vpc-07923ceeb002d7002, ap-southeast-2
## finally, deleting vpc: vpc-07923ceeb002d7002 in ap-southeast-2
# deleting vpc: vpc-0d30e720c87b1820c in eu-central-1
## deleting igw: igw-0f2a6e7380e016443 in vpc-0d30e720c87b1820c, eu-central-1
--> detatching
--> deleteing
## deleting subnet: subnet-0ccdfc320c6a543c9 in vpc-0d30e720c87b1820c, eu-central-1
## deleting subnet: subnet-02c30577daec1cb37 in vpc-0d30e720c87b1820c, eu-central-1
## deleting subnet: subnet-08e2ce5d4fe3afa5d in vpc-0d30e720c87b1820c, eu-central-1
## finally, deleting vpc: vpc-0d30e720c87b1820c in eu-central-1
# deleting vpc: vpc-05c2183af252f4cf7 in us-east-1
## deleting igw: igw-08682de8fd2dcdff8 in vpc-05c2183af252f4cf7, us-east-1
--> detatching
--> deleteing
## deleting subnet: subnet-07736db9efef1bd3d in vpc-05c2183af252f4cf7, us-east-1
## deleting subnet: subnet-0da0560da08abe62d in vpc-05c2183af252f4cf7, us-east-1
## deleting subnet: subnet-00275b377be0b5a4c in vpc-05c2183af252f4cf7, us-east-1
## deleting subnet: subnet-0fc8c2638457192a7 in vpc-05c2183af252f4cf7, us-east-1
## deleting subnet: subnet-0b5fdecfb0150bea5 in vpc-05c2183af252f4cf7, us-east-1
## deleting subnet: subnet-04327a186077a7afc in vpc-05c2183af252f4cf7, us-east-1
## finally, deleting vpc: vpc-05c2183af252f4cf7 in us-east-1
# deleting vpc: vpc-0f801526be63a681d in us-east-2
## deleting igw: igw-04a0c2d11999bbfc4 in vpc-0f801526be63a681d, us-east-2
--> detatching
--> deleteing
## deleting subnet: subnet-0d9332bd1485482fa in vpc-0f801526be63a681d, us-east-2
## deleting subnet: subnet-0547c362a3359c858 in vpc-0f801526be63a681d, us-east-2
## deleting subnet: subnet-07e3b1951b3240865 in vpc-0f801526be63a681d, us-east-2
## finally, deleting vpc: vpc-0f801526be63a681d in us-east-2
# deleting vpc: vpc-006a9597f9877621e in us-west-1
## deleting igw: igw-037e488ea69fd14cc in vpc-006a9597f9877621e, us-west-1
--> detatching
--> deleteing
## deleting subnet: subnet-05479399c2cac1b38 in vpc-006a9597f9877621e, us-west-1
## deleting subnet: subnet-0adf17ee9c3d34c1d in vpc-006a9597f9877621e, us-west-1
## finally, deleting vpc: vpc-006a9597f9877621e in us-west-1
# deleting vpc: vpc-0e87d631482171d64 in us-west-2
## deleting igw: igw-0ffe7ea7725f95ffe in vpc-0e87d631482171d64, us-west-2
--> detatching
--> deleteing
## deleting subnet: subnet-08959d98b75a130fc in vpc-0e87d631482171d64, us-west-2
## deleting subnet: subnet-0682a444b6953b34e in vpc-0e87d631482171d64, us-west-2
## deleting subnet: subnet-0572b50aae16e68ad in vpc-0e87d631482171d64, us-west-2
## deleting subnet: subnet-08c8079d314a552ba in vpc-0e87d631482171d64, us-west-2
## finally, deleting vpc: vpc-0e87d631482171d64 in us-west-2


3. 事後確認

3.1. EC2 Global Viewでリソース数を見てみる

■ 確認方法

  1. EC2 Global View へアクセスする
  2. 「リソースの概要」「リージョンあたりのリソース数」が表示される


「リソースの概要」を見てみると、17リージョンでリソースが「0」になりました。


3.2. TagEditorで残ってるリソースを確認する

■ 確認方法

  1. TagEditorへアクセスする ※リージョン指定はありません
  2. 「タグ付けするリソースを検索」へ必要事項を入力して「リソースを検索」する
    • リージョン: All regions
    • リソースタイプ: All supported resource types
    • タグ - オプション: 空白のまま
  3. 待つ ※出力には5分ほど時間がかかる
  4. 検索が終わったら「xx resources を CSV にエクスポート」からCSVをダウンロードする
    • ※検証時は「152 → 20」まで減った
  5. CSVは文字コードがUTF-8なので、Excelで開く場合はShift-JISにする


全リージョン分のリソースが出力されています。
デフォルトVPC系リソースが削除され、DHCPオプションのみが残ってることが確認できました。