3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS-SDK for RubyからELB直下のインスタンスのpublic DNSを一括取得するには

3
Posted at

AWS-SDK

みなさんAWS-SDK使ってますか?
あれドキュメントたくさんある上にサンプルコードあまりなくてあれですよね。

Elastic Load Blancerを操作する

ちょいちょいやってるとあの直下にぶら下がっているインスタンス全台に対して処理したいなんてことがでてきたりします。

増えたり減ったりするインスタンスを意識せずにデプロイとか。

Public DNSの取得

以下でできます。

# !/usr/bin/ruby 
require "aws-sdk"
require "pp"

AWS.config(
  access_key_id: '******',
  secret_access_key: '******',
  ec2_endpoint: '[環境に合わせてください]',
  elb_endpoint: '[環境にあわせてください]'
)
elb = AWS::ELB.new.load_balancers['[自分のELB名]']
instances = elb.instances.select {|i| i.exists? && i.status == :running}.map(&:dns_name)

pp instances

これでインスタンスの一覧が配列で出てきます。

ppで出した結果はこんな感じ。

[ "ec2-**-***-***-**.ap-southeast-1.compute.amazonaws.com",
 "ec2-**-***-***-**.ap-southeast-1.compute.amazonaws.com",
 "ec2-**-***-***-**.ap-southeast-1.compute.amazonaws.com",
 "ec2-**-***-***-**.ap-southeast-1.compute.amazonaws.com"]

あとはそのままdeploy.rbやserverspecにぶちこんで回せば検証ができると思います。

それではよいAWSライフを!

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?