Created
May 12, 2017 21:43
-
-
Save fredbourni/f2f8b43163f79f0ced97c63997a00929 to your computer and use it in GitHub Desktop.
AWS EC2 List All Resources for Pre-Defined Regions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
regions = frozenset([ | |
'us-east-1', | |
'us-east-2', | |
'us-west-1', | |
'us-west-2', | |
'eu-central-1', | |
'eu-west-1', | |
'eu-west-2', | |
'ca-central-1', | |
'ap-south-1', | |
'ap-southeast-1', | |
'ap-southeast-2', | |
'ap-northeast-1', | |
'ap-northeast-2', | |
'sa-east-1', | |
]) | |
def get_tag(name, tags): | |
for t in tags: | |
if t['Key'] == name: | |
return t['Value'] | |
return 'NIL' | |
session = boto3.Session(profile_name='default') | |
for r in regions: | |
ec2 = session.client('ec2', region_name=r) | |
response = ec2.describe_instances() | |
try: | |
instances = response['Reservations'] | |
except: | |
instances = [] | |
for i in instances: | |
vm = i['Instances'][0] | |
print( | |
','.join([ | |
r, | |
vm['InstanceId'], | |
vm['InstanceType'], | |
vm['ImageId'], | |
vm.get('KeyName', 'NIL'), | |
get_tag('Name', vm['Tags']), | |
]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment