Created
July 8, 2020 05:46
-
-
Save Burekasim/16ba31ecf4d4303adbf97aff026aec5d to your computer and use it in GitHub Desktop.
describe ec2 instance, per aws (cli) profile, per region, print the instance id, instance type and instance state (running/stop/etc) in csv format
This file contains 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 | |
from pprint import pprint | |
profiles = ['default'] | |
regions = [region['RegionName'] for region in boto3.client('ec2', region_name='us-east-1').describe_regions()['Regions']] | |
print('profile,region,instance_id,instance_type,instance_state') | |
for profile in profiles: | |
for region in regions: | |
session = boto3.Session(profile_name=profile) | |
client = session.client('ec2', region_name=region) | |
instances = client.describe_instances() | |
output = instances['Reservations'] | |
if output: | |
for instance_list in output: | |
for instance in instance_list['Instances']: | |
instance_id = instance['InstanceId'] | |
instance_type = instance['InstanceType'] | |
instance_state = instance['State']['Name'] | |
print(f'{profile},{region},{instance_id},{instance_type},{instance_state}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment