Skip to content

Instantly share code, notes, and snippets.

@rubic
Created January 21, 2012 17:44
Show Gist options
  • Save rubic/1653405 to your computer and use it in GitHub Desktop.
Save rubic/1653405 to your computer and use it in GitHub Desktop.
Magic for working with EC2 instances interactively with IPython
# Magic imports for working interactively (ipython)
# ipython -i ec2connection.py
import re
import boto.ec2
RegionRegex = re.compile("^us-") # all regions starting with 'us-'
class EC2(object):
def __init__(self):
class Instance(object):
pass
self._connection = {}
self.instances = {}
self.i = Instance()
self.regions = dict([(r.name, r) for r in boto.ec2.regions()])
for region in self.regions.values():
if RegionRegex.match(region.name):
self._connection[region.name] = region.connect()
setattr(self, region.name.replace('-', '_'), self._connection[region.name])
self.reload()
def reload(self):
for attr in self.instances:
delattr(self.i, attr)
del self.instance[attr]
for conn in self._connection.values():
for reservation in conn.get_all_instances():
for i in reservation.instances:
setattr(self.i, i.id.replace('i-', ''), i)
key = i.tags.get('Name', i.id)
if key in self.instances:
self.instances[i.id] = i
else:
self.instances[key] = i
setattr(self.i, key, i)
ec2 = EC2()
__builtins__.i = ec2.i
@rubic
Copy link
Author

rubic commented Jan 21, 2012

Here's a sample session of working interactively with EC2 instances in IPython:

$ ipython -i ec2connection.py

In [1]: ec2. <TAB>
ec2.i          ec2.regions    ec2.us_east_1  ec2.us_west_2  
ec2.instances  ec2.reload     ec2.us_west_1  

In [1]: ec2.regions
Out[1]: 
{u'ap-northeast-1': RegionInfo:ap-northeast-1,
 u'ap-southeast-1': RegionInfo:ap-southeast-1,
 u'eu-west-1': RegionInfo:eu-west-1,
 u'sa-east-1': RegionInfo:sa-east-1,
 u'us-east-1': RegionInfo:us-east-1,
 u'us-west-1': RegionInfo:us-west-1,
 u'us-west-2': RegionInfo:us-west-2}

In [2]: ec2.us_east_1
Out[2]: EC2Connection:ec2.us-east-1.amazonaws.com

In [3]: i. <TAB>
i.07b9e56f  i.688c1958  i.bates     i.carriage  i.chat      i.e4d68c86
i.17cc887d  i.84201dec  i.beta      i.collator  i.dc7077bc  i.fbdc9e91
i.59c40c3a  i.a0b7b0c2  i.bouncer   i.c75daaa4  i.df4a6db7  i.itrack

In [4]: i.beta.state
Out[4]: u'running'

In [5]: i.beta.remove_tag?
Definition: i.beta.remove_tag(self, key, value=None)
Docstring:
Remove a tag from this object.  Removing a tag involves a round-trip
to the EC2 service.

:type key: str
:param key: The key or name of the tag being stored.

:type value: str
:param value: An optional value that can be stored with the tag.
              If a value is provided, it must match the value
              currently stored in EC2.  If not, the tag will not
              be removed.  If a value of None is provided, all
              tags with the specified name will be deleted.
              NOTE: There is an important distinction between
              a value of '' and a value of None.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment