Skip to content

How can I configure Amazon ECS service auto scaling on Fargate?

4 minute read
0

I want to configure Amazon Elastic Container Service (Amazon ECS) service auto scaling on AWS Fargate.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.

When an Amazon CloudWatch alarm initiates auto scaling, Application Auto Scaling uses your scaling policy to determine the new desired count. Then, Application Auto Scaling initiates the UpdateService API call to Amazon ECS with the new value for the desiredCount parameter. The Amazon ECS service scheduler launches or stops tasks to match the new desired count. Your scaling activity remains in the InProgress state until the new desired count matches the running count.

You can use either the Amazon ECS console or AWS CLI to configure Amazon ECS auto scaling on Fargate. The AWS Identity and Access Management (IAM) user that configures service auto scaling settings must have permissions to services that support dynamic scaling. For more information, see IAM permissions required for Amazon ECS service auto scaling.

Use the Amazon ECS console

Complete the following steps:

  1. Open the Amazon ECS console.
  2. Choose Service auto scaling, and then select the Use service auto scaling option.
  3. Enter the following values for service auto scaling:
    For Minimum number of tasks, enter the lowest number of tasks.
    For Maximum number of tasks, enter the highest number of tasks.
    Note: The desired count doesn't go below the minimum or above the maximum numbers.
  4. Create a target tracking scaling policy, or create a step scaling policy to add to Amazon ECS.

Use the AWS CLI

Run the following register-scalable-target command to register Fargate 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/CLUSTER_NAME/SERVICE_NAME \
--min-capacity 1 --max-capacity 10 --region REGION

Note: Replace CLUSTER_NAME with your cluster, SERVICE_NAME with your service, and REGION with your AWS Region.

Then, run the put-scaling-policy to create the target tracking scaling policy or step scaling policy for Fargate as the scalable target. In the command, include the JSON file that you created.

Example command for target tracking scaling policy:

aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--scalable-dimension ecs:service:DesiredCount \
--resource-id service/CLUSTER_NAME/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: Replace CLUSTER_NAME with your cluster, SERVICE_NAME with your service, and REGION with your Region. Application Auto Scaling creates CloudWatch alarms for scaling based on your target values.

Example command for step scaling policy:

aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--scalable-dimension ecs:service:DesiredCount \
--resource-id service/CLUSTER_NAME/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"}'

Note: Replace CLUSTER_NAME with your cluster, SERVICE_NAME with your service, and REGION with your Region.

If you created a step scaling policy, then run the following put-metric-alarm command to add your scaling policy to a CloudWatch alarm. Then, repeat the preceding step to create a second step scaling policy and CloudWatch alarm for the scaling activity:

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=CLUSTER_NAME Name=ServiceName,Value=SERVICE_NAME --unit Percent \
--alarm-actions "arn:aws:autoscaling:us-east-1:xxxxxxx:scalingPolicy:xxxxxx/Test-Step-scaling-policy-ScaleOut"

Note: Replace CLUSTER_NAME with your cluster, SERVICE_NAME with your service, and REGION with your Region.

Application Auto Scaling blocks all scaling in activities during scaling out activities until scaling out completes. If it doesn't meet the count that you configured, then Amazon ECS times out the InProgress scaling out activity. Reasons for a timeout include image or networking issues. If CloudWatch metrics scale in during the scale out cooldown period and the scale out activity completes, then Application Auto Scaling runs the scaling in activity.

Related information

Troubleshooting service auto scaling in Amazon ECS

Automatically scale your Amazon ECS service

DescribeScalingActivities

application-autoscaling

2 Comments

Some information is missing making it hard to use this article:

On this line:

You must choose a scale-out and scale-in cooldown period. The hyperlink to the cooldown period is broken.

Also, the "Target tracking policy" and the "Step scaling policy" are missing from the text.

replied a year ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT
replied a year ago