2011-07-12 112 views
2

我得到某個特定區域的elb詳細信息,例如歐洲,然後我可以獲得與ELB相關的實例。問題是我無法獲得這些實例的公共DNS。我做的是如何使用BOTO python獲得AWS中實例的公共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'

回答

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) 
相關問題