Skip to content

Instantly share code, notes, and snippets.

@johnko
Forked from jokeru/aws_delete-default-vpc.sh
Last active January 30, 2022 09:56
Show Gist options
  • Save johnko/f08e6db94936ec6b9a9cb8c8a665f014 to your computer and use it in GitHub Desktop.
Save johnko/f08e6db94936ec6b9a9cb8c8a665f014 to your computer and use it in GitHub Desktop.

Revisions

  1. johnko revised this gist Jan 30, 2022. No changes.
  2. johnko revised this gist Jan 30, 2022. 1 changed file with 49 additions and 26 deletions.
    75 changes: 49 additions & 26 deletions _aws_delete-default-vpc.sh
    Original file line number Diff line number Diff line change
    @@ -1,42 +1,65 @@
    #/usr/bin/env bash
    set -euo pipefail

    exit 1

    # Need the SystemAdministrator AWS SSO / IAM PermissionSet

    for region in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text); do

    echo "* Region ${region}"

    # get default vpc
    vpc=$(aws ec2 --region ${region} describe-vpcs --filter Name=isDefault,Values=true --query 'Vpcs[0].VpcId' --output text)
    if [ "${vpc}" = "None" ]; then
    if [ "${vpc}" != "None" ]; then
    echo "Found default vpc ${vpc}"

    # get internet gateway
    igw=$(aws ec2 --region ${region} describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} --query 'InternetGateways[0].InternetGatewayId' --output text)
    if [ "${igw}" != "None" ]; then
    echo "Detaching and deleting internet gateway ${igw}"
    aws ec2 --region ${region} detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
    aws ec2 --region ${region} delete-internet-gateway --internet-gateway-id ${igw}
    fi

    # get subnets
    subnets=$(aws ec2 --region ${region} describe-subnets --filters Name=vpc-id,Values=${vpc} --query 'Subnets[].SubnetId' --output text)
    if [ "${subnets}" != "None" ]; then
    for subnet in ${subnets}; do
    echo "Deleting subnet ${subnet}"
    aws ec2 --region ${region} delete-subnet --subnet-id ${subnet}
    done
    fi

    # https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
    # - You can't delete the main route table
    # - You can't delete the default network acl
    # - You can't delete the default security group

    # delete default vpc
    echo "Deleting vpc ${vpc}"
    aws ec2 --region ${region} delete-vpc --vpc-id ${vpc}
    else
    echo " No default vpc found"
    continue
    fi
    echo "Found default vpc ${vpc}"

    # get internet gateway
    igw=$(aws ec2 --region ${region} describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} --query 'InternetGateways[0].InternetGatewayId' --output text)
    if [ "${igw}" != "None" ]; then
    echo "Detaching and deleting internet gateway ${igw}"
    aws ec2 --region ${region} detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
    aws ec2 --region ${region} delete-internet-gateway --internet-gateway-id ${igw}
    fi

    # get subnets
    subnets=$(aws ec2 --region ${region} describe-subnets --filters Name=vpc-id,Values=${vpc} --query 'Subnets[].SubnetId' --output text)
    if [ "${subnets}" != "None" ]; then
    for subnet in ${subnets}; do
    echo "Deleting subnet ${subnet}"
    aws ec2 --region ${region} delete-subnet --subnet-id ${subnet}
    done
    fi
    # # get dhcp options
    # dhcpoptions=$(aws ec2 --region ${region} describe-dhcp-options --query 'DhcpOptions[0].DhcpOptionsId' --output text)
    # if [ "${dhcpoptions}" != "None" ]; then
    # for dhcp in ${dhcpoptions}; do

    # if aws ec2 --region ${region} describe-dhcp-options --dhcp-options-id ${dhcp} --query 'DhcpOptions[0].DhcpConfigurations[].Key' --output text \
    # | grep -q 'domain-name\s*domain-name-servers'; then

    # if aws ec2 --region ${region} describe-dhcp-options --dhcp-options-id ${dhcp} --query 'DhcpOptions[0].DhcpConfigurations[].Values[].Value' --output text \
    # | grep -q -E '('$region'.compute.internal\s*AmazonProvidedDNS|ec2.internal\s*AmazonProvidedDNS)'; then

    # https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
    # - You can't delete the main route table
    # - You can't delete the default network acl
    # - You can't delete the default security group
    # echo "Deleting dhcp-options ${dhcp}"
    # aws ec2 --region ${region} delete-dhcp-options --dhcp-options-id ${dhcp}

    # delete default vpc
    echo "Deleting vpc ${vpc}"
    aws ec2 --region ${region} delete-vpc --vpc-id ${vpc}
    # fi
    # fi
    # done
    # fi

    done
  3. johnko revised this gist Jan 30, 2022. 1 changed file with 42 additions and 58 deletions.
    100 changes: 42 additions & 58 deletions _aws_delete-default-vpc.sh
    Original file line number Diff line number Diff line change
    @@ -1,58 +1,42 @@
    #!/usr/bin/env bash


    if [ "$AWS_PROFILE" = "" ]; then
      echo "No AWS_PROFILE set"
      exit 1
    fi


    for region in $(aws ec2 describe-regions --region eu-west-1 | jq -r .Regions[].RegionName); do

      echo "* Region ${region}"

      # get default vpc
      vpc=$(aws ec2 --region ${region} \
        describe-vpcs --filter Name=isDefault,Values=true \
        | jq -r .Vpcs[0].VpcId)
      if [ "${vpc}" = "null" ]; then
        echo "No default vpc found"
        continue
      fi
      echo "Found default vpc ${vpc}"

      # get internet gateway
      igw=$(aws ec2 --region ${region} \
        describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} \
        | jq -r .InternetGateways[0].InternetGatewayId)
      if [ "${igw}" != "null" ]; then
        echo "Detaching and deleting internet gateway ${igw}"
        aws ec2 --region ${region} \
          detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
        aws ec2 --region ${region} \
          delete-internet-gateway --internet-gateway-id ${igw}
      fi

      # get subnets
      subnets=$(aws ec2 --region ${region} \
        describe-subnets --filters Name=vpc-id,Values=${vpc} \
        | jq -r .Subnets[].SubnetId)
      if [ "${subnets}" != "null" ]; then
        for subnet in ${subnets}; do
          echo "Deleting subnet ${subnet}"
          aws ec2 --region ${region} \
            delete-subnet --subnet-id ${subnet}
        done
      fi

      # https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
      # - You can't delete the main route table
      # - You can't delete the default network acl
      # - You can't delete the default security group

      # delete default vpc
      echo "Deleting vpc ${vpc}"
      aws ec2 --region ${region} \
        delete-vpc --vpc-id ${vpc}

    done
    #/usr/bin/env bash
    set -euo pipefail

    for region in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text); do

    echo "* Region ${region}"

    # get default vpc
    vpc=$(aws ec2 --region ${region} describe-vpcs --filter Name=isDefault,Values=true --query 'Vpcs[0].VpcId' --output text)
    if [ "${vpc}" = "None" ]; then
    echo " No default vpc found"
    continue
    fi
    echo "Found default vpc ${vpc}"

    # get internet gateway
    igw=$(aws ec2 --region ${region} describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} --query 'InternetGateways[0].InternetGatewayId' --output text)
    if [ "${igw}" != "None" ]; then
    echo "Detaching and deleting internet gateway ${igw}"
    aws ec2 --region ${region} detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
    aws ec2 --region ${region} delete-internet-gateway --internet-gateway-id ${igw}
    fi

    # get subnets
    subnets=$(aws ec2 --region ${region} describe-subnets --filters Name=vpc-id,Values=${vpc} --query 'Subnets[].SubnetId' --output text)
    if [ "${subnets}" != "None" ]; then
    for subnet in ${subnets}; do
    echo "Deleting subnet ${subnet}"
    aws ec2 --region ${region} delete-subnet --subnet-id ${subnet}
    done
    fi

    # https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
    # - You can't delete the main route table
    # - You can't delete the default network acl
    # - You can't delete the default security group

    # delete default vpc
    echo "Deleting vpc ${vpc}"
    aws ec2 --region ${region} delete-vpc --vpc-id ${vpc}

    done
  4. johnko renamed this gist Jan 30, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @jokeru jokeru created this gist Mar 31, 2018.
    58 changes: 58 additions & 0 deletions aws_delete-default-vpc.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    #!/usr/bin/env bash


    if [ "$AWS_PROFILE" = "" ]; then
      echo "No AWS_PROFILE set"
      exit 1
    fi


    for region in $(aws ec2 describe-regions --region eu-west-1 | jq -r .Regions[].RegionName); do

      echo "* Region ${region}"

      # get default vpc
      vpc=$(aws ec2 --region ${region} \
        describe-vpcs --filter Name=isDefault,Values=true \
        | jq -r .Vpcs[0].VpcId)
      if [ "${vpc}" = "null" ]; then
        echo "No default vpc found"
        continue
      fi
      echo "Found default vpc ${vpc}"

      # get internet gateway
      igw=$(aws ec2 --region ${region} \
        describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} \
        | jq -r .InternetGateways[0].InternetGatewayId)
      if [ "${igw}" != "null" ]; then
        echo "Detaching and deleting internet gateway ${igw}"
        aws ec2 --region ${region} \
          detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
        aws ec2 --region ${region} \
          delete-internet-gateway --internet-gateway-id ${igw}
      fi

      # get subnets
      subnets=$(aws ec2 --region ${region} \
        describe-subnets --filters Name=vpc-id,Values=${vpc} \
        | jq -r .Subnets[].SubnetId)
      if [ "${subnets}" != "null" ]; then
        for subnet in ${subnets}; do
          echo "Deleting subnet ${subnet}"
          aws ec2 --region ${region} \
            delete-subnet --subnet-id ${subnet}
        done
      fi

      # https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
      # - You can't delete the main route table
      # - You can't delete the default network acl
      # - You can't delete the default security group

      # delete default vpc
      echo "Deleting vpc ${vpc}"
      aws ec2 --region ${region} \
        delete-vpc --vpc-id ${vpc}

    done