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 97b0978 commit da298b9
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions scripts/awscleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,29 @@ for sg in $security_groups; do
aws ec2 delete-security-group --group-id "$sg"
done

# Delete Route Tables , do not delete Main route table
# Delete Route Tables
# 1. Make first rt as Main , as we cannot delete vpcs attached with main
# 2. replace all rt with first rt
# 3.delete rt
first_rt=""
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' ' ')
for rt in $route_tables; do
associations=$(aws ec2 describe-route-tables \
--route-table-ids "$rt" \
--query "RouteTables[0].Associations[].RouteTableAssociationId" \
--output text | tr -d '\r' | tr '\n' ' ')
for assoc in $associations; do
aws ec2 disassociate-route-table --association-id "$assoc"
done
aws ec2 delete-route-table --route-table-id "$rt"
if [ -z "$first_rt" ]; then
aws ec2 replace-route-table-association --association-id $(aws ec2 describe-route-tables --route-table-id $rt --query "RouteTables[].Associations[].RouteTableAssociationId" --output text) --route-table-id $rt
first_rt=$rt
else
associations=$(aws ec2 describe-route-tables \
--route-table-ids "$rt" \
--query "RouteTables[].RouteTableId" \
--output text | tr -d '\r' | tr '\n' ' ')
for assoc_id in $associations; do
aws ec2 replace-route-table-association --association-id $assoc_id --route-table-id $first_rt
done
aws ec2 delete-route-table --route-table-id "$rt"
fi
done

# Delete Subnets
Expand Down

0 comments on commit da298b9

Please sign in to comment.