-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathaws_region_stats.py
executable file
·104 lines (88 loc) · 3.1 KB
/
aws_region_stats.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
#!/usr/bin/env python
"""
Python script to print a table with some statistics from each AWS region. Stats
include number of RDS instances, EC2 instances, volumes, snapshots, VPCs, and
AMIs, ECS clusters, ELBs and ASGs.
Should work with python 2.7-3.x. Requires ``boto3``from pypi.
The latest version of this script can be found at:
https://github.com/jantman/misc-scripts/blob/master/aws_region_stats.py
Copyright 2018 Jason Antman <[email protected]> <http://www.jasonantman.com>
Free for any use provided that patches are submitted back to me.
CHANGELOG:
2018-09-07 Jason Antman <[email protected]>:
- initial version of script
"""
import sys
try:
import boto3
except ImportError:
sys.stderr.write("ERROR: you must 'pip install boto3'.\n")
raise SystemExit(1)
try:
from terminaltables import AsciiTable
except ImportError:
sys.stderr.write("ERROR: you must 'pip install terminaltables'.\n")
raise SystemExit(1)
RESULT_KEYS = [
'AMIs',
'ASGs',
'ECS Clusters',
'ELB',
'ELBv2',
'Instances',
'RDS Inst',
'Snapshots',
'VPCs',
'Volumes'
]
def get_region_names():
ec2 = boto3.client('ec2', region_name='us-east-1')
return sorted([x['RegionName'] for x in ec2.describe_regions()['Regions']])
def get_account_id():
client = boto3.client('sts')
cid = client.get_caller_identity()
return cid['Account']
def do_region(rname, acct_id):
print('Checking region: %s' % rname)
res = {x: 0 for x in RESULT_KEYS}
# RDS
rds = boto3.client('rds', region_name=rname)
for r in rds.get_paginator('describe_db_instances').paginate():
res['RDS Inst'] += len(r['DBInstances'])
# ELBv2
elbv2 = boto3.client('elbv2', region_name=rname)
for r in elbv2.get_paginator('describe_load_balancers').paginate():
res['ELBv2'] += len(r['LoadBalancers'])
# ELB
elb = boto3.client('elb', region_name=rname)
for r in elb.get_paginator('describe_load_balancers').paginate():
res['ELB'] += len(r['LoadBalancerDescriptions'])
# ECS
ecs = boto3.client('ecs', region_name=rname)
for r in ecs.get_paginator('list_clusters').paginate():
res['ECS Clusters'] += len(r['clusterArns'])
# EC2
ec2 = boto3.resource('ec2', region_name=rname)
res['VPCs'] = len(list(ec2.vpcs.all()))
res['Volumes'] = len(list(ec2.volumes.all()))
res['Snapshots'] = len(list(ec2.snapshots.filter(OwnerIds=[acct_id])))
res['Instances'] = len(list(ec2.instances.all()))
res['AMIs'] = len(list(ec2.images.filter(Owners=['self'])))
# AutoScaling
autoscaling = boto3.client('autoscaling', region_name=rname)
for r in autoscaling.get_paginator('describe_auto_scaling_groups').paginate():
res['ASGs'] += len(r['AutoScalingGroups'])
return res
headers = [k for k in RESULT_KEYS]
headers.insert(0, 'REGION')
tdata = [headers]
acct_id = get_account_id()
print('Found Account ID as: %s' % acct_id)
for rname in get_region_names():
res = do_region(rname, acct_id)
tmp = [rname]
for k in RESULT_KEYS:
tmp.append(res[k])
tdata.append(tmp)
table = AsciiTable(tdata)
print(table.table)