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.

Short description

To increase or decrease your task count, integrate Amazon ECS on Fargate with Amazon CloudWatch alarms and AWS Application Auto Scaling. Then, you can use CloudWatch metrics to configure your CloudWatch alarms.

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

Resolution

Note: The AWS Identity and Access Management (IAM) user that accesses service auto scaling settings must have the appropriate permissions for the services that support dynamic scaling. For more information, see IAM permissions required for service auto scaling.

Amazon ECS console

To configure Amazon ECS service auto scaling on Fargate, use the Amazon ECS console to configure service auto scaling. Then, add the scaling policy to the service.

Configure service auto scaling

When you create or update a service in the Amazon ECS console, under Service auto scaling, complete the following steps:

  1. Select the Use service auto scaling option.
  2. For Minimum number of tasks, enter the lowest number of tasks that you want service auto scaling to use.
  3. 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.

Add a scaling policy to the service

Based on your requirements, choose either a target tracking policy or step scaling policy:

  • Target tracking policies: Complete the steps under To configure target scaling policies for your Amazon ECS service using the Amazon ECS console on Target tracking scaling policies.
  • Step scaling policies: Complete the steps under To configure step scaling policies for your Amazon ECS service using the Amazon ECS console on Step scaling policies.

AWS CLI

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

To configure Amazon ECS service auto scaling on Fargate, use the AWS CLI to configure service auto scaling. Then, create a target tracking or step scaling policy to scale based on the values that you set.

Configure service auto scaling

To register your Fargate service as a scalable target with Application Auto Scaling, run the following command:

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

Create a target tracking or step scaling policy

Create a target tracking or step scaling policy for the scalable target. In the following example, your Fargate service is the scalable target.

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 based on the target values that you choose.

Step scaling policy:

  1. Create a step scaling policy with the required step adjustments for your scale out activity:

    aws application-autoscaling put-scaling-policy \
    --service-namespace ecs --scalable-dimension ecs:service:DesiredCount \
    --resource-id service/<cluster>/<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 as an alarm action to your 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=<cluster> Name=ServiceName,Value=<service-name> --unit Percent \
    --alarm-actions "arn:aws:autoscaling:us-east-1:xxxxxxx:scalingPolicy:xxxxxx/Test-Step-scaling-policy-ScaleOut"
  3. Repeat the steps to create a second step scaling policy and CloudWatch alarm for the scale in activity.

Note: When CloudWatch is scaling out, all scale in activity that CloudWatch starts is blocked until the scale out activity completes. The InProgress scale out activity times out when Amazon ECS doesn't meet the configured count that Application Auto Scaling set. Common reasons for a timeout include image or networking issues. If CloudWatch metrics start to scale in during the scale-out cooldown and a scale-out activity completes, then a scale-in activity is run.

Related information

Troubleshooting service auto scaling

Service auto scaling

DescribeScalingActivities

application-autoscaling

AWS OFFICIAL
AWS OFFICIALUpdated a month ago
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 2 months ago

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

profile pictureAWS
MODERATOR
replied 2 months ago