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 b8e359b commit 4465075
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/awscleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ jobs:
echo "$status" | grep -qvE '^(queued|in_progress)$'
echo "KUMAR"
if [[ "$status" != "null" && ! -z "$status" && $(echo "$status" | grep -qvE '^(queued|in_progress)$'; echo $?) -eq 0 ]]; then
echo "Holodeck Job status is not in running stage , Delete the dependend resources"
echo "Holodeck Job status is not in running stage , Delete the dependent resources"
scripts/awscleanup.sh $vpc
fi
done
Expand Down
84 changes: 39 additions & 45 deletions scripts/checkdependency.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

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
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

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

Expand All @@ -19,67 +23,57 @@ if [ ! -z "$igws" ]; then
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' ' ')
nat_gateways=$(aws ec2 describe-nat-gateways --filter "Name=vpc-id,Values=$vpc" --query "NatGateways[].NatGatewayId" --output text)
if [ ! -z "$nat_gateways" ]; then
echo "Please detach and delete the following nat_gateways:"
echo "Please delete the following NAT Gateways before deleting the VPC:"
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' ' ')
eips=$(aws ec2 describe-addresses --filters "Name=network-interface.vpc-id,Values=$vpc" --query "Addresses[].PublicIp" --output text)
if [ ! -z "$eips" ]; then
echo "Please detach and delete the following eips:"
echo "Please release the following Elastic IPs before deleting the VPC:"
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
eni_ids=$(aws ec2 describe-network-interfaces --filters "Name=vpc-id,Values=$vpc" --query "NetworkInterfaces[].NetworkInterfaceId" --output text)
if [ ! -z "$eni_ids" ]; then
echo "Please delete or detach the following network interfaces before deleting the VPC:"
echo $eni_ids
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
vpc_peering=$(aws ec2 describe-vpc-peering-connections --filters "Name=requester-vpc-info.vpc-id,Values=$vpc" --query "VpcPeeringConnections[].VpcPeeringConnectionId" --output text)
if [ ! -z "$vpc_peering" ]; then
echo "Please delete the following VPC Peering Connections before deleting the VPC:"
echo $vpc_peering
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
vpn_connections=$(aws ec2 describe-vpn-connections --filters "Name=vpc-id,Values=$vpc" --query "VpnConnections[].VpnConnectionId" --output text)
if [ ! -z "$vpn_connections" ]; then
echo "Please delete the following VPN Connections before deleting the VPC:"
echo $vpn_connections
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
route_tables=$(aws ec2 describe-route-tables --filters "Name=vpc-id,Values=$vpc" --query "RouteTables[].RouteTableId" --output text)
if [ ! -z "$route_tables" ]; then
echo "Please delete the following Route Tables before deleting the VPC:"
echo $route_tables
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
nacl_ids=$(aws ec2 describe-network-acls --filters "Name=vpc-id,Values=$vpc" --query "NetworkAcls[].NetworkAclId" --output text)
if [ ! -z "$nacl_ids" ]; then
echo "Please delete the following Network ACLs before deleting the VPC:"
echo $nacl_ids
exit 1
fi

echo "No dependencies found. Proceeding with VPC deletion..."
aws ec2 describe-vpcs --vpc-ids $vpc --query 'Vpcs[0].State'



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

0 comments on commit 4465075

Please sign in to comment.