2022-08-05 14:56:21 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
region=$1
|
2022-08-05 15:08:37 +08:00
|
|
|
vpc=$(aws ec2 --region ${region} \
|
|
|
|
describe-vpcs --filter Name=isDefault,Values=true \
|
|
|
|
| jq -r .Vpcs[0].VpcId)
|
|
|
|
if [ "${vpc}" = "null" ]; then
|
|
|
|
echo "No default vpc found"
|
|
|
|
exit 0
|
|
|
|
fi
|
2022-08-05 14:56:21 +08:00
|
|
|
|
2022-08-05 15:08:37 +08:00
|
|
|
igw=$(aws ec2 --region ${region} \
|
|
|
|
describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} \
|
|
|
|
| jq -r .InternetGateways[0].InternetGatewayId)
|
|
|
|
if [ "${igw}" != "null" ]; then
|
|
|
|
echo "Detaching and deleting internet gateway ${igw}"
|
|
|
|
aws ec2 --region ${region} \
|
|
|
|
detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
|
|
|
|
aws ec2 --region ${region} \
|
|
|
|
delete-internet-gateway --internet-gateway-id ${igw}
|
|
|
|
fi
|
2022-08-05 14:56:21 +08:00
|
|
|
|
2022-08-05 15:08:37 +08:00
|
|
|
subnets=$(aws ec2 --region ${region} \
|
|
|
|
describe-subnets --filters Name=vpc-id,Values=${vpc} \
|
|
|
|
| jq -r .Subnets[].SubnetId)
|
|
|
|
if [ "${subnets}" != "null" ]; then
|
|
|
|
for subnet in ${subnets}; do
|
|
|
|
echo "Deleting subnet ${subnet}"
|
|
|
|
aws ec2 --region ${region} \
|
|
|
|
delete-subnet --subnet-id ${subnet}
|
|
|
|
done
|
|
|
|
fi
|
2022-08-05 14:56:21 +08:00
|
|
|
|
2022-08-05 15:08:37 +08:00
|
|
|
echo "Deleting vpc ${vpc}"
|
|
|
|
aws ec2 --region ${region} \
|
|
|
|
delete-vpc --vpc-id ${vpc}
|
2022-08-05 14:56:21 +08:00
|
|
|
|
|
|
|
|