-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-ec2-region-all.py
42 lines (40 loc) · 1.66 KB
/
aws-ec2-region-all.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
##This Script provides Provides number of EC2 instances for each Region
##Please contact Saurabh Shah for any Enhancements/Modifications
import os,pip
try:
import boto3
except ModuleNotFoundError:
os.system('pip install boto3')
import boto3
print("")
print("This Script provides Provides number of EC2 instances for each Region")
print("")
def activeregion(access_key,secret_key,session_key):
ec2 = boto3.client('ec2',aws_access_key_id=access_key,aws_secret_access_key=secret_key,aws_session_token=session_key)
response = ec2.describe_regions()
ec2count = {}
for region in response['Regions']:
vm = boto3.client('ec2', aws_access_key_id=access_key,aws_secret_access_key=secret_key,aws_session_token=session_key,region_name=region['RegionName'])
eachresponse = vm.describe_instances()
count = 0
for reservation in eachresponse['Reservations']:
for instance in reservation["Instances"]:
count += 1
ec2count.update({region['RegionName']:count})
print("")
print("Below is Mapping of Region : number of EC2 instances")
print("")
for key , value in ec2count.items():
print (key + " : " + str(value))
if max(ec2count.values()) == value:
max_region = key
print("")
print ("Region with maximum EC2 instance is {}".format(max_region))
print("")
def main():
access_key = input("please enter access key id:")
secret_key = input("please enter secret key id:")
session_key = input("please enter session key id:")
activeregion(access_key,secret_key,session_key)
if __name__ == '__main__':
main()