我想用boto获取LoadBalancer上的所有实例,我该如何实现?
这是我到目前为止所得到的:
import boto
from boto.regioninfo import RegionInfo
from boto import ec2
ACCESS_KEY_ID = '*****'
SECRET_ACCESS_KEY = '********'
reg = RegionInfo(
name='ap-southeast-1',
endpoint='elasticloadbalancing.ap-southeast-1.amazonaws.com')
conn = boto.connect_elb(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=reg)
ec2_connection = boto.ec2.connection.EC2Connection(
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=reg)
instances = [ instance.id for instance in conn.get_all_load_balancers()[3].instances ]
# instances is now [u'i-62448d36'], so far so good.
ec2_connection.get_all_instances(instances)
以...结尾:
<ErrorResponse xmlns="http://webservices.amazon.com/AWSFault/2005-15-09"> <Error> <Type>Sender</Type> <Code>InvalidAction</Code> <Message>Could not find operation DescribeInstances for version 2012-03-01</Message> </Error> <RequestId>c6aab70d-b22b-11e1-a990-a747bbde9f63</RequestId> </ErrorResponse>
我正在使用boto 2.4.1。
答案 0 :(得分:10)
这就是我最终的结果:
import boto
from boto import regioninfo
from boto import ec2
ACCESS_KEY_ID = '***********'
SECRET_ACCESS_KEY = '***********'
elb_region = boto.regioninfo.RegionInfo(
name='ap-southeast-1',
endpoint='elasticloadbalancing.ap-southeast-1.amazonaws.com')
elb_connection = boto.connect_elb(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=elb_region)
ec2_region = ec2.get_region(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region_name='ap-southeast-1')
ec2_connection = boto.ec2.connection.EC2Connection(
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=SECRET_ACCESS_KEY,
region=ec2_region)
load_balancer = elb_connection.get_all_load_balancers(load_balancer_names=['MediaPopClients'])[0]
instance_ids = [ instance.id for instance in load_balancer.instances ]
reservations = ec2_connection.get_all_instances(instance_ids)
instance_addresses = [ i.public_dns_name for r in reservations for i in r.instances ]
给出:
[u'ec2-46-137-194-58.ap一南1.compute.amazonaws.com']
答案 1 :(得分:4)
我认为这样的事情应该有效:
>>> import boto
>>> elb = boto.connect_elb()
>>> load_balancer = elb.get_all_load_balancers(['my_lb_name'])[0]
>>> for instance_info in load_balancer.instances:
... print instance_info.id
LoadBalancer对象的instances属性是InstanceInfo对象的列表。这些不是实际的Instance对象,但它确实具有instance-id,因此您可以查找与之关联的Instance对象。