-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillByTag.py
More file actions
87 lines (78 loc) · 2.2 KB
/
Copy pathkillByTag.py
File metadata and controls
87 lines (78 loc) · 2.2 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import sys
import boto3
import urllib3
# Instances tagged with BOTH of these tags and values will be destroyed
target_tags = {
"dispensible": "1",
"Test": "Teeest"
}
groupTags = False
def main():
global groupTags
access_key = ""
secret_key = ""
account_num = ""
region = ""
try:
# creates boto3 session
session = boto3.session.Session(
aws_access_key_id = access_key,
aws_secret_access_key = secret_key,
region_name = region,
)
except Exception as e:
print("Error creating AWS session: "+str(e))
sys.exit(3)
client = session.client(
"ec2",
verify=False
)
filter = buildFilter()
if(groupTags):
killThese = filterByTag(client,filter)
else:
killThese = segmentedFilterByTag(client, filter)
ec2 = session.resource(
"ec2",
verify=False
)
kill(ec2, killThese)
def buildFilter():
global target_tags
filter = []
for key, value in target_tags.items():
filter.append({'Name': 'tag:'+key, 'Values': [value]})
print(filter)
return filter
def filterByTag(client,filter):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
nukeThese = []
all = client.describe_instances(Filters=filter)
for res in all.get("Reservations"):
for inst in res.get("Instances"):
nukeThese.append(inst.get("InstanceId"))
return nukeThese
def segmentedFilterByTag(client,filter):
killThese = []
for each in filter:
killThese.extend(filterByTag(client,[each]))
killThese = list(dict.fromkeys(killThese))
print(killThese)
return killThese
def kill(ec2, instances):
print("Attempting killing all of these:")
print(instances)
try:
ec2.instances.filter(InstanceIds = instances).terminate()
except Exception as e:
print("Yikes, terminate function failed: "+e)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted \_[*.*]_/\n')
try:
sys.exit(0)
except SystemExit:
os._exit(0)