Edited at

Ruby を使って、特定の ELB 下の EC2 インスタンスの IPアドレスを取得する

More than 5 years have passed since last update.

require "aws-sdk"

def get_ec2_instances elb_name, region
client = AWS::ELB.new region: region,
access_key_id: ENV["AWS_ACCESS_KEY_ID"],
secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"]
elb = client.load_balancers.find{|lb| lb.name == elb_name}
Array.new.tap do |ip_addrs|
elb.instances.each do |instance|
ip_addrs << instance.private_ip_address
end
end

ip_addrs = get_ec2_instances "YOUR_ELB_NAME", "YOUR_REGION"
p ip_addrs