NEW: delete-default-vpcs module
This commit is contained in:
parent
a0de86d92e
commit
3c5c56737f
12
modules/networking/delete-default-vpcs/README.md
Normal file
12
modules/networking/delete-default-vpcs/README.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# In the root module, use these to call this module
|
||||||
|
|
||||||
|
```
|
||||||
|
data "aws_regions" "current" {}
|
||||||
|
|
||||||
|
module delete-default-vpc {
|
||||||
|
source = "./module/delete-default-vpc"
|
||||||
|
for_each = data.aws_regions.current.names
|
||||||
|
region-name = each.value
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
47
modules/networking/delete-default-vpcs/exec.sh
Executable file
47
modules/networking/delete-default-vpcs/exec.sh
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
region=$1
|
||||||
|
# get default vpc
|
||||||
|
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"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
echo "Found default vpc ${vpc}"
|
||||||
|
|
||||||
|
# get internet gateway
|
||||||
|
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
|
||||||
|
|
||||||
|
# get subnets
|
||||||
|
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
|
||||||
|
|
||||||
|
# https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-vpc.html
|
||||||
|
# - You can't delete the main route table
|
||||||
|
# - 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}
|
||||||
|
|
5
modules/networking/delete-default-vpcs/main.tf
Normal file
5
modules/networking/delete-default-vpcs/main.tf
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
resource "null_resource" "shell" {
|
||||||
|
provisioner "local-exec" {
|
||||||
|
command = "/bin/bash -c '${path.module}/exec.sh ${var.region-name}'"
|
||||||
|
}
|
||||||
|
}
|
1
modules/networking/delete-default-vpcs/variables.tf
Normal file
1
modules/networking/delete-default-vpcs/variables.tf
Normal file
@ -0,0 +1 @@
|
|||||||
|
variable region-name {}
|
Loading…
Reference in New Issue
Block a user