Skip to content

Instantly share code, notes, and snippets.

@stefansedich
Created December 4, 2019 18:54
Show Gist options
  • Save stefansedich/79072d3dbec9b92c8c3fb9252c36ce9a to your computer and use it in GitHub Desktop.
Save stefansedich/79072d3dbec9b92c8c3fb9252c36ce9a to your computer and use it in GitHub Desktop.
#!/bin/bash
set -eou pipefail
regions=$(aws ec2 describe-regions --query="Regions[].RegionName" --output=text)
for region in $regions
do
vpc_id=$(aws ec2 describe-vpcs --region=$region --filter="Name=is-default,Values=true" --query="Vpcs[].VpcId" --output=text)
if ! [ -z $vpc_id ]
then
echo "Deleting $vpc_id in $region"
subnet_ids=$(aws ec2 describe-subnets --region=$region --filter="Name=vpc-id,Values=$vpc_id" --query="Subnets[].SubnetId" --output=text)
for subnet_id in $subnet_ids
do
echo "Deleting subnet $subnet_id in $region"
aws ec2 delete-subnet --region=$region --subnet-id="$subnet_id"
done
igw_ids=$(aws ec2 describe-internet-gateways --region="$region" --filter="Name=attachment.vpc-id,Values=$vpc_id" --query="InternetGateways[].InternetGatewayId" --output=text)
for igw_id in $igw_ids
do
echo "Deleting igw $igw_id in $region"
aws ec2 detach-internet-gateway --region="$region" --internet-gateway-id="$igw_id" --vpc-id="$vpc_id"
aws ec2 delete-internet-gateway --region="$region" --internet-gateway-id="$igw_id"
done
aws ec2 delete-vpc --region="$region" --vpc-id="$vpc_id"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment