-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinventory-aws-to-csv.py
executable file
·396 lines (352 loc) · 14.5 KB
/
inventory-aws-to-csv.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/python3
import boto3
import csv
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--profile", help="specify aws profile to use")
parser.add_argument("--out_file", help="csv file to create")
parser.add_argument("--csv_test", type=bool, default=False, help="generate sample csv file")
# Logical groupings
parser.add_argument("--compute", type=bool, default=False, help="True enables counting compute resources")
parser.add_argument("--network", type=bool, default=False, help="True enables counting network resources")
parser.add_argument("--paas", type=bool, default=False, help="True enables counting paas resources")
parser.add_argument("--security", type=bool, default=False, help="True enables counting security resources")
parser.add_argument("--storage", type=bool, default=False, help="True enables counting storage resources")
# Specific api intensive modules
parser.add_argument("--s3Objects", type=bool, default=False, help="True enables counting s3 objects, default = false")
parser.add_argument("--sgRules", type=bool, default=False, help="True enables counting sg rules, default = false")
parser.add_argument("--naclRules", type=bool, default=False, help="True enables counting nacl rules, default = false")
args = parser.parse_args()
profile = args.profile
# COMPUTE
def aws_compute(regions):
"This kicks off all compute inventory functions"
aws_inventory["ec2_instances"] = ec2_instances(regions)
aws_inventory["ecs_clusters"] = ecs_clusters(regions)
aws_inventory["auto_scaling_groups"] = auto_scaling_groups(regions)
aws_inventory["lambda_functions"] = lambda_functions(regions)
aws_inventory["elastic_loadbalancers"] = elastic_loadbalancers(regions)
aws_inventory["application_loadbalancers"] = application_loadbalancers(regions)
def ec2_instances(regions):
"This prints out count of ec2 instances on an account"
i = 0
for region in regions:
try:
j = sum(1 for _ in boto3.session.Session(profile_name=profile,region_name=region).resource('ec2').instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]))
i += j
print("ec2.instances.{}:".format(region),j)
except:
print("ec2.instances.{}: none")
return i
def ecs_clusters(regions):
"Prints out a count of clusters"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ecs').list_clusters()["clusterArns"])
i += j
print("ecs.clusters.{}:".format(region),j)
except:
print("ecs.clusters.{}: unsupported".format(region))
return i
def auto_scaling_groups(regions):
"Prints out a count of autoscale groups"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('autoscaling').describe_auto_scaling_groups()["AutoScalingGroups"])
i += j
print("asg.groups.{}:".format(region),j)
except:
print("asg.groups.{}: none".format(region))
return i
def lambda_functions(regions):
"Prints count of lambda functions"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('lambda').list_functions()["Functions"])
i += j
print("lambda.functions.{}:".format(region),j)
except:
print("lambda.functions.{}: unsupported".format(region))
return i
def elastic_loadbalancers(regions):
"Prints count of classic elb"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('elb').describe_load_balancers()["LoadBalancerDescriptions"])
i += j
print("elb.{}:".format(region),j)
except:
print("elb.{}: unsupported".format(region))
return i
def application_loadbalancers(regions):
"Prints count of alb"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('elbv2').describe_load_balancers()["LoadBalancers"])
i += j
print("alb.{}:".format(region),j)
except:
print("alb.{}: unsupported".format(region))
return i
# NETWORK
def aws_network(regions):
"This kicks off all network inventory functions"
aws_inventory["vpcs"] = vpcs(regions)
aws_inventory["vpns"] = vpns(regions)
aws_inventory["route_tables"] = route_tables(regions)
aws_inventory["subnets"] = subnets(regions)
def vpcs(regions):
"Prints count of vpcs"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_vpcs()["Vpcs"])
i += j
print("vpc.{}:".format(region),j)
except:
print("vpc.{}: unsupported".format(region))
return i
def vpns(regions):
"Prints count of vpns"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_vpn_connections()["VpnConnections"])
i += j
print("vpn.{}:".format(region),j)
except:
print("vpn.{}: unsupported".format(region))
return i
def route_tables(regions):
"Prints count of route tables"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_route_tables()["RouteTables"])
i += j
print("route_tables.{}:".format(region),j)
except:
print("route_tables.{}: unsupported".format(region))
return i
def subnets(regions):
"Prints count of subnets"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_subnets()["Subnets"])
i += j
print("subnets.{}:".format(region),j)
except:
print("subnets.{}: unsupported".format(region))
return i
# PAAS
def aws_paas(regions):
"This kicks off all paas inventory functions"
aws_inventory["cf_distributions"] = cf_distributions()
aws_inventory["elastic_search"] = elastic_search()
aws_inventory["aurora_clusters"] = aurora_clusters()
aws_inventory["rds_instances"] = rds_instances(regions)
aws_inventory["dynamo_db"] = dynamo_db()
def cf_distributions():
"Prints count of cloudfront distributions"
j = 0
try:
j = len(boto3.session.Session(profile_name=profile).client('cloudfront').list_distributions()["DistributionList"]["Items"])
print("cf_distributions:",j)
except:
print("cf_distributions: unsupported")
return j
def elastic_search():
"Prints count of elastic search clusters"
j = 0
try:
j = len(boto3.session.Session(profile_name=profile).client('es').list_domain_names()["DomainNames"])
print("es.clusters:",j)
except:
print("es.clusters: unsupported")
return j
def aurora_clusters():
"Prints count of aurora clusters"
j = 0
try:
j = len(boto3.session.Session(profile_name=profile).client('rds').describe_db_clusters()["DBClusters"])
print("rds.aurora.clusters:",j)
except:
print("rds.aurora.clusters: unsupported")
return j
def rds_instances(regions):
"Prints count of rds instances"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('rds').describe_db_instances()["DBInstances"])
i += j
print("rds.instances.{}:".format(region),j)
except:
print("rds.instances.{}: unsupported".format(region))
return i
def dynamo_db():
"Prints count of dynamo databases"
j = 0
try:
j = len(boto3.session.Session(profile_name=profile).client('dynamodb').list_tables()["TableNames"])
print("dynamodb.tables:",j)
except:
print("dynamodb.tables: unsupported")
return j
# SECURITY
def aws_security(regions):
"This kicks off all security inventory functions"
aws_inventory["security_groups"] = security_groups(regions)
if args.sgRules:
aws_inventory["security_group_rules"] = security_group_rules(regions)
aws_inventory["nacls"] = nacls(regions)
if args.naclRules:
aws_inventory["nacl_rules"] = nacl_rules(regions)
aws_inventory["wafs"] = wafs(regions)
def security_groups(regions):
"Prints count of security groups"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_security_groups()["SecurityGroups"])
i += j
print("sg.{}:".format(region),j)
except:
print("sg.{}: unsupported".format(region))
return i
def security_group_rules(regions):
"Prints count of security groups"
i = 0
for region in regions:
try:
response = boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_security_groups()
j = 0
for sg in response["SecurityGroups"]:
j += (len(sg["IpPermissions"]) + len(sg["IpPermissionsEgress"]))
i += j
print("sg.rules.{}:".format(region),j)
except:
print("sg.rules.{}: unsupported".format(region))
return i
def nacls(regions):
"Prints count of nacls"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_network_acls()["NetworkAcls"])
i += j
print("nacl.{}:".format(region),j)
except:
print("nacl.{}: unsupported".format(region))
return i
def nacl_rules(regions):
"Prints count of security groups"
i = 0
for region in regions:
try:
response = boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_network_acls()
j = 0
for nacl in response["NetworkAcls"]:
j += len(nacl["Entries"])
i += j
print("nacl.rules.{}:".format(region),j)
except:
print("nacl.rules.{}: unsupported".format(region))
return i
def wafs(regions):
"Prints count of wafs"
i = 0
for region in regions:
try:
j = len(boto3.session.Session(profile_name=profile,region_name=region).client('waf').list_web_acls()["WebACLs"])
i += j
print("waf.{}:".format(region),j)
except:
print("waf.{}: unsupported".format(region))
return i
# STORAGE
def aws_storage(regions):
"This kicks off all storage inventory functions"
aws_inventory["ebs_volumes"] = ebs_volumes(regions)
aws_inventory["efs_filesystems"] = efs_filesystems()
aws_inventory["s3_buckets"] = s3_buckets()
if args.s3Objects:
aws_inventory["s3_objects"] = s3_objects()
def ebs_volumes(regions):
"Prints count of ebs volumes"
i = 0
try:
for region in regions:
ec2volumes = boto3.session.Session(profile_name=profile,region_name=region).client('ec2').describe_volumes().get('Volumes',[])
j = len(sum(
[
[i for i in r['Attachments']]
for r in ec2volumes
], []))
i += j
print("ebs.volumes.{}:".format(region),j)
except:
print("ebs.volumes.{}: none".format(region))
return i
def efs_filesystems():
"This prints count of efs file systems on account"
i = 0
try:
j = len(boto3.session.Session(profile_name=profile).client('efs').describe_file_systems()['FileSystems'])
i += j
print("efs.filesystem.count:",j)
except:
print("efs.filesystem.count: none")
return i
def s3_buckets():
"This prints a count of buckets on an account"
i = 0
try:
j = len(boto3.session.Session(profile_name=profile).client('s3').list_buckets()["Buckets"])
i += j
print("s3.bucket.count:",j)
except:
print("s3.bucket.count: none")
return i
def s3_objects():
"This prints object counts for all buckets of account"
i = 0
for bucket_name in boto3.session.Session(profile_name=profile).client('s3').list_buckets()["Buckets"]:
try:
j = sum(1 for _ in boto3.session.Session(profile_name=profile).resource('s3').Bucket(bucket_name["Name"]).objects.all())
i += j
print("s3.bucket.{}.objects:".format(bucket_name["Name"]),j)
except:
print("s3.bucket.{}.objects: none".format(bucket_name["Name"]))
return i
# If ran directly this will start everything
if __name__ == '__main__':
aws_inventory = dict([])
if args.profile:
regions = [region['RegionName'] for region in boto3.session.Session(profile_name=profile).client('ec2').describe_regions()['Regions']]
# Run components by logical group
if args.compute:
aws_compute(regions)
if args.network:
aws_network(regions)
if args.paas:
aws_paas(regions)
if args.security:
aws_security(regions)
if args.storage:
aws_storage(regions)
if args.csv_test:
aws_inventory = {'elastic_loadbalancers': 0, 'subnets': 34, 'nacls': 12, 'security_groups': 21, 'aurora_clusters': 0, 'rds_instances': 0, 'ec2_instances': 0, 'dynamo_db': 0, 'wafs': 0, 'vpns': 0, 's3_buckets': 17, 'application_loadbalancers': 0, 'auto_scaling_groups': 0, 'vpcs': 12, 'elastic_search': 0, 'lambda_functions': 0, 'ecs_clusters': 0, 'route_tables': 14, 'cf_distributions': 0, 'efs_filesystems': 0, 'ebs_volumes': 0}
if args.out_file:
with open(args.out_file, 'w+') as csvfile:
fieldnames = ['vpcs','vpns','route_tables','subnets','ec2_instances','ecs_clusters','auto_scaling_groups','lambda_functions','elastic_loadbalancers','application_loadbalancers','ebs_volumes','efs_filesystems','s3_buckets','s3_objects','security_groups','security_group_rules','nacls','nacl_rules','wafs','cf_distributions','elastic_search','aurora_clusters','rds_instances','dynamo_db']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(aws_inventory)