Skip to content

Instantly share code, notes, and snippets.

@fridim fridim/delete_vpc.sh
Last active Sep 21, 2019

Embed
What would you like to do?
#!/bin/sh
#
# Delete a VPC and its dependencies
REGION=eu-central-1
REGION=us-west-2
REGION=ap-southeast-1
export AWS_PROFILE=sandbox37
export AWS_PROFILE=sandbox29
if [ -z "$1" ]; then
echo "usage: $0 <vpcid>"
exit 64
fi
vpcid="$1"
# Delete network acl
for i in $(aws --region $REGION ec2 describe-network-acls --filters Name=vpc-id,Values="${vpcid}" --output text --query 'NetworkAcls[].NetworkAclId'); do
aws --region $REGION ec2 delete-network-acl --network-acl-id=$i
if [ $? = 0 ]; then
echo "network acl $i deleted"
fi
done
# Delete route tables
for i in $(aws --region $REGION ec2 describe-route-tables --filters Name=vpc-id,Values="${vpcid}" --output text --query 'RouteTables[].RouteTableId'); do
aws --region $REGION ec2 delete-route-table --route-table-id=$i
if [ $? = 0 ]; then
echo "route table $i deleted"
fi
done
# disassociate route table
for i in $(aws ec2 describe-route-tables --filters Name=vpc-id,Values="${vpcid}" --output text --region $REGION --query 'RouteTables[].Associations[].RouteTableAssociationId'); do
aws ec2 --region $REGION disassociate-route-table --association-id $i
if [ $? = 0 ]; then
echo "route table association $i deleted"
fi
done
# Delete DHCP options
for i in $(aws --region $REGION ec2 describe-dhcp-options --output text --query 'DhcpOptions[].DhcpOptionId'); do
aws --region $REGION ec2 delete-dhcp-option --dhcp-option-id=$i
if [ $? = 0 ]; then
echo "dhcp $i deleted"
fi
done
# Delete subnets
for i in `aws --region $REGION ec2 describe-subnets --filters Name=vpc-id,Values="${vpcid}" | grep subnet- | sed -E 's/^.*(subnet-[a-z0-9]+).*$/\1/'`; do
aws --region $REGION ec2 delete-subnet --subnet-id=$i
if [ $? = 0 ]; then
echo "subets $i deleted"
fi
done
# Detach internet gateways
for i in `aws --region $REGION ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values="${vpcid}" | grep igw- | sed -E 's/^.*(igw-[a-z0-9]+).*$/\1/'`; do
aws --region $REGION ec2 detach-internet-gateway --internet-gateway-id=$i --vpc-id=${vpcid}
if [ $? = 0 ]; then
echo "internet gateway $i detached"
fi
done
# Delete internet gateways
for i in `aws --region $REGION ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values="${vpcid}" | grep igw- | sed -E 's/^.*(igw-[a-z0-9]+).*$/\1/'`; do
aws --region $REGION ec2 delete-internet-gateway --internet-gateway-id=$i;
if [ $? = 0 ]; then
echo "internet gateway $i deleted"
fi
done
# Delete security groups (ignore message about being unable to delete default security group)
for i in `aws --region $REGION ec2 describe-security-groups --filters Name=vpc-id,Values="${vpcid}" | grep sg- | sed -E 's/^.*(sg-[a-z0-9]+).*$/\1/' | sort | uniq`; do
aws --region $REGION ec2 delete-security-group --group-id $i
if [ $? = 0 ]; then
echo "security group $i deleted"
fi
done
# Delete the VPC
aws --region $REGION ec2 delete-vpc --vpc-id ${vpcid}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.