From 4465075185aa00b660e8887a781e3c7c5559e6c6 Mon Sep 17 00:00:00 2001 From: shiva kumar Date: Wed, 18 Dec 2024 19:47:28 +0530 Subject: [PATCH] unused aws instace and vpcs cleanup Signed-off-by: shiva kumar --- .github/workflows/awscleanup.yaml | 2 +- scripts/checkdependency.sh | 84 ++++++++++++++----------------- 2 files changed, 40 insertions(+), 46 deletions(-) diff --git a/.github/workflows/awscleanup.yaml b/.github/workflows/awscleanup.yaml index 4804b3ad..ff1e32e1 100644 --- a/.github/workflows/awscleanup.yaml +++ b/.github/workflows/awscleanup.yaml @@ -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 diff --git a/scripts/checkdependency.sh b/scripts/checkdependency.sh index a595057c..2707a8be 100755 --- a/scripts/checkdependency.sh +++ b/scripts/checkdependency.sh @@ -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 @@ -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..."