LoginSignup
3
1

More than 5 years have passed since last update.

AWS SDK for RubyでALBを操作する

Last updated at Posted at 2016-10-10

2016/08に新しいELBである、ALB(Application Load Banalcer)が発表されましたので、そちらをRubyで操作してみます。

ドキュメント

gemのインストール

sdk 2.5.3で対応されたようなので、それ以上のバージョンを入れましょう。

gem 'aws-sdk', '~> 2.5.3'

ELB Clietnの生成

ALBは、Aws::ElasticLoadBalancingV2::Clientクラスを利用して操作します。

access_key =  "XXXXXXXXXXXXXXXXXXXXX"
secret_key =  "XXXXXXXXXXXXXXXXXXXXX"

credentials = Aws::Credentials.new(access_key, secret_key)

region = 'ap-northeast-1'

alb_cleint = Aws::ElasticLoadBalancingV2::Client.new(
        :credentials => credentials,
        :region => region
)

ターゲットグループへのインスタンス追加・削除

ALBでは「ターゲットグループ」という新しい概念が登場しているため、インスタンスの取り外しをする場合はALBではなく、ターゲットグループに対して操作します。

target_group_arns = "arn:aws:elasticloadbalancing:ap-northeast-1:12345678:targetgroup/elb-xxxxxx/123456"

instance_id ="i-123456"

     alb_cleint.register_targets({
        target_group_arn: target_group_arns,
        targets: [
          {
             id: instance_id,
          },
        ],
      })


    alb_cleint.deregister_targets({
        target_group_arn: target_group_arns,
        targets: [
          {
            id: instance_id,
          },
        ],
    })
3
1
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
1