开发者

Ruby calling AWS ELB functions

I'm writing some Ruby scripts to wrap AWS ELB开发者_开发知识库 command line calls, mostly so that I can act on several ELB instances simultaneously. One task is to use the elb-describe-instance-health call to see what instance IDs are attached to this ELB.

I want to match the Instance ID to a nickname we have set up for those instances, so that I can see at a glance what machines area connected to the ELB, without having to look up the instance names.

So I am issuing:

cmd = "elb-describe-instance-health #{elbName}"
value = `#{cmd}`

Passing the elb name into the call. This returns output such as:

INSTANCE_ID  i-jfjtktykg  InService  N/A  N/A
INSTANCE_ID  i-ujelforos  InService  N/A  N/A

One line appear for each instance in the ELB. There are two spaces between each field.

What I need to get is the second field, which is the actual instance ID. Basically I'm trying to get each line returned, turn it into an array, get the 2nd field, which I can then use to lookup our server nickname.

Not sure if this is the right approach, but any suggestions on how to get this done are very welcome.


The newly released aws-sdk gem supports Elastic Load Balancing (AWS::ELB). If you want to get a list of instance ids attached to your load balancer you can do the following:

AWS.config(:access_key_id => '...', :secret_access_key => '...')

elb = AWS::ELB.new
intsance_ids = elb.load_balancers['LOAD_BALANCER_NAME'].instances.collect(&:id)

You could also use EC2 to store your instance nicknames.

ec2 = AWS::EC2.new
ec2.instances['INSTANCE_ID'].tags['nickname'] = 'NICKNAME'

Assuming your instances are tagged with their nicknames, you could collect them like so:

elb = AWS::ELB.new
elb.load_balancers['LOAD_BALANCER_NAME'].instances.collect{|i| i.tags['nickname'] }


A simple way to extract the second column would be something like this:

ids = value.split("\n").collect { |line| line.split(/\s+/)[1] }

This will leave the second column values in the Array ids. All this does is breaks the value into lines, breaks each line into whitespace delimited columns, and then extracts the second column.

There's probably no need to try to be too clever for something like this, a simple and straight forward solution should be sufficient.

References:

  • collect
  • split
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜