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 19, 2024
1 parent f32956d commit 1704b44
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions scripts/awscleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,31 @@ internet_gateways=$(aws ec2 describe-internet-gateways \
--query "InternetGateways[].InternetGatewayId" \
--output text | tr -d '\r' | tr '\n' ' ')
for igw in $internet_gateways; do
echo "Terminating delete-internet-gateway: $igw"
aws ec2 detach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc"
aws ec2 delete-internet-gateway --internet-gateway-id "$igw"
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 "Terminating delete-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 "Terminating eip: $eip"
aws ec2 release-address --allocation-id "$eip"
done

# Detach and Delete Security Groups
security_groups=$(aws ec2 describe-security-groups \
--filters Name=vpc-id,Values=$vpc \
Expand All @@ -39,12 +60,14 @@ for sg in $security_groups; do
--query "NetworkInterfaces[].NetworkInterfaceId" \
--output text | tr -d '\r' | tr '\n' ' ')
for eni in $enis; do
echo "Terminating delete-security-group: $eni"
aws ec2 modify-network-interface-attribute \
--network-interface-id "$eni" \
--groups "$(aws ec2 describe-security-groups \
--query 'SecurityGroups[?GroupName==`default`].GroupId' \
--output text)"
done
echo "Terminating delete-security-group: $sg"
aws ec2 delete-security-group --group-id "$sg"
done

Expand All @@ -70,6 +93,7 @@ for rt in $route_tables; do
else
aws ec2 replace-route-table-association --association-id $assoc_id --route-table-id $first_rt
aws ec2 delete-route-table --route-table-id "$rt"
echo "Terminating delete-route-table: $rt"
fi
done
done
Expand All @@ -80,9 +104,29 @@ subnets=$(aws ec2 describe-subnets \
--query "Subnets[].SubnetId" \
--output text | tr -d '\r' | tr '\n' ' ')
for subnet in $subnets; do
echo "Terminating delete-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 \
--query "NetworkInterfaces[].NetworkInterfaceId" \
--output text | tr -d '\r' | tr '\n' ' ')
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 | tr -d '\r' | tr '\n' ' ')
for acl in $nw_acls; do
echo "Deleting Network ACL: $acl"
aws ec2 delete-network-acl --network-acl-id $acl
done

# Delete vpc
# try 3 times with 30 seconds interval
attempts=0
Expand Down

0 comments on commit 1704b44

Please sign in to comment.