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
- Install pip
- Install boto
sudo pip 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
Comments
It took me some time to notice the 'break' statement :-), but thanks for the code! very useful.
Dirk: sorry, I added a comment to better highlight the break statement. :) Glad it was useful for you!
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().
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.
thank you, just what I was looking for!
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!
Dirk: thanks for the update! yeah tags are a great feature. we are making use of them at work.
Nice one, thanks.
This doesn't work anymore or at least it became incomplete, i have a single instance in europe west 1 region, but this call above gives me an empty list, probably you have to specify the zone also.
your posts are always a delight (and relief!) in reading-- thanks!
Is it possible to use get_all_instances as a model in Django? If so, how would you do that?
How to find total number of reserved instances under particular tag?
Eg. We have two tags group and name. Then I want to show total number of reserved instances of particular type (Eg. i2.xlarge) under group tag. How to do this, I did not find this in AWS console also ?
One idea that i have is to find all ec2 instances by get_all_instances()
and all reserved instances by get_all_reserved_instances(), But i didn’t find any strong matching criteria for find reserved instances by matching these two lists.
Any other way to find reserved instance under particular tag ?
disqus:2440335212
Hi, I want to check whether particular instance is running or not through python script. Can you help me. I completed almost, but i'm getting erro. Can anyone help me?
disqus:3118306419
