-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteDefaultVPCs.py
executable file
·71 lines (61 loc) · 2.75 KB
/
deleteDefaultVPCs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
import boto3
import argparse
def deleteSubnets(ec2):
subnets = ec2.describe_subnets()['Subnets']
for subnet in subnets:
ec2.delete_subnet(SubnetId=subnet['SubnetId'])
def deleteSecurityGroups(ec2):
sgs = ec2.describe_security_groups()['SecurityGroups']
for sgp in sgs:
default = sgp['GroupName']
if default != 'default':
sgid = sgp['GroupId']
ec2.delete_security_group(GroupId=sgid)
def deleteNACLS(ec2):
nacls = ec2.describe_network_acls()['NetworkAcls']
for nacl in nacls:
if nacl["IsDefault"] != True:
ec2.delete_network_acl(NetworkAclId=nacl['NetworkAclId'])
def deleteIGWS(ec2, vpcid):
igws = ec2.describe_internet_gateways()['InternetGateways']
for igw in igws:
ec2.detach_internet_gateway(InternetGatewayId=igw['InternetGatewayId'], VpcId=vpcid)
ec2.delete_internet_gateway(InternetGatewayId=igw['InternetGatewayId'])
def deleteVPC(ec2, vpcid):
ec2.delete_vpc(VpcId=vpcid)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--accept', default="n",
help="Auto accept the deletion of the VPC's. Valid values are y or n")
parser.add_argument('-p', '--profile',
help="The aws cli profile to use")
arg = parser.parse_args()
session = boto3.Session(profile_name=arg.profile)
client = session.client('ec2')
regions = client.describe_regions()
for region in regions['Regions']:
ec2 = session.client('ec2',region_name=region["RegionName"])
vpcs = ec2.describe_vpcs()
for vpc in vpcs["Vpcs"]:
if vpc["IsDefault"] == True:
if arg.accept == "y":
print("Deleting VPC dependencies for " + vpc["VpcId"] + " in the region " + region["RegionName"])
deleteSubnets(ec2)
deleteSecurityGroups(ec2)
deleteNACLS(ec2)
deleteIGWS(ec2,vpc["VpcId"])
print("Deleting VPC " + vpc["VpcId"] + " in the region " + region["RegionName"])
deleteVPC(ec2,vpc["VpcId"])
elif arg.accept == "n":
response = input("Would you like to delete " + vpc["VpcId"] + " in the region " + region["RegionName"] + "? " + "Type y or n (default no): ")
if response == "y":
print("Deleting VPC dependencies")
deleteSubnets(ec2)
deleteSecurityGroups(ec2)
deleteNACLS(ec2)
deleteIGWS(ec2,vpc["VpcId"])
print("Deleting VPC")
deleteVPC(ec2,vpc["VpcId"])
if __name__ == '__main__':
main()