Python Boto EC2 查找给定 IP 地址的实例

Python Boto EC2 find instance given its IP address

在 Python 中使用 boto,如何找到给定 IP 地址的 boto.ec2 实例对象?

Filter.N 标题下挖掘 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

因此,例如,要获取 IP 为 1.1.1.1 的实例,您可以这样做:

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

那么 result_list[0] 应该是具有该 IP 地址的实例的实例 object。

有意识

ec2 = boto3.client('ec2')

filters = [{
    'Name': 'ip-address', 
    'Values': ['1.1.1.1'],
}]
result_list = ec2.describe_instances(Filters=filters)