diff --git a/scripts/awscleanup.sh b/scripts/awscleanup.sh index aa70ba0b..57b51737 100755 --- a/scripts/awscleanup.sh +++ b/scripts/awscleanup.sh @@ -109,6 +109,15 @@ 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) +for acl in $nw_acls; do + echo "Deleting Network ACL: $acl" + aws ec2 delete-network-acl --network-acl-id $acl +done + +scripts/checkdependency.sh $vpc + # Delete vpc # try 3 times with 30 seconds interval attempts=0 diff --git a/scripts/checkdependency.sh b/scripts/checkdependency.sh new file mode 100755 index 00000000..a595057c --- /dev/null +++ b/scripts/checkdependency.sh @@ -0,0 +1,85 @@ +#!/bin/bash + +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 + exit 1 +fi + +igws=$(aws ec2 describe-internet-gateways --filters "Name=attachment.vpc-id,Values=$vpc" --query "InternetGateways[].InternetGatewayId" --output text) +if [ ! -z "$igws" ]; then + echo "Please detach and delete the following Internet Gateways before deleting the VPC:" + echo $igws + 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' ' ') +if [ ! -z "$nat_gateways" ]; then + echo "Please detach and delete the following nat_gateways:" + 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' ' ') +if [ ! -z "$eips" ]; then + echo "Please detach and delete the following eips:" + 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 + 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 + 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 + 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 + 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 + exit 1 +fi + +echo "No dependencies found. Proceeding with VPC deletion..." +