EC2 Instances List to CSV


NOTE: This is post that I had already posted here.

Since we use a shared account of AWS to work. It becomes incredibly painful to manage it. So I thought it would be good to have a mechanism write or export the list to of instances to a CSV so we could send a claim mail to see who claims these instances. So I wrote this

#!/usr/bin/env python

from boto.ec2 import EC2Connection

csv_file = open('instances.csv','w+')

def process_instance_list(connection):
    map(build_instance_list,connection.get_all_instances())

def build_instance_list(reservation):
    map(write_instances,reservation.instances)

def write_instances(instance):
    if (instance.virtualizationType == 'hvm'):
        platform = 'Windows'
    else:
        platform = 'Linux'
    csv_file.write("%s,%s,%s,%s,%s,%s\n"%(instance.id,instance.public_dns_name,
                                          instance.state,instance.placement,instance.architecture, platform))
    csv_file.flush()

if __name__=="__main__":
    connection = EC2Connection(aws_access_key_id='XXXXXXX',aws_secret_access_key='XXXXXXXX')
    process_instance_list(connection)
    csv_file.close()

This piece of code is pretty straight and any EC2 noop could understand it. What I do is, get list of instances and layout their details line-wise and comma separated into a csv file. Now this file could be easily imported into excel sheet. I need to get a better mechanism, I know. But, for now this works.

Advertisements

One thought on “EC2 Instances List to CSV

  1. Hey there,
    Thanks for this code, perfect little script to export the list of instances and also get started with boto.
    One small fix would be to change the line 14 as follows:
    if (instance.virtualization_type == ‘hvm’):

    Your original code refers to nonexistent attrib virtualizationType.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s