FIX: correct shell script error
This commit is contained in:
parent
3c5c56737f
commit
6044f17540
@ -1,47 +1,38 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
region=$1
|
region=$1
|
||||||
# get default vpc
|
vpc=$(aws ec2 --region ${region} \
|
||||||
vpc=$(aws ec2 --region ${region} \
|
|
||||||
describe-vpcs --filter Name=isDefault,Values=true \
|
describe-vpcs --filter Name=isDefault,Values=true \
|
||||||
| jq -r .Vpcs[0].VpcId)
|
| jq -r .Vpcs[0].VpcId)
|
||||||
if [ "${vpc}" = "null" ]; then
|
if [ "${vpc}" = "null" ]; then
|
||||||
echo "No default vpc found"
|
echo "No default vpc found"
|
||||||
continue
|
exit 0
|
||||||
fi
|
fi
|
||||||
echo "Found default vpc ${vpc}"
|
|
||||||
|
|
||||||
# get internet gateway
|
igw=$(aws ec2 --region ${region} \
|
||||||
igw=$(aws ec2 --region ${region} \
|
|
||||||
describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} \
|
describe-internet-gateways --filter Name=attachment.vpc-id,Values=${vpc} \
|
||||||
| jq -r .InternetGateways[0].InternetGatewayId)
|
| jq -r .InternetGateways[0].InternetGatewayId)
|
||||||
if [ "${igw}" != "null" ]; then
|
if [ "${igw}" != "null" ]; then
|
||||||
echo "Detaching and deleting internet gateway ${igw}"
|
echo "Detaching and deleting internet gateway ${igw}"
|
||||||
aws ec2 --region ${region} \
|
aws ec2 --region ${region} \
|
||||||
detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
|
detach-internet-gateway --internet-gateway-id ${igw} --vpc-id ${vpc}
|
||||||
aws ec2 --region ${region} \
|
aws ec2 --region ${region} \
|
||||||
delete-internet-gateway --internet-gateway-id ${igw}
|
delete-internet-gateway --internet-gateway-id ${igw}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# get subnets
|
subnets=$(aws ec2 --region ${region} \
|
||||||
subnets=$(aws ec2 --region ${region} \
|
|
||||||
describe-subnets --filters Name=vpc-id,Values=${vpc} \
|
describe-subnets --filters Name=vpc-id,Values=${vpc} \
|
||||||
| jq -r .Subnets[].SubnetId)
|
| jq -r .Subnets[].SubnetId)
|
||||||
if [ "${subnets}" != "null" ]; then
|
if [ "${subnets}" != "null" ]; then
|
||||||
for subnet in ${subnets}; do
|
for subnet in ${subnets}; do
|
||||||
echo "Deleting subnet ${subnet}"
|
echo "Deleting subnet ${subnet}"
|
||||||
aws ec2 --region ${region} \
|
aws ec2 --region ${region} \
|
||||||
delete-subnet --subnet-id ${subnet}
|
delete-subnet --subnet-id ${subnet}
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
|
echo "Deleting vpc ${vpc}"
|
||||||
# - You can't delete the main route table
|
aws ec2 --region ${region} \
|
||||||
# - You can't delete the default network acl
|
|
||||||
# - You can't delete the default security group
|
|
||||||
|
|
||||||
# delete default vpc
|
|
||||||
echo "Deleting vpc ${vpc}"
|
|
||||||
aws ec2 --region ${region} \
|
|
||||||
delete-vpc --vpc-id ${vpc}
|
delete-vpc --vpc-id ${vpc}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user