alonsovb alonsovb - 2 months ago 28
Python Question

Python Boto EC2 find instance given its IP address

Using boto in Python, how can I find the boto.ec2 instance object given an IP address?


Digging through the boto documentation, I found the get_only_instances method, which you use to get all instances. You can pass a filter dictionary to it, to filter by IP Address (I found this in the EC2 API Reference under the Filter.N title).

So for example, to get the instance with IP, you would do:

filters = {"ip-address": ""}
result_list = conn.get_only_instances(filters=filters)

Then result_list[0] should be the Instance object for the instance with that IP address.