/
ec2-list-all-tags.py
executable file
·52 lines (41 loc) · 1.31 KB
/
ec2-list-all-tags.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
#!/usr/bin/env python
"""
Using boto3, list all distinct tag names on all EC2 instances in all regions.
If you have ideas for improvements, or want the latest version, it's at:
<https://github.com/jantman/misc-scripts/blob/master/ec2-list-all-tags.py>
Copyright 2016 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
CHANGELOG:
2016-07-21 Jason Antman <jason@jasonantman.com>:
- initial version of script
"""
from boto3 import resource, client
def get_region_names():
conn = client('ec2')
res = conn.describe_regions()
regions = []
for r in res['Regions']:
regions.append(r['RegionName'])
return regions
def tags_for_region(region_name):
tags = set()
res = resource('ec2', region_name=region_name)
count = 0
for i in res.instances.all():
count += 1
if i.tags is None:
continue
for t in i.tags:
tags.add(t['Key'])
print('Examined %d instances in %s'% (count, region_name))
return tags
def main():
tags = set()
regions = get_region_names()
for r in regions:
tags.update(tags_for_region(r))
print('Found %d distinct tag names:' % len(tags))
for t in sorted(tags):
print(t)
if __name__ == "__main__":
main()