Skip to content

Commit

Permalink
unused aws instace and vpcs cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: shiva kumar <[email protected]>
  • Loading branch information
shivakunv committed Dec 18, 2024
1 parent da298b9 commit b8e359b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions scripts/awscleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ for eni in $eni_ids; do
aws ec2 delete-network-interface --network-interface-id "$eni"
done

# Delete Network ACLs
nw_acls=$(aws ec2 describe-network-acls --filters "Name=vpc-id,Values=$vpc" --query "NetworkAcls[?IsDefault==false].NetworkAclId" --output text)
for acl in $nw_acls; do
echo "Deleting Network ACL: $acl"
aws ec2 delete-network-acl --network-acl-id $acl
done

scripts/checkdependency.sh $vpc

# Delete vpc
# try 3 times with 30 seconds interval
attempts=0
Expand Down
85 changes: 85 additions & 0 deletions scripts/checkdependency.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash

vpc="$1"

instances=$(aws ec2 describe-instances \
--filters "Name=vpc-id,Values=$vpc" \
--query "Reservations[].Instances[].InstanceId" \
--output text | tr -d '\r' | tr '\n' ' ')
if [ ! -z "$instances" ]; then
echo "Please delete the following instances before deleting the VPC:"
echo $instances
exit 1
fi

igws=$(aws ec2 describe-internet-gateways --filters "Name=attachment.vpc-id,Values=$vpc" --query "InternetGateways[].InternetGatewayId" --output text)
if [ ! -z "$igws" ]; then
echo "Please detach and delete the following Internet Gateways before deleting the VPC:"
echo $igws
exit 1
fi

nat_gateways=$(aws ec2 describe-nat-gateways \
--filter Name=vpc-id,Values=$vpc \
--query "NatGateways[].NatGatewayId" \
--output text | tr -d '\r' | tr '\n' ' ')
if [ ! -z "$nat_gateways" ]; then
echo "Please detach and delete the following nat_gateways:"
echo $nat_gateways
exit 1
fi

eips=$(aws ec2 describe-addresses \
--filters Name=domain,Values=vpc \
--query "Addresses[].[AllocationId,Association.VpcId]" \
--output text | grep "$vpc" | awk '{print $1}' | tr -d '\r' | tr '\n' ' ')
if [ ! -z "$eips" ]; then
echo "Please detach and delete the following eips:"
echo $eips
exit 1
fi

sgs=$(aws ec2 describe-security-groups --filters "Name=vpc-id,Values=$vpc" --query "SecurityGroups[?GroupName!='default'].GroupId" --output text)
if [ ! -z "$sgs" ]; then
echo "Please delete the following security groups before deleting the VPC:"
echo $sgs
exit 1
fi

route_tables=$(aws ec2 describe-route-tables \
--filters Name=vpc-id,Values=$vpc \
--query "RouteTables[?Associations[?Main==false]].RouteTableId" \
--output text | tr -d '\r' | tr '\n' ' ')
if [ ! -z "$route_tables" ]; then
echo "Please delete the following route_tables before deleting the VPC:"
echo $route_tables
exit 1
fi

eni_ids=$(aws ec2 describe-network-interfaces \
--filters Name=vpc-id,Values=$vpc \
--query "NetworkInterfaces[].NetworkInterfaceId" \
--output text | tr -d '\r' | tr '\n' ' ')
if [ ! -z "$eni_ids" ]; then
echo "Please delete the following eni_ids before deleting the VPC:"
echo $eni_ids
exit 1
fi


subnets=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc" --query "Subnets[].SubnetId" --output text)
if [ ! -z "$subnets" ]; then
echo "Please delete the following subnets before deleting the VPC:"
echo $subnets
exit 1
fi

nw_acls=$(aws ec2 describe-network-acls --filters "Name=vpc-id,Values=$vpc" --query "NetworkAcls[?IsDefault==false].NetworkAclId" --output text)
if [ ! -z "$subnets" ]; then
echo "Please delete the following nw_acls before deleting the VPC:"
echo $nw_acls
exit 1
fi

echo "No dependencies found. Proceeding with VPC deletion..."

0 comments on commit b8e359b

Please sign in to comment.