Tagging EC2 Instances using Boto

For a while, I have been creating command line tools provided right with boto which I used to manage AWS. Recently, others have become interested in these tools as well, and I've seen several other contributors adding to these tools to make them even more useful to others. One recent submission by Ales Zoulek added some nice features to my list_instances command, which I use on a regular basis to list out the instances that are currently active for my account in EC2.

Amazon now lets you add Tags to EC2 objects such as Instances and Snapshots. This allows you to actually "Name" your EC2 instance, as well as add some metadata that could be used for AMI initialization, etc. Ales added the ability to list these tags by name within the list_instances command line application:

% list_instances -H ID,Zone,Hostname,T:name
ID             Zone           Hostname                                   T:name                        
-------------------------------------------------------------------------------                        
i-xxa          us-east-1a     xxxx.compute-1.amazonaws.com               www  
i-xxb          us-east-1b     xxxx.compute-1.amazonaws.com               ftp
i-xxc          us-east-1c     xxxx.compute-1.amazonaws.com               webdav 
i-xxd          us-east-1d     xxxx.compute-1.amazonaws.com               


This is great for when you're launching instances, since you can also specify a tag at launch time:


% launch_instance -z us-east-1a -t m1.small -a my-ami -T name:mobile boto.cfg

But what if you've already got a few instances running and you want to label (or re-label) them?

Simple, use the create_tags functionality of boto.ec2:


>>> import boto
>>> ec2 = boto.connect_ec2()
>>> ec2.create_tags(["i-xxd"], {"name": "mobile"})
True

You can actually specify any number of tags to be applied to any number of instances (or snapshots) with this command. The first argument is the list of objects (in this case we just had one), and the second is a dictionary of key-value pairs to tag the objects with.


That's it! when you go back in and check your instances, they'll be tagged properly with the new tags you just set:

% list_instances -H ID,Zone,Hostname,T:name
ID             Zone           Hostname                                   T:name                        
-------------------------------------------------------------------------------                        
i-xxa          us-east-1a     xxxx.compute-1.amazonaws.com               www  
i-xxb          us-east-1b     xxxx.compute-1.amazonaws.com               ftp
i-xxc          us-east-1c     xxxx.compute-1.amazonaws.com               webdav 
i-xxd          us-east-1d     xxxx.compute-1.amazonaws.com               mobile

Comments

reehasmith said…
Need some more information and help regarding that. Ink cartridges
jasimmk said…
Thankz buddy :)

You saved my day.. I was cooking tags with run_instances :(