Skip to content

Instantly share code, notes, and snippets.

@katzchang
Last active January 13, 2019 23:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katzchang/9360133 to your computer and use it in GitHub Desktop.
Save katzchang/9360133 to your computer and use it in GitHub Desktop.
EC2 Auto Scalingを使ったときのインスタンス破棄のポリシーについての覚書 @katzchang.gist

EC2 Auto Scalingを使ったときのインスタンス破棄のポリシーについての覚書

http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/us-termination-policy.html より。Auto Scalingがインスタンスを破棄(terminate)するとき、どのインスタンスを選ぶかのポリシーがある。無指定であればdefaultポリシーが適用されているが、選ぶこともできる。

どんなポリシーが用意されているか?

Default

  • Availability Zone毎にインスタンス数を取り、多いAZを特定する。
  • 同数ならランダムに選ぶ。
  • 選択したAZ内で、
  • 古いLaunch Config(CreatedTimeで判断)のものがあればそれ
  • instance-hourに到達する時間に最も近いもの、つまり、なるべく課金すくなくなるように
  • 同じならランダム

選択できるポリシー

  • OldestInstence
  • もっとも古い時間にlaunchされたインスタンスを選択
  • AZ選択はdefaultに同じ…な気がするけど、具体的な記述はない
  • NewestInstance
  • 読んで字のごとく
  • これを選びたい場面、なかなか想像できない
  • OldestLaunchConfiguration
  • defaultのポリシーの一部
  • ClosestToNextInstanceHour
  • defaultのポリシーの一部
  • Default

ポリシーは複数組み合わせて指定できるそうだ。

設定方法

Auto Scaling Groupの属性として設定する。Management Console, Command Line Interfaceあたりからできるっぽい。 詳しくは http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/us-termination-policy.html を参照。

例示の asg-test-termination-policy はAuto Scaling Group name の例(若干紛らわしい)。

確認例

as-describe-auto-scaling-groups asg-test-termination-policy asg-test-termination-policy  --headers

作成例

as-create-auto-scaling-group asg-test-termination-policy --launch-configuration lc-test-termination-policy --availability-zones "us-east-1e" --max-size 3 --min-size 1 –desired-capacity 2 --termination-policies "OldestInstance"

--termination-policies "OldestInstance"

どうでもいいけど、公式ドキュメントの↑部分文字がダメで、コピペで動かないんじゃないか疑惑がある。

更新例

as-update-auto-scaling-group your Auto Scaling group name --termination-policies "OldestInstance"

複数指定する場合

,で複数指定する。前後のスペースは不要(あると怒られる安心設計)。

さて、適用順がどうなるかが気になったので実験した。矛盾した2つの設定をして…

as-update-auto-scaling-group my-ag --termination-policies "OldestInstance,NewestInstance"

2台->4台に増やし、インスタンスが立ち上がったことを確認したあと、4台->2台に減らした。 すると、古い2台が破棄された。

前方が優先されるらしいことがわかった。

まぁそうだよね。

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