-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_envquery.py
68 lines (57 loc) · 3.67 KB
/
aws_envquery.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
#!/usr/bin/env python
import boto
import boto.ec2
import string
import os
import sys
import re
import csv
import json
import ConfigParser
import time
home = os.path.expanduser("~")
file_path = "%s/.aws/profile" % home
cp = ConfigParser.ConfigParser()
cp.read(file_path)
for section in cp.sections():
for name, value in cp.items(section):
if name == 'aws_access_key_id':
os.environ['aws_access_key_id'] = value
A_KEY = os.environ['aws_access_key_id']
elif name == 'aws_secret_access_key':
os.environ['aws_secret_access_key'] = value
S_KEY = os.environ['aws_secret_access_key']
conn = boto.ec2.EC2Connection(A_KEY, S_KEY)
region = conn.get_all_regions()
for acct_id in conn.get_all_security_groups(groupnames='default'):
aws_account_id = acct_id.owner_id
print aws_account_id
file_name = 'aws_accounts.csv'
with open(file_name, 'a') as fn:
writer = csv.writer(fn, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for name in region:
conn = boto.ec2
ec2_region_connect = conn.connect_to_region(name.name, aws_access_key_id=A_KEY, aws_secret_access_key=S_KEY)
for instance_name in ec2_region_connect.get_all_instances():
for instance in instance_name.instances:
if instance.tags.has_key('xxxx') and instance.tags.has_key('xxxx'):
data = ([aws_account_id, instance.region.name, instance.id, instance.tags['xxxx'], instance.tags['xxxx'], instance.state, instance.launch_time])
writer.writerow(data)
elif instance.tags.has_key('xxxx') and instance.tags.has_key('xxxx'):
data = ([aws_account_id, instance.region.name, instance.id, instance.tags['xxxx'], instance.tags['xxxx'], instance.state, instance.launch_time])
writer.writerow(data)
elif instance.tags.has_key('xxxx') and not instance.tags.has_key('xxxx):
data = ([aws_account_id, instance.region.name, instance.id, instance.tags['xxxx'], "missing xxxx" , instance.state, instance.launch_time])
writer.writerow(data)
elif instance.tags.has_key('xxxx') and not instance.tags.has_key('xxxx'):
data =([aws_account_id, instance.region.name, instance.id, instance.tags['xxxx'], 'missing xxxx', instance.state, instance.launch_time])
writer.writerow(data)
elif not instance.tags.has_key('xxxx') and instance.tags.has_key('xxxx'):
data = ([aws_account_id, instance.region.name, instance.id, 'missing xxxx', instance.tags['xxxx'], instance.state, instance.launch_time])
writer.writerow(data)
elif not instance.tags.has_key('xxxx') and instance.tags.has_key('xxxx'):
data = ([aws_account_id, instance.region.name, instance.id, 'missing xxxx', instance.tags['xxxx'], instance.state, instance.launch_time])
writer.writerow(data)
else:
data =([aws_account_id, instance.region.name, instance.id, 'missing xxxx', 'missing xxxx', instance.state, instance.launch_time])
writer.writerow(data)