gpt4 book ai didi

boto - 如何使用 Boto 获取绑定(bind)到 LoadBalancer 的所有 EC2 实例的地址?

转载 作者:行者123 更新时间:2023-12-04 22:06:46 30 4
gpt4 key购买 nike

我想使用 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。

最佳答案

这就是我最终的结果:

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-southeast-1.compute.amazonaws.com']

关于boto - 如何使用 Boto 获取绑定(bind)到 LoadBalancer 的所有 EC2 实例的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10955741/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com