ECS に Auto Scaling を設定します。

  • Web マネジメントコンソールからサービス作成時に Auto Scaling を設定
  • CloudFormation で Auto Scaling 設定 CloudFormation の Auto Scaling 関連リソース
    • AWS::ApplicationAutoScaling::ScalableTarget:MaxCapacity、MinCapacity に最大数と最小数を指定。ResourceId に Auto Scaling を設定する ECS サービスの ARN を指定
    • AWS::ApplicationAutoScaling::ScalingPolicy:ResourceId に Auto Scaling を設定する ECS サービスの ARN を指定

Webマネジメントコンソール

Auto Scaling 1

CloudFormation

ref. Application Auto Scaling resource type reference

ECS に Auto Scalingを設定するにはおもに3つのリソースが必要です。

  • AWS::CloudWatch::Alarm
    CloudWatch Alarm Metrix(例:SQS の ApproximateNumberOfMessagesVisible など)をトリガーにして、指定した AlarmActions(AWS::ApplicationAutoScaling::ScalingPolicy) が実行される
  • AWS::ApplicationAutoScaling::ScalingPolicy
    • CloudWatch Alarm Metrix をトリガーに実行される AutoScaling を指定する
    • ScalingTargetId に Auto Scaling する対象リソース(AWS::ApplicationAutoScaling::ScalableTarget)を指定する
  • AWS::ApplicationAutoScaling::ScalableTarget
    • AWS::ApplicationAutoScaling::ScalingPolicy の ScalableTarget に指定できるリソース一覧は RegisterScalableTarget の ResourceId を参照

MaxCapacity、MinCapacity と ScalingAdjustment の関係

AWS::ApplicationAutoScaling::ScalableTargetの MaxCapacity、MinCapacity で指定した範囲内で、AWS::ApplicationAutoScaling::ScalingPolicy の AWS::ApplicationAutoScaling::ScalingAdjustment を指定します。

MetricIntervalUpperBound、MetricIntervalUpperBound

MetricIntervalUpperBound

  • アラーム閾値(AWS::CloudWatch::Alarm.Threshold)を超えている場合は含まない
  • アラーム閾値(AWS::CloudWatch::Alarm.Threshold)を超えていない場合は含む

MetricIntervalUpperBound The upper bound for the difference between the alarm threshold and the CloudWatch metric. If the metric value is above the breach threshold, the upper bound is exclusive (the metric must be less than the threshold plus the upper bound). Otherwise, it is inclusive (the metric must be less than or equal to the threshold plus the upper bound). A null value indicates positive infinity.

The upper bound must be greater than the lower bound.

– https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-autoscaling-scalingpolicy-stepadjustments.html#cfn-autoscaling-scalingpolicy-stepadjustment-metricintervalupperbound

DeepL 翻訳:

MetricIntervalUpperBound アラーム閾値とCloudWatchメトリックの差の上限値。メトリック値が違反しきい値より上の場合、上限は排他的である(メトリックはしきい値に上限を足した値より小さくなければならない)。それ以外の場合は、包含的です(メトリックは、閾値に上限を加えた値以下でなければなりません)。ヌル値は正の無限大を示す。

MetricIntervalLowerBound

  • アラーム閾値(AWS::CloudWatch::Alarm.Threshold)を超えている場合は含む
  • アラーム閾値(AWS::CloudWatch::Alarm.Threshold)を超えていない場合は含まない

MetricIntervalLowerBound The lower bound for the difference between the alarm threshold and the CloudWatch metric. If the metric value is above the breach threshold, the lower bound is inclusive (the metric must be greater than or equal to the threshold plus the lower bound). Otherwise, it is exclusive (the metric must be greater than the threshold plus the lower bound). A null value indicates negative infinity.

– https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-properties-autoscaling-scalingpolicy-stepadjustments.html#cfn-autoscaling-scalingpolicy-stepadjustment-metricintervalupperbound

DeepL翻訳:

アラーム閾値とCloudWatchメトリックの差の下限値。メトリック値が違反しきい値を超えている場合、下限は包括的である(メトリックはしきい値に下限を足した値以上でなければならない)。それ以外の場合は、排他的です(メトリックは、閾値に下限を加えた値より大きくなければなりません)。ヌル値は負の無限大を示す。

SQS のキューが空になったら ECS サービスを停止する

Fargate で Amazon ECS を Amazon CloudWatch アラームおよび Application Auto Scaling と統合することで、タスクの必要数を増減できます。その後、CloudWatch メトリクスを使用して CloudWatch アラームを設定できます。

CloudWatch アラームが Auto Scaling ポリシーをトリガーすると、Application Auto Scaling は、設定されたスケーリングポリシーに基づいて新しい必要数を決定します。その後、Application Auto Scaling は、新しい必要数の値を使用して UpdateService API を Amazon ECS に呼び出します。Amazon ECS サービススケジューラは、新しい必要数を満たすた

– https://aws.amazon.com/jp/premiumsupport/knowledge-center/ecs-fargate-service-auto-scaling/?utm_source=pocket_mylist

ECS スケジュールタスク

スケジュールタスクには Auto Scaling の機能はなく、事前にターゲットに指定されたタスクが起動する。
以下だと3つのtargetが起動する。

Auto Scaling 2

タスクの数と Auto Scaling の必要数

  • タスクの数
    Auto Scaling 3
  • Auto Scalingの必要数
    Auto Scaling 4