Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the addresses of all the EC2 instances tied to a LoadBalancer using Boto?

Tags:

boto

I'd like to get all the instances that are on a LoadBalancer with boto, how can I accomplish that?

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.

like image 402
Kit Sunde Avatar asked Dec 09 '22 23:12

Kit Sunde


2 Answers

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']

like image 192
Kit Sunde Avatar answered Apr 28 '23 08:04

Kit Sunde


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.

like image 20
garnaat Avatar answered Apr 28 '23 08:04

garnaat