Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

How to list attributes of an EC2 instance with Python and boto

Here's how to find out information about your Amazon EC2 instances using the Python boto library.

Install boto

Example

from pprint import pprint
from boto import ec2

AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

ec2conn = ec2.connection.EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
reservations = ec2conn.get_all_instances()
instances = [i for r in reservations for i in r.instances]
for i in instances:
    pprint(i.__dict__)
    break # remove this to list all instances

Results:

{'_in_monitoring_element': False,
 'ami_launch_index': u'0',
 'architecture': u'x86_64',
 'block_device_mapping': {},
 'connection': EC2Connection:ec2.amazonaws.com,
 'dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'id': u'i-xxxxxxxx',
 'image_id': u'ami-xxxxxxxx',
 'instanceState': u'\n                    ',
 'instance_class': None,
 'instance_type': u'm1.large',
 'ip_address': u'xxx.xxx.xxx.xxx',
 'item': u'\n                ',
 'kernel': None,
 'key_name': u'FARM-xxxx',
 'launch_time': u'2009-10-27T17:10:22.000Z',
 'monitored': False,
 'monitoring': u'\n                    ',
 'persistent': False,
 'placement': u'us-east-1d',
 'previous_state': None,
 'private_dns_name': u'ip-10-xxx-xxx-xxx.ec2.internal',
 'private_ip_address': u'10.xxx.xxx.xxx',
 'product_codes': [],
 'public_dns_name': u'ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com',
 'ramdisk': None,
 'reason': '',
 'region': RegionInfo:us-east-1,
 'requester_id': None,
 'rootDeviceType': u'instance-store',
 'root_device_name': None,
 'shutdown_state': None,
 'spot_instance_request_id': None,
 'state': u'running',
 'state_code': 16,
 'subnet_id': None,
 'vpc_id': None}

For more information

7 Comments — feed icon Comments feed for this post


#1 Dirk Krause commented on 2010-04-13:

It took me some time to notice the 'break' statement :-), but thanks for the code! very useful.


#2 Eliot commented on 2010-04-13:

Dirk: sorry, I added a comment to better highlight the break statement. :) Glad it was useful for you!


#3 Mayank commented on 2010-05-07:

Thanks for the example. I was struggling for past 24 hrs to figure out how to get instance details from Reservation object returned by get_all_instances().


#4 Eliot commented on 2010-05-07:

Mayank: yeah this wasn't very clear to me either. I think I remember looking at the source code in order to figure it out. Glad this was helpful.


#5 shirlei commented on 2010-06-09:

thank you, just what I was looking for!


#6 Dirk Krause commented on 2010-11-13:

just for info: boto now support the EC2 tags, i.e. you can name your instances. The result looks like this these days:

...
'state': u'running',
'state_code': 16,
'state_reason': None,
'subnet_id': None,
'tags': {u'Name': u'opensimxyz'},
'virtualizationType': u'paravirtual',
'vpc_id': None}

which is great!


#7 Eliot commented on 2010-11-16:

Dirk: thanks for the update! yeah tags are a great feature. we are making use of them at work.

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Linode