3

我得到了特定地区的 elb 详细信息,比如欧洲,然后我就可以得到与 ELB 相关的实例。问题是我无法获取这些实例的公共 dns。我要做的是

conn = regions[3].connect(aws_access_key_id= access, aws_secret_access_key = secret_key)
loadbalancers = conn.get_all_load_balancers()
for lb in loadbalancers:
print lb.instances

如何获取这些实例的 public_dns_name

当我尝试

for i in lb.instances:
    i.public_dns_name

AttributeError:“InstanceInfo”对象没有属性“public_dns_name”

4

1 回答 1

9

LoadBalancer 类的“instances”属性只包含关于实例的一小部分信息——它不是一个完整的 Instance 对象。要获取完整的实例对象,您必须使用可用的 instanceId 来查询更多信息。此代码片段通过所需的调用扩展了您的代码片段:

#Create connection to ec2, credentials stored in environment
ec2_conn = connect_ec2()

conn = regions[3].connect(aws_access_key_id= access, aws_secret_access_key = secret_key)
loadbalancers = conn.get_all_load_balancers()
for lb in loadbalancers:
    for i in lb.instances:
        #Here, 'i' is an InstanceInfo object, not a full Instance
        instance_id = i.id

        #Query based on the instance_id we've got
        #This function actually gives reservations, not instances
        reservations = ec2_conn.get_all_instances(instance_ids=[instance_id])
        #pull out our single instance
        instance = reservations[0].instances[0]

        #Now we've got the full instance and we can get parameters from here
        print(instance.public_dns_name)
于 2011-07-14T13:39:32.490 回答