Skip to content

Instantly share code, notes, and snippets.

@evandhoffman
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandhoffman/68bc20aca7449e73375e to your computer and use it in GitHub Desktop.
Save evandhoffman/68bc20aca7449e73375e to your computer and use it in GitHub Desktop.
List all instances in an ELB, and all ELBs the instances are in.

Usage

$ ruby ./instance_to_elb.rb

Output

The output is a JSON containing the mapping of LB->instances as well as Instance->LBs.

require 'rubygems'
require 'json'
require 'aws-sdk'
ec2 = AWS::EC2.new
aws_elb = AWS::ELB.new
instances = {}
elbs = {}
parent = {}
aws_elb.load_balancers.each do |elb|
puts elb.name
elbs[elb.name] = Array.new
elb.instances.each do |instance|
elbs[elb.name].push(instance.tags.Name)
if (instances[instance.tags.Name].nil?)
instances[instance.tags.Name] = Array.new
end
instances[instance.tags.Name].push(elb.name)
end
end
parent['elb-to-instance'] = elbs
parent['instance-to-elb'] = instances
puts JSON.pretty_generate(parent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment