-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: shiva kumar <[email protected]>
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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..." | ||
|