diff --git a/scripts/awscleanup.sh b/scripts/awscleanup.sh index dd1596b9..36893939 100755 --- a/scripts/awscleanup.sh +++ b/scripts/awscleanup.sh @@ -18,37 +18,6 @@ for instance in $instances; do aws ec2 terminate-instances --instance-ids "$instance" done -# 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 - echo "Terminating delete-internet-gateway: $igw" - aws ec2 detach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc" - aws ec2 delete-internet-gateway --internet-gateway-id "$igw" -done - -# 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 - echo "Terminating delete-nat-gateway: $ngw" - aws ec2 delete-nat-gateway --nat-gateway-id "$ngw" -done - -# 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 - echo "Terminating eip: $eip" - aws ec2 release-address --allocation-id "$eip" -done - # Detach and Delete Security Groups security_groups=$(aws ec2 describe-security-groups \ --filters Name=vpc-id,Values=$vpc \ @@ -71,6 +40,16 @@ for sg in $security_groups; do aws ec2 delete-security-group --group-id "$sg" done +# Delete Subnets +subnets=$(aws ec2 describe-subnets \ + --filters Name=vpc-id,Values=$vpc \ + --query "Subnets[].SubnetId" \ + --output text | tr -d '\r' | tr '\n' ' ') +for subnet in $subnets; do + echo "Terminating delete-subnet: $subnet" + aws ec2 delete-subnet --subnet-id "$subnet" +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 @@ -98,33 +77,15 @@ for rt in $route_tables; do done done -# Delete Subnets -subnets=$(aws ec2 describe-subnets \ - --filters Name=vpc-id,Values=$vpc \ - --query "Subnets[].SubnetId" \ - --output text | tr -d '\r' | tr '\n' ' ') -for subnet in $subnets; do - echo "Terminating delete-subnet: $subnet" - aws ec2 delete-subnet --subnet-id "$subnet" -done - -# Delete Network Interfaces -eni_ids=$(aws ec2 describe-network-interfaces \ - --filters Name=vpc-id,Values=$vpc \ - --query "NetworkInterfaces[].NetworkInterfaceId" \ - --output text | tr -d '\r' | tr '\n' ' ') -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" \ +# 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 acl in $nw_acls; do - echo "Deleting Network ACL: $acl" - aws ec2 delete-network-acl --network-acl-id $acl +for igw in $internet_gateways; do + echo "Terminating delete-internet-gateway: $igw" + aws ec2 detach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc" + aws ec2 delete-internet-gateway --internet-gateway-id "$igw" done # Delete vpc