Search code examples
pythonamazon-ec2boto

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?


Solution

  • 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 1.1.1.1, you would do:

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

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