diff --git a/scripts/awscleanup.sh b/scripts/awscleanup.sh index 9c411ca4..b2dbf018 100755 --- a/scripts/awscleanup.sh +++ b/scripts/awscleanup.sh @@ -18,34 +18,14 @@ for instance in $instances; do aws ec2 terminate-instances --instance-ids "$instance" done -# SHIVA -# 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 - aws ec2 release-address --allocation-id "$eip" -done - -# SHIVA -# 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 - aws ec2 delete-nat-gateway --nat-gateway-id "$ngw" -done - -# SHIVA -# Delete Network Interfaces -eni_ids=$(aws ec2 describe-network-interfaces \ - --filters Name=vpc-id,Values=$vpc \ - --query "NetworkInterfaces[].NetworkInterfaceId" \ +# Delete Internet Gateway +internet_gateways=$(aws ec2 describe-internet-gateways \ + --filters Name=attachment.vpc-id,Values=$vpc \ + --query "InternetGateways[].InternetGatewayId" \ --output text | tr -d '\r' | tr '\n' ' ') -for eni in $eni_ids; do - aws ec2 delete-network-interface --network-interface-id "$eni" +for igw in $internet_gateways; do + aws ec2 detach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc" + aws ec2 delete-internet-gateway --internet-gateway-id "$igw" done # Detach and Delete Security Groups @@ -68,17 +48,6 @@ for sg in $security_groups; do aws ec2 delete-security-group --group-id "$sg" done -# SHIVA -# 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 Route Tables # 1. Make first rt as Main , as we cannot delete vpcs attached with main # 2. replace all rt with first rt @@ -114,16 +83,6 @@ for subnet in $subnets; do aws ec2 delete-subnet --subnet-id "$subnet" done -# Detach and Delete Internet Gateway -internet_gateways=$(aws ec2 describe-internet-gateways \ - --filters Name=attachment.vpc-id,Values=$vpc \ - --query "InternetGateways[].InternetGatewayId" \ - --output text | tr -d '\r' | tr '\n' ' ') -for igw in $internet_gateways; do - aws ec2 detach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc" - aws ec2 delete-internet-gateway --internet-gateway-id "$igw" -done - # Delete vpc # try 3 times with 30 seconds interval attempts=0