Skip to content

Instantly share code, notes, and snippets.

@fredbourni
Created May 12, 2017 21:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbourni/f2f8b43163f79f0ced97c63997a00929 to your computer and use it in GitHub Desktop.
Save fredbourni/f2f8b43163f79f0ced97c63997a00929 to your computer and use it in GitHub Desktop.
AWS EC2 List All Resources for Pre-Defined Regions
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