Created
November 13, 2016 19:50
-
-
Save arnaldopereira/ed5176998bd2a9a7beaacfe38fcb6a3c to your computer and use it in GitHub Desktop.
get aws ELB instance IP addresses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import boto | |
from boto.ec2 import elb | |
ACCESS_KEY_ID = 'access-key-id' | |
SECRET_ACCESS_KEY = 'secret-access-key' | |
def get_instances_ips(region, load_balancer_name): | |
elb_conn = boto.ec2.elb.connect_to_region(region) | |
ec2_connection = boto.ec2.connect_to_region(region) | |
load_balancer = elb_conn.get_all_load_balancers(load_balancer_names=[load_balancer_name])[0] | |
instance_ids = [ instance.id for instance in load_balancer.instances ] | |
reservations = ec2_connection.get_all_instances(instance_ids) | |
instance_addresses = [ i.ip_address for r in reservations for i in r.instances ] | |
ips = [] | |
for r in reservations: | |
for instance in r.instances: | |
ips += [i.private_ip_addresses[0].private_ip_address for i in instance.interfaces] | |
return ips | |
print 'IPs:', get_instances_ips('sa-east-1', 'foo') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment