Aleksey
Aleksey

Reputation: 93

AWS Python boto3 describe all instances in all regions and output to CSV

I'm starting using boto3 and I wonder how I can get an inventory of all ec2 instances in all regions with custom attributes and put it to CSV file. For the single region it looks simple:

import boto3
import jmespath
import csv

client = boto3.client('ec2')
response = client.describe_instances()

myData = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)

myFile = open('inventory.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(myData)

But I can't figure out how to achieve a similar result for all regions. I've tried something like this:

all_regions = client.describe_regions()

RegionList = []
for r in all_regions['Regions']:
    RegionList.append(r['RegionName'])

for r in RegionList:
    client = boto3.client('ec2', region_name = r)
    response = client.describe_instances()
    myData = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)

myFile = open('inventory.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(myData)

But I'm getting just an empty list.

Upvotes: 3

Views: 12265

Answers (2)

Aleksey
Aleksey

Reputation: 93

This work for me:

import csv
import jmespath
import boto3
import itertools
import configparser
import os

# Get list of profiles
config = configparser.ConfigParser()
path = os.path.join(os.path.expanduser('~'), '.aws/credentials')
config.read(path)
profiles = config.sections()

# Get list of regions
ec2_client = boto3.client('ec2')
regions = [region['RegionName']
            for region in ec2_client.describe_regions()['Regions']]

# Get list of EC2 attributes from all profiles and regions
myData = []
for profile in profiles:
    for region in regions:
        current_session = boto3.Session(profile_name = profile, region_name = region)
        client = current_session.client('ec2')
        response = client.describe_instances()
        output = jmespath.search("Reservations[].Instances[].[NetworkInterfaces[0].OwnerId, InstanceId, InstanceType, \
            State.Name, Placement.AvailabilityZone, PrivateIpAddress, PublicIpAddress, KeyName, [Tags[?Key=='Name'].Value] [0][0]]", response)
        myData.append(output)

# Write myData to CSV file with headers
output = list(itertools.chain(*myData))
with open("ec2-inventory-latest.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerow(['AccountID','InstanceID','Type','State','AZ','PrivateIP','PublicIP','KeyPair','Name'])
    writer.writerows(output)

It iterates through eight accounts and all regions but it takes about five minutes to complete. For comparison, it takes only one minute on bash. Is there a way to increase execution time speed?

Upvotes: 6

r0ck
r0ck

Reputation: 192

try this...

import boto3 

regions= [
    #'ap-east-1',
    'ap-northeast-1',
    'ap-northeast-2',
    'ap-south-1',
    'ap-southeast-1',
    'ap-southeast-2',
    'ca-central-1',
    'eu-central-1',
    'eu-north-1',
    'eu-west-1',
    'eu-west-2',
    'eu-west-3',
    #'me-south-1',
    'sa-east-1',
    'us-east-1',
    'us-east-2',
    'us-west-1',
    'us-west-2'
    ]

 for region_name in regions:
    print(f'region_name: {region_name}')
    ec2= boto3.resource('ec2', region_name=region_name)
    instances= ec2.meta.client.describe_instances()
    for instance in instances['Reservations']:
        print(instance)

Hope it helps
Cheers
r0ck

Upvotes: 2

Related Questions