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 17, 2024
1 parent 9e995a2 commit 2a07703
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions scripts/awsvpcscleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export vpc=$1

echo "Start Deleting VPC: $vpc resource"

# Detach Internet Gateway
# Delete Internet Gateway
internet_gateways=$(aws ec2 describe-internet-gateways \
--filters Name=attachment.vpc-id,Values=$vpc \
--query "InternetGateways[].InternetGatewayId" \
Expand All @@ -20,15 +20,24 @@ for igw in $internet_gateways; do
aws ec2 delete-internet-gateway --internet-gateway-id "$igw"
done

# Delete Subnets
subnets=$(aws ec2 describe-subnets \
--filters Name=vpc-id,Values=$vpc \
--query "Subnets[].SubnetId" \
# Delete NAT Gateways
nat_gateways=$(aws ec2 describe-nat-gateways \
--filter Name=vpc-id,Values=$vpc \
--query "NatGateways[].NatGatewayId" \
--output text | tr -d '\r' | tr '\n' ' ')
for ngw in $nat_gateways; do
echo "Deleting NAT Gateway: $ngw"
aws ec2 delete-nat-gateway --nat-gateway-id "$ngw"
done

for subnet in $subnets; do
echo "Deleting subnet: $subnet"
aws ec2 delete-subnet --subnet-id "$subnet"
# Delete Elastic IPs
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' ' ')
for eip in $eips; do
echo "Releasing Elastic IP: $eip"
aws ec2 release-address --allocation-id "$eip"
done

# Delete Route Tables
Expand All @@ -38,9 +47,21 @@ route_tables=$(aws ec2 describe-route-tables \
--output text | tr -d '\r' | tr '\n' ' ')
for rt in $route_tables; do
echo "Deleting route table: $rt"
aws ec2 disassociate-route-table --association-id "$rt"
aws ec2 delete-route-table --route-table-id "$rt"
done

# Delete Subnets
subnets=$(aws ec2 describe-subnets \
--filters Name=vpc-id,Values=$vpc \
--query "Subnets[].SubnetId" \
--output text | tr -d '\r' | tr '\n' ' ')

for subnet in $subnets; do
echo "Deleting subnet: $subnet"
aws ec2 delete-subnet --subnet-id "$subnet"
done

# Delete Network Interfaces
eni_ids=$(aws ec2 describe-network-interfaces \
--filters Name=vpc-id,Values=$vpc \
Expand All @@ -51,26 +72,6 @@ for eni in $eni_ids; do
aws ec2 delete-network-interface --network-interface-id "$eni"
done

# Delete NAT Gateways
nat_gateways=$(aws ec2 describe-nat-gateways \
--filter Name=vpc-id,Values=$vpc \
--query "NatGateways[].NatGatewayId" \
--output text | tr -d '\r' | tr '\n' ' ')
for ngw in $nat_gateways; do
echo "Deleting NAT Gateway: $ngw"
aws ec2 delete-nat-gateway --nat-gateway-id "$ngw"
done

# Delete Elastic IPs
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' ' ')
for eip in $eips; do
echo "Releasing Elastic IP: $eip"
aws ec2 release-address --allocation-id "$eip"
done

echo "All resource Deleted for VPC: $vpc , now delete vpc"

if aws ec2 delete-vpc --vpc-id $vpc; then
Expand Down

0 comments on commit 2a07703

Please sign in to comment.