ようへいの日々精進XP

よかろうもん

AWS の AutoScaling Group をいじるコマンドラインツールを作っていたけど, ちゃんと使ってみたら意外に便利だったのでメモ

tl;dr

なにかの拍子に AWS の AutoScaling Group の以下のようなパラメータをいじるコマンドラインツールを作っていたんだけど, 実運用で使ってみたら意外に便利だったのでメモしておきます.

  • サービスメンテナンスの際に一時的に AutoScaling Group 内の EC2 起動台数を増やしたい
  • 同じく, EC2 の最大起動台数を調整したい
  • スポットインスタンスを使っていて, 一時的にスポットインスタンスとオンデマンドインスタンスの割合を調整した

作ったもの

だいぶん前に作っていて, 実運用で利用していなかったんですが... だいぶん雑な実装になっています. すいません.

github.com

インストールはバイナリをパスが通ったディレクトリに放り込んで下さい.

出来ること

事前に

環境変数に以下の内容を設定しておきましょう.

export AWS_PROFILE=your-profile-name
export AWS_REGION=ap-northeast-1

AutoScaling Group 一覧の取得

$ asg
+--------------------------+-------------------+------------------+----------+----------+
|  AUTOSCALING GROUP NAME  | RUNNING INSTANCES | DESIRED CAPACITY | MIN SIZE | MAX SIZE |
+--------------------------+-------------------+------------------+----------+----------+
| oreno-autoscaling-demo   |                 0 |                0 |        0 |        2 |
| oreno-autoscaling-demo1  |                 0 |                0 |        0 |        1 |
+--------------------------+-------------------+------------------+----------+----------+

desired capacity の変更

起動して欲しい EC2 インスタンス台数の変更することが出来ます.

$ asg --group=oreno-autoscaling-demo --desired=2 --dryrun
Will be updated as follows...
  Min              : 2
  Max              : 2
  Desired Capacity : 2

$ asg --group=oreno-autoscaling-demo --desired=2
Change the capacity of AutoScaling Group: oreno-autoscaling-demo
Do you want to continue processing? (y/n): y
+--------------------------+-------------------+------------------+----------+----------+
|  AUTOSCALING GROUP NAME  | RUNNING INSTANCES | DESIRED CAPACITY | MIN SIZE | MAX SIZE |
+--------------------------+-------------------+------------------+----------+----------+
| oreno-autoscaling-demo   |                 0 |                2 |        2 |        2 |
+--------------------------+-------------------+------------------+----------+----------+

起動して欲しい最大の EC2 台数の変更

起動して欲しい最大 EC2 台数を変更することが出来ます.

$ asg --group=oreno-terraform-dev-demo --max=10 --dryrun
Will be updated as follows...
  Min              : 2
  Max              : 10
  Desired Capacity : 2

$ asg --group=oreno-terraform-dev-demo --max=10
Change the capacity of AutoScaling Group: oreno-terraform-dev-demo.
Do you want to continue processing? (y/n): y
+--------------------------+-------------------+------------------+----------+----------+
|  AUTOSCALING GROUP NAME  | RUNNING INSTANCES | DESIRED CAPACITY | MIN SIZE | MAX SIZE |
+--------------------------+-------------------+------------------+----------+----------+
| oreno-autoscaling-demo   |                 2 |                2 |        2 |       10 |
+--------------------------+-------------------+------------------+----------+----------+

オンデマンドインスタンスとスポットインスタンスの台数の割合を変更

AutoScaling Group 内でスポットインスタンスを利用する場合, OnDemandBaseCapacity を超えるキャパシティを超える EC2 についてオンデマンドインスタンスとスポットインスタンスの割合を変更することが出来ます.

$ asg --group=oreno-terraform-dev-demo --per=10 --dryrun
Will be updated as follows...
  OnDemand Percentage : 10

$ asg --group=oreno-terraform-dev-demo --per=10
Change the ondemand percentage of AutoScaling Group: oreno-terraform-dev-demo.
Do you want to continue processing? (y/n): y
+--------------------------+-------------------+------------------+----------+----------+---------------------+
|  AUTOSCALING GROUP NAME  | RUNNING INSTANCES | DESIRED CAPACITY | MIN SIZE | MAX SIZE | ONDEMAND PERCENTAGE |
+--------------------------+-------------------+------------------+----------+----------+---------------------+
| oreno-autoscaling-demo   |                 0 |                0 |        0 |       10 |                  10 |
+--------------------------+-------------------+------------------+----------+----------+---------------------+

内部的には OnDemandPercentageAboveBaseCapacity の値を指定することになります.

docs.aws.amazon.com

ヘルプ

$ asg --help
Usage of asg:
  -desired string
        Set a Desired capacity number.
  -dryrun
        Show a update execution.
  -group string
        Set a AutoScaling Group Name.
  -max string
        Set a Max capacity number.
  -per string
        Set a OnDemand percentage number (%).
  -version
        Print version number.

以上

Terraform のような構成管理ツールで構築されている環境に対して, このようなコマンドラインツールでの操作が正しいアプローチなのかはわかりませんが, マネジメントコンソールからポチポチするよりはミスは防げそうな気がするので, よろしければ利用をご検討頂けると幸いです.