-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2tagger.py
141 lines (97 loc) · 3.85 KB
/
ec2tagger.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
import boto.ec2.elb
import boto.ec2
import boto3
import argparse
#get command args
parser = argparse.ArgumentParser()
parser.add_argument("region", help="specifies the EC2 region; e.g. 'us-west-1'")
parser.add_argument("keytag", help="specifies the key tag; e.g. 'CostAlloc'")
parser.add_argument("--tagvols", help='tag volumes', action="store_true")
parser.add_argument("--taglbs", help='tag load balancers', action="store_true")
args = parser.parse_args()
key_tag = (args.keytag)
region = (args.region)
vol_func = (args.tagvols)
lb_func = (args.taglbs)
def tag_volumes():
"""Get Key/Value tags from Instances
and apply them to the Instance's Volumes"""
print 'Gathering tag information for Volumes...'
#connect to EC2 and get instances and volumes
ec2_conn = boto.ec2.connect_to_region(region)
reservations = ec2_conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
vol = ec2_conn.get_all_volumes()
inst_vol_dict = {}
for volumes in vol:
filter = {'block-device-mapping.volume-id':volumes.id}
volumesinstance = ec2_conn.get_all_instances(filters=filter)
ids = [z for k in volumesinstance for z in k.instances]
#set values for instances and their volumes
for s in ids:
instance_id = s.id
volume_id = volumes.id
#Create a dict with instanceid/keytag as key/value
for res in reservations:
for inst in res.instances:
if key_tag in inst.tags:
inst_vol_dict [ inst.id ] = inst.tags[key_tag]
#use boto function 'create_tags'
try:
instance_tag = inst_vol_dict[instance_id]
print 'Tagging volumes... %s' % volume_id
ec2_conn.create_tags([volume_id], {key_tag: instance_tag})
except:
pass
def tag_load_balancers():
"""Get Key/Value tags from Instances
and apply them to the Instance's Load Balancers"""
print 'Gathering information for Load Balancers...'
#connect to EC2 and get load balancers
client = boto3.client('elb', region_name = region)
elb = boto.ec2.elb.connect_to_region(region)
lb_conn = elb.get_all_load_balancers()
instance_elb_dict = {}
instance_tag_dict = {}
#create a dict with instanceid/loadbalancer as key/value
for b in lb_conn:
lb = elb.get_all_load_balancers([b.name])[0]
for instance_info in lb.instances:
instance_elb_dict [ instance_info.id ] = b.name
#connect to EC2 and get instances
inst_conn = boto.ec2.connect_to_region(region)
reservations = inst_conn.get_all_instances()
#create a dict with instanceid/tag as key/value
for res in reservations:
for inst in res.instances:
if key_tag in inst.tags:
instance_tag_dict [ inst.id ] = inst.tags[key_tag]
#get the instance id from load balancer metadata
for b in lb_conn:
lb = elb.get_all_load_balancers([b.name])[0]
for instance_info in lb.instances:
inst_id = instance_info.id
#pull matching tags and load balancer info from dicts
try:
inst_tag = instance_tag_dict[inst_id]
inst_elb = instance_elb_dict[inst_id]
print 'Tagging Load Balancer... %s' % inst_elb
response = client.add_tags(
LoadBalancerNames=[
inst_elb,
],
Tags=[
{
'Key': key_tag,
'Value': inst_tag
},
]
)
except:
pass
if __name__ == "__main__":
if vol_func:
tag_volumes()
if lb_func:
tag_load_balancers()