Skip to content

Instantly share code, notes, and snippets.

@camposdelima
Created August 1, 2022 02:36
Show Gist options
  • Save camposdelima/87bbffc623600cead9415d57f549090f to your computer and use it in GitHub Desktop.
Save camposdelima/87bbffc623600cead9415d57f549090f to your computer and use it in GitHub Desktop.
Generate prune script to default vpc aws
export REGIONS=$(aws ec2 describe-regions | jq -r ".Regions[].RegionName ")
for region in $REGIONS ; do
echo "####### Killing $region"
# list vpcs
export IDs=$(aws --region=$region ec2 describe-vpcs | jq -r ".Vpcs[]|{is_default: .IsDefault, id: .VpcId} | select(.is_default) | .id")
for id in "$IDs" ; do
if [ -z "$id" ] ; then
continue
fi
# kill igws
for igw in `aws --region=$region ec2 describe-internet-gateways | jq -r ".InternetGateways[] | {id: .InternetGatewayId, vpc: .Attachments[0].VpcId} | select(.vpc == \"$id\") | .id"` ; do
echo "#Killing igw $region $id $igw"
echo aws --region=$region ec2 detach-internet-gateway --internet-gateway-id=$igw --vpc-id=$id
echo aws --region=$region ec2 delete-internet-gateway --internet-gateway-id=$igw
done
# kill subnets
for sub in `aws --region=$region ec2 describe-subnets | jq -r ".Subnets[] | {id: .SubnetId, vpc: .VpcId} | select(.vpc == \"$id\") | .id"` ; do
echo "#Killing subnet $region $id $sub"
echo aws --region=$region ec2 delete-subnet --subnet-id=$sub
done
echo "#Killing vpc $region $id"
echo aws --region=$region ec2 delete-vpc --vpc-id=$id
done
echo "############################################################################################"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment