I'd like to get all the instances that are on a LoadBalancer with boto, how can I accomplish that?
我想用boto获取LoadBalancer上的所有实例,我该如何实现?
This is what I've got so far:
这是我到目前为止所得到的:
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)
Which ends with:
结束于:
<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>
I'm using boto 2.4.1.
我正在使用boto 2.4.1。
2 个解决方案
#1
10
This is what I ended up with:
这就是我最终的结果:
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 ]
Gives:
得到:
[u'ec2-46-137-194-58.ap-southeast-1.compute.amazonaws.com']
[u'ec2-46-137-194-58.ap一南1.compute.amazonaws.com']
#2
4
I think something like this should work:
我认为这样的事情应该有效:
>>> 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
The instances attribute of the LoadBalancer object is a list of InstanceInfo objects. These are not actual Instance object but it does have the instance-id so you can then look up the Instance object associated with it.
LoadBalancer对象的instances属性是InstanceInfo对象的列表。这些不是实际的Instance对象,但它确实具有instance-id,因此您可以查找与其关联的Instance对象。
#1
10
This is what I ended up with:
这就是我最终的结果:
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 ]
Gives:
得到:
[u'ec2-46-137-194-58.ap-southeast-1.compute.amazonaws.com']
[u'ec2-46-137-194-58.ap一南1.compute.amazonaws.com']
#2
4
I think something like this should work:
我认为这样的事情应该有效:
>>> 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
The instances attribute of the LoadBalancer object is a list of InstanceInfo objects. These are not actual Instance object but it does have the instance-id so you can then look up the Instance object associated with it.
LoadBalancer对象的instances属性是InstanceInfo对象的列表。这些不是实际的Instance对象,但它确实具有instance-id,因此您可以查找与其关联的Instance对象。