I get the elb details of a specific region, say Europe. Then I am able to get the instances that are related to the ELB. The problem is I am not able to get the public dns of those instances. What I do is:
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
How to get the pu开发者_运维问答blic_dns_name of these instances?
When I try:
for i in lb.instances:
i.public_dns_name
AttributeError: 'InstanceInfo' object has no attribute 'public_dns_name'
The "instances" attribute of the LoadBalancer class only contains a tiny bit of information about the instance - it's not a full Instance object. To get the full instance object you must use the instanceId, which is available, to query for more info. This code snippet extends yours with the required calls:
#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)
精彩评论