-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroynetwork.py
73 lines (66 loc) · 2.16 KB
/
destroynetwork.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
72
73
import boto3, sys, time, yaml
import conf
import jaws
ec2=boto3.client('ec2')
with open("network.yml", 'r') as stream:
try:
network=yaml.load(stream)
except yaml.YAMLError as exc:
print(exc)
if network['igw']==True:
try:
print("Detaching IGW.")
#get IGW ID
igwid=ec2.describe_internet_gateways(
Filters=[{'Name': 'attachment.vpc-id', 'Values': [conf.vpcid]}]
)['InternetGateways'][0]['InternetGatewayId']
#detatch from VPC
response = ec2.detach_internet_gateway(
InternetGatewayId=igwid,
VpcId=conf.vpcid
)
#wait for detach before delete
for i in range(1,100,1):
time.sleep(5)
response=ec2.describe_internet_gateways(
Filters=[{'Name': 'attachment.vpc-id', 'Values': [conf.vpcid]}]
)
igws=len(response['InternetGateways'])
print("Found %i attached IGWs" % igws)
if len(response['InternetGateways']) == 0:
print("Successfully detached IGW from VPC")
break
if i==100:
print("IGW never detached from VPC. Moving on anyway.")
response = ec2.delete_internet_gateway(
InternetGatewayId=igwid
)
#now delete IGW and confirm.
for i in range(1,100,1):
time.sleep(5)
response=ec2.describe_internet_gateways(
Filters=[{'Name': 'internet-gateway-id', 'Values': [igwid]}]
)
if len(response['InternetGateways']) == 0:
print("Successfully deleted IGW")
break
if i==100:
print("IGW never detached from VPC. Moving on anyway.")
except Exception as e:
print("Failed deleting igw.")
print(e)
#Delete subnets
# First get list of subnets in this VPC
response = ec2.describe_subnets(
Filters=[
{
'Name': 'vpc-id',
'Values': [
conf.vpcid,
]
},
]
)
for subnet in response['Subnets']:
print("Deleting Subnet: %s" % subnet['SubnetId'])
jaws.delete_subnet(subnet['SubnetId'])