How can I configure Amazon ECS Service Auto Scaling on Fargate?

Last updated: 2021-04-06

I want to configure Amazon Elastic Container Service (Amazon ECS) Service Auto Scaling on AWS Fargate.

Short description

You can increase or decrease your desired task count by integrating Amazon ECS on Fargate with Amazon CloudWatch alarms and Application Auto Scaling. Then, you can use CloudWatch metrics to configure your CloudWatch alarms.

When your CloudWatch alarms trigger an Auto Scaling policy, Application Auto Scaling decides the new desired count based on the configured scaling policy. Then, Application Auto Scaling makes the UpdateService API call to Amazon ECS with the new desired count value. The Amazon ECS service scheduler launches or shuts down tasks to meet the new desired count. Your scaling activity remains in the InProgress state until the desired count and the running count are same.

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.

Resolution

Configure Service Auto Scaling for your Fargate service

When you create or update a service in the Amazon ECS console, choose the following from the Set Auto Scaling page:

1.    Choose Configure Service Auto Scaling to adjust your service’s desired count.

2.    For Minimum number of tasks, enter the lowest number of tasks that you want Service Auto Scaling to use.

3.    For Desired number of tasks, enter the number of tasks that you want Service Auto Scaling to use.

Note: The desired number of tasks must be in the range of your minimum and maximum task count.

4.    For Maximum number of tasks, enter the highest number of tasks that you want Service Auto Scaling to use.

Note: The minimum and maximum number of tasks are hard limits for your service.

5.    For IAM role for Service Auto Scaling, choose ecsAutoscaleRole.

Note: If this role doesn't exist, choose Create new role to have the console create the role for you.

6.    In the Automatic task scaling policies section, choose Auto Scaling Policy.

7.    Complete the remaining steps in the setup wizard to create or update your service.

Choose a scaling policy

Choose either a target tracking policy or step scaling policy based on your requirements and the following considerations:

For target tracking policies:

  • You must define a target value (threshold) for your specified metric. Application Auto Scaling creates and manages the CloudWatch alarms that trigger the scaling policy.
  • You can use the ECSServiceAverageCPUUtilization, ECSServiceAverageMemoryUtilization, and ALBRequestCountPerTarget Amazon ECS service metrics for target tracking.
  • A target tracking policy calculates the scaling adjustment (that is, desired task count) based on the metric and target value that you define. You don't need to configure the scaling action like you do with a step scaling policy. This is because a target tracking policy adds or removes capacity as required to keep the metric at, or close to, the specified target value.
  • You must choose a scale-out and scale-in cooldown period.

For step scaling policies:

  • You can create or use existing CloudWatch alarms for any metric for step scaling.
  • You must choose scaling actions or step adjustments, such as ScalingAdjustment, MetricIntervalUpperBound, and MetricIntervalLowerBound.
  • You can specify the scaling adjustment type as a percentage of the current capacity of your scalable target, or by using absolute numbers.
  • You can increase or decrease your desired task count by creating scaling policies to handle scale-in and scale-out activity.

Note: For more information, see Step 5: Configuring your service to use Service Auto Scaling.

Configure Service Auto Scaling using the AWS CLI

1.    Register your Fargate service as a scalable target with Application Auto Scaling:

aws application-autoscaling register-scalable-target \
--service-namespace ecs --scalable-dimension ecs:service:DesiredCount \
--resource-id service/your-cluster/your-service-name \
--min-capacity 1 --max-capacity 10 --region us-east-1

2.    Create a target tracking or step scaling policy for the scalable target (that is, your Fargate service).

Target tracking policy:

Create a target tracking policy:

aws application-autoscaling put-scaling-policy \
--service-namespace ecs --scalable-dimension ecs:service:DesiredCount \
--resource-id service/<cluster>/<service-name> \
--policy-name Test-target-tracking-scaling-policy --policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration '{ "TargetValue": 75.0, "PredefinedMetricSpecification": {"PredefinedMetricType": "ECSServiceAverageCPUUtilization" }, "ScaleOutCooldown": 60,"ScaleInCooldown": 60}'

Note: When you create your target tracking policy, Application Auto Scaling creates CloudWatch alarms for scaling in and scaling out based on the target value (threshold) that you choose.

Step scaling policy:

1.    Create a step scaling policy with the required step adjustments:

aws application-autoscaling put-scaling-policy \
--service-namespace ecs --scalable-dimension ecs:service:DesiredCount \
--resource-id service/your-cluster/your-service-name \
--policy-name Test-Step-scaling-policy-ScaleOut --policy-type StepScaling \
--step-scaling-policy-configuration '{"AdjustmentType": "ExactCapacity","StepAdjustments": [ { "MetricIntervalLowerBound": 20, "ScalingAdjustment": 10}, { "MetricIntervalLowerBound": 0, "MetricIntervalUpperBound": 20,"ScalingAdjustment": 5}],"Cooldown": 60, "MetricAggregationType": "Average"}'

2.    Associate your scaling policy from step 1 as an alarm action to your new or existing CloudWatch alarm:

aws cloudwatch put-metric-alarm \
--alarm-name Test-ScaleOut --metric-name MemoryUtilization \
--namespace AWS/ECS --statistic Average --period 60 --threshold 60 \
--comparison-operator GreaterThanOrEqualToThreshold --evaluation-periods 1 --datapoints-to-alarm 1 \
--dimensions Name=ClusterName,Value=your-cluster Name=ServiceName,Value=your-service-name --unit Percent \
--alarm-actions "arn:aws:autoscaling:us-east-1:xxxxxxx:scalingPolicy:xxxxxx/Test-Step-scaling-policy-ScaleOut"

3.    Create another step scaling policy and CloudWatch alarm for the scale-in activity.

Note: When the scale-out activity of your service is in progress, any scale-in activity that's triggered by CloudWatch metrics is blocked until the scale-out activity is completed. The InProgress scale-out activity times out when Amazon ECS doesn't meet the desired count set by Auto Scaling. The time out can have various causes, such as image or networking issues. If a scale-in activity is triggered by CloudWatch metrics during the scale-out cooldown period but a scale-out activity is completed, then a scale-in activity is run.