スケジュールされた時間にスケールする
$ aws autoscaling put-scheduled-update-group-action \
--scheduled-action-name scheduled-scale-out \
--auto-scaling-group-name sample-auto-scale \
--start-time "2017-12-10T14:00:00Z" \
--desired-capacity 2
CloudWatchのメトリクスを元にスケールする
今回はCPU使用率をメトリクスに以下のルールでスケールするように設定します。
CPU使用率 | アクション |
---|---|
70 <= CPUUtilization < 80 | 現在のインスタンス数から10%増やす |
80 <= CPUUtilization < 90 | 現在のインスタンス数から20%増やす |
90 <= CPUUtilization (90%以上) | 現在のインスタンス数から30%増やす |
ScalingPolicyを作成する
$ aws autoscaling put-scaling-policy \
--auto-scaling-group-name sample-auto-scale \
--policy-name sample-auto-scale-step-policy \
--policy-type StepScaling \
--adjustment-type PercentChangeInCapacity \
--metric-aggregation-type Average \
--step-adjustments MetricIntervalLowerBound=10.0,MetricIntervalUpperBound=20.0,ScalingAdjustment=10 \
MetricIntervalLowerBound=20.0,MetricIntervalUpperBound=30.0,ScalingAdjustment=20 \
MetricIntervalLowerBound=30.0,ScalingAdjustment=30 \
--min-adjustment-magnitude 1
--step-adjustments
のパラメータですが、
API または CLI を使用している場合は、違反しきい値と相対的に上限と下限を指定します。
なので、CloudWatch Alarm
の閾値を60%にする想定で、差分を指定しています。
例えば、CPU使用率70%以上80未満の指定は MetricIntervalLowerBound=10.0,MetricIntervalUpperBound=20.0
のように行っています。
CloudWatch Alarmを設定する
$ aws cloudwatch put-metric-alarm \
--alarm-name sample-auto-scale-out-alarm \
--metric-name CPUUtilization --namespace AWS/EC2 \
--statistic Average \
--threshold 60 --comparison-operator GreaterThanOrEqualToThreshold \
--period 180 --evaluation-periods 1 \
--dimensions "Name=AutoScalingGroupName,Value=sample-auto-scale" \
--alarm-actions arn:aws:autoscaling:us-west-2:999999999999:scalingPolicy:xxxxxxxx-yyyy-zzzz-aaaa-999999999999:autoScalingGroupName/sample-auto-scale:policyName/sample-auto-scale-step-policy
対象のAutoScalingGroupを --dimensions
で指定します。
またこのアラームで作成したScalingPolicyを呼び出して欲しいので --alarm-actions
で先ほど作成したScalingPolicyのARNを指定します。
またScalingPolicy作成時に閾値を60に想定して差分指定していたので--threshold
を60に指定します。