Re: [boto-users] help with get_all_instances using filters with custom tags

5,684 views
Skip to first unread message

Mitchell Garnaat

unread,
Jul 3, 2012, 8:49:22 AM7/3/12
to boto-...@googlegroups.com
Hi -

For custom tags with an empty value, you have to use this:

reservations = ec2.get_all_instances(filters={'tag-key': 'tvr'})

rather than using the custom tag as the key in the dictionary.  This is described in the EC2 docs:


but it's not very intuitive.

Mitch

On Tue, Jul 3, 2012 at 12:05 AM, Todd Rovito <rovi...@gmail.com> wrote:
Greetings,
    I have been using AWS for a few years but have not gotten into automation much, everything so far I have doing by hand.  Being a python guy I thought I would try boto so I picked up a book Python and AWS Cookbook and started working some of the recipes.  On the receipt "Keeping Track of Instances with Tags" I am trying to find my instance which has a tag of "tvr" with empty value, so I use this code:
import boto

ec2 = boto.connect_ec2()
reservations = ec2.get_all_instances(filters={'tvr' : None}) # doesn't work
new_instance = reservations[0].instances[0]

The code always fails with the following error:
python ec2_openCmdShellForInstanceWithSpecificTags.py 
Traceback (most recent call last):
  File "ec2_openCmdShellForInstanceWithSpecificTags.py", line 6, in <module>
    reservations = ec2.get_all_instances(filters={'tvr' : None}) # doesn't work
  File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/boto-2.5.2-py2.7.egg/boto/ec2/connection.py", line 478, in get_all_instances
    [('item', Reservation)], verb='POST')
  File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site-packages/boto-2.5.2-py2.7.egg/boto/connection.py", line 980, in get_list
    raise self.ResponseError(response.status, response.reason, body)
boto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidParameterValue</Code><Message>The filter 'tvr' is invalid</Message></Error></Errors><RequestID>21611511-97ad-44b7-874b-da7deda41ac1</RequestID></Response>

With the built in Amazon tags (instance-id) it works great like if I use this code:
import boto

ec2 = boto.connect_ec2()
reservations = ec2.get_all_instances(filters={'instance-id' : 'i-50599f28'}) # works
new_instance = reservations[0].instances[0]


Any ideas?  I may be doing something stupid.  Thanks for the help.

--
You received this message because you are subscribed to the Google Groups "boto-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/boto-users/-/51lV_1MzmogJ.
To post to this group, send email to boto-...@googlegroups.com.
To unsubscribe from this group, send email to boto-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/boto-users?hl=en.

Todd Rovito

unread,
Jul 3, 2012, 10:52:41 AM7/3/12
to boto-...@googlegroups.com
That works great thanks for the quick response and excellent solution!

Amit Mund

unread,
Mar 26, 2014, 2:02:17 AM3/26/14
to boto-...@googlegroups.com, mi...@garnaat.com
Hi,

I am working having automated monitoring system of all our aws instance. As of now I can get the hosts details, and can able to put those hosts into monitoring automatically, but in this way, I can monitor only the generic thinks, that need to be monitor for all the hosts. Example: Cpu utilization.

But, there are few of the other param that I have to monitor, example: We are not running database on all the servers so, I can automate that check the "database status" for all the system. To over come that one, I have created another custom tags groups, having value like database

so, I need help in: How do I get only values where tags.key='groups'

Following is my script:

import boto.ec2

conn=boto.ec2.connect_to_region('us-east-1')

#reservations = conn.get_all_instances(filters={'tag-key': 'groups'})
reservations = conn.get_all_instances()

for res in reservations:
    for inst in res.instances:
            print ("define host{")
            print "%s \t %s " % ("use","generic-host")
            print "%s  %s " % ("host_name", inst.tags['Name'])
            print "%s \t %s " % ("alias", inst.tags['Name'])
            print "%s  %s " % ("address", inst.public_dns_name)
            print "%s    %s " % ("hostgroups", inst.instance_type)
            print (inst.tags)
            # Output example: {u'Name': u'DEV-JobTracker', u'groups': u'database'}  # <--- over here I can able to get all, but if I just want to get the value of the custom value of "group", then how I can get it?
            print (inst.key_name)     # <-- over here I am getting the same value as inst.tags['Name']
            print ("}\n")

so, can I get only the values of the custom tags?

Thanks,
Amit
Reply all
Reply to author
Forward
0 new messages