Skip to content

Instantly share code, notes, and snippets.

Created December 2, 2012 08:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Find EC2 worker instances
#!/usr/bin/env ruby
require 'rubygems'
require 'fileutils'
require 'right_aws'
require 'optparse'
require 'pp'
options = {}
optparse = do|opts|
opts.banner = "Usage: bootstrap.rb [options]"
# Define the options, and what they do
options[:"access-key"] = nil
opts.on( '-a', '--access-key ACCESS_KEY', 'AWS Access Key' ) do |x|
options[:"access-key"] = x
options[:"secret-key"] = nil
opts.on( '-s', '--secret-key SECRET_KEY', 'AWS Secret Key' ) do |x|
options[:"secret-key"] = x
options[:"stack-name"] = nil
opts.on( '-n', '--stack-name STACK_NAME', 'CloudFormation Stack Name' ) do |x|
options[:"stack-name"] = x
puts("parsed args")
logger = Logger::new(STDOUT)
logger.level = Logger::ERROR
instance_id = `curl -s`.chomp
puts("instance_id = #{instance_id}")
ec2 =[:'access-key'], options[:'secret-key'], :logger => logger)
puts("signed into ec2")
workers = []
puts(" finding workers:")
instances = ec2.describe_instances
for instance in instances
if instance.has_key? :tags \
and instance[:tags].has_key? "aws:autoscaling:groupName" \
and instance[:tags].has_key? "aws:cloudformation:stack-name" \
and instance[:tags]["aws:cloudformation:stack-name"] == \
options[:'stack-name'] and instance[:aws_state] == "running"
puts("got a stack member")
workers.push instance
puts("found #{workers.size} workers")
numcores = 4
master_ipaddr = `curl -s`.chomp
puts "#{master_ipaddr} #{numcores}"
workers.each_with_index do |worker, i|
puts "#{worker[:private_ip_address]} #{numcores}"
Copy link

cbare commented Dec 6, 2012

Credit due to Dan Tenenbaum for the original, which can be found here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment