新規アカウント作成後に CloudShell(AWS CLI) を使ってデフォルトVPCを削除する

2022.02.28

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

デフォルトVPCはアカウント作成時に自動で用意されるVPCです。 インターネットとの接続が可能なネットワーク設定になっています。 このVPCは特別な要件が無い限り削除してしまうのがいいでしょう。 意図せず このVPC上にリソースを配置して、パブリック公開になってしまう事態を避けるためです。

今回は「新規アカウント作成後に(全リージョンの)デフォルトVPCを削除する」ために、CloudShell (AWS CLI) 版の手順を書いてみました。

※今回は手動ですが、自動化のやりかたは色々あります。以下 自動化の1つです。

デフォルトVPCを削除する手順

0. 前提

  • 新規アカウント作成後の「ワークロードがまだ構築されていない」環境を想定しています
  • EC2(VPC)周りの変更権限を持つユーザー(たとえば AdministratorAccess )が必要です

1. AWS CloudShell の起動

マネコンにログインします。

上部 にある CloudShellアイコンをクリック、もしくは 「CloudShell へのアクセスリンク」を開きます。

img

CloudShell コンソールへ入れること、 aws --versionaws sts get-caller-identity で AWS CLIが実施できること確認できればOKです。

img

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

以下コマンドを実行して、デフォルトVPCを削除します。

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

CloudShell の設定によっては以下のような表示が事前に出ることがあります。 内容を確認して [Paste] を選択します。

img

手順としてはこれで完了です。コンソール上では以下のような出力が確認できます。

# deleting vpc: vpc-6b6f9c00 in ap-northeast-2
## deleting igw: igw-efe14087 in vpc-6b6f9c00, ap-northeast-2
--> detatching
--> deleteing
## deleting subnet: subnet-1d615f41 in vpc-6b6f9c00, ap-northeast-2
## deleting subnet: subnet-f73d6dbb in vpc-6b6f9c00, ap-northeast-2
## deleting subnet: subnet-c8bb57a3 in vpc-6b6f9c00, ap-northeast-2
## deleting subnet: subnet-1fa07964 in vpc-6b6f9c00, ap-northeast-2
## finally, deleting vpc: vpc-6b6f9c00 in ap-northeast-2
# deleting vpc: vpc-0b1b206c in us-west-1
## deleting igw: igw-6adfe60e in vpc-0b1b206c, us-west-1
--> detatching
--> deleteing
## deleting subnet: subnet-7b050320 in vpc-0b1b206c, us-west-1
...(略)

(切り戻す場合) デフォルトVPCを復元する

デフォルトVPCを復元したい場合は以下コマンドを実行して、デフォルトVPCを復元します。

aws --output text ec2 describe-regions --query "Regions[].[RegionName]" \
| while read region; do
  echo "creating default VPC in ${region}"
  aws --region ${region} ec2 create-default-vpc
done

おわりに

新規アカウント作成時のベースライン作成プロセスの 1例でした。 マルチアカウント戦略では、新規アカウントのガードレール・ベースラインの対応が大変になってきます。 CLIや自動化で効率化できる部分はどんどん効率化していきましょう。

参考