EC2 Auto ScalingのAvailability Zone rebalancingを停止する

2022年5月24日

Amazon EC2

Amazon EC2 Auto Scaling group には Availability Zone rebalancing という Availability Zone のインスタンス数の偏りを修正する機能があります。この機能が発動すると、偏りをなくすために別の AZ に新しくインスタンスが立ち上がり、古いインスタンスが終了されます。

この機能はデフォルトで有効になっていて、ロードバランサの配下にあるときはロードバランサが適切に Draining の処理を行ってくれるので意識しなくても問題がないことが多いです。しかし、SQS などキューと Auto Scaling を組み合わせて複数の EC2 インスタンスでジョブを実行するときなどに、インスタンスが AZ rebalance により終了されてしまうと、実行中のジョブが予期せず停止してしまうことがあります。

対処方法はアプリケーションの仕様や設計によりいくつか考えられますが、今回は単純に AZ rebalancing が行われないようにする方法を紹介します。

目次

  1. AZ rebalancing が発生したときのメッセージ
  2. AZ rebalancing を停止する

AZ rebalancing が発生したときのメッセージ

AZ rebalancing が発生すると Auto Scaling group の Activity history に、以下のようにメッセージが出力されます。

ap-northeast-1a に 0 台、ap-northeast-1d に 2 台のインスタンスがあり、バランスを取るために 1 台のインスタンスが起動したようです。

At 2022-05-22T06:07:24Z availability zones ap-northeast-1a ap-northeast-1d had 0 2 instances respectively. An instance was launched to aid in balancing the group's zones.

10 秒後にさらにメッセージ出力。1 台インスタンスが起動したことで、desired capacity を上回ったために 1 台インスタンスを終了したとのこと。

At 2022-05-22T06:07:24Z instances were launched to balance instances in zones ap-northeast-1a ap-northeast-1d with other zones resulting in more than desired number of instances in the group. At 2022-05-22T06:07:45Z an instance was taken out of service in response to a difference between desired and actual capacity, shrinking the capacity from 4 to 3. At 2022-05-22T06:07:45Z instance i-0a050225a67b26747 was selected for termination.

このエラーメッセージの説明は、Troubleshoot why Auto Scaling terminated an instance に詳しく書かれています。エラーメッセージでページ内検索をしてみてください。

"An instance was launched to aid in balancing the group's zones" or "Instances were launched to balance instances in zones us-east-1a with other zones"

デフォルトでは、Amazon EC2 Auto Scaling はすべてのアベイラビリティーゾーンを対象にインスタンスを分散します。ユーザーが新しいアベイラビリティーゾーンを Auto Scaling グループに追加すると、Amazon EC2 Auto Scaling はそのゾーンで新しいインスタンスを起動し、リバランスにより他のゾーンのインスタンスが終了する場合があります。

AZ rebalancing を停止する

AZ rebalancing を停止する方法は Suspend and resume a process for an Auto Scaling group - Amazon EC2 Auto Scaling に詳しく書かれています。

マネジメントコンソールと AWS CLI の 2 パターン紹介します。

マネジメントコンソールから操作する場合は Suspend and resume processes (console) を参考に設定を変更します。Auto Scaling group の Details タブで、Advanced configurations の編集ボタンを押し、Suspended process に AZRebalance を選択します。

AWS CLI の場合は Suspend and resume processes (AWS CLI) を参考に以下のコマンドを実行します。CloudFormation や Terraform の場合も同じ API を探します。

aws autoscaling suspend-processes --auto-scaling-group-name my-asg --scaling-processes AZRebalance

-技術ブログ
-