Use target tracking to scale container deployments with Amazon ECS
About
AWS Application Auto Scaling implements automated scaling policies and rules across many AWS services, including Amazon ECS.
Target tracking is a scaling mode in which Application Auto Scaling automatically learns how to adjust your scale to meet your expectation that a target metric will stay at a specified target. Target tracking works best with larger services, where there is a linear relationship between scaling and metrics.
Architecture
This is how auto scaling works:
- Your application container uses CPU, memory, and other computing resources
- An ECS agent running on the same EC2 instance or AWS Fargate task gathers telemetry from your application container's usage statistics
- Telemetry is stored in AWS CloudWatch metrics
- AWS Application Auto Scaling triggers scaling rules based on CloudWatch metrics
- Amazon ECS receives an
UpdateService
call from AWS Application Auto Scaling, which adjusts the desired count for the service - Amazon ECS launches additional copies of your application container on EC2 or AWS Fargate, or scales in the service to reduce the number of copies of your application, when there is no utilization.
CloudFormation Template
The following template automatically sets up CloudWatch alarms, auto scaling policies, and attaches them to an ECS service.
AWSTemplateFormatVersion: '2010-09-09'
Description: Add target tracking scaling rules for an ECS service
Parameters:
ClusterName:
Type: String
Default: default
Description: The cluster that is running the service you want to scale
ServiceName:
Type: String
Default: nginx
Description: The name of the service to scale
Resources:
# Role that Application Auto Scaling will use to interact with
# CloudWatch and Amazon ECS
AutoscalingRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: [application-autoscaling.amazonaws.com]
Action: ['sts:AssumeRole']
Path: /
Policies:
- PolicyName: service-autoscaling
PolicyDocument:
Statement:
- Effect: Allow
Action:
- 'application-autoscaling:*'
- 'cloudwatch:DescribeAlarms'
- 'cloudwatch:PutMetricAlarm'
- 'ecs:DescribeServices'
- 'ecs:UpdateService'
Resource: '*'
# Enable autoscaling for the service
ScalableTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
ServiceNamespace: 'ecs'
ScalableDimension: 'ecs:service:DesiredCount'
ResourceId: !Sub 'service/${ClusterName}/${ServiceName}'
MinCapacity: 2
MaxCapacity: 10
RoleARN: !GetAtt AutoscalingRole.Arn
# Create scaling policies that describe how to scale the service up and down.
ScalingPolicy:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
DependsOn: ScalableTarget
Properties:
PolicyName: !Sub scale-${ClusterName}-${ServiceName}
PolicyType: TargetTrackingScaling
ResourceId: !Sub 'service/${ClusterName}/${ServiceName}'
ScalableDimension: 'ecs:service:DesiredCount'
ServiceNamespace: 'ecs'
TargetTrackingScalingPolicyConfiguration:
TargetValue: 70
DisableScaleIn: false
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
The template requires the following input parameters:
ClusterName
- The name of the ECS cluster that runs the service you would like to scaleServiceName
- The name of the service you want to scale
Things to note in this template:
ScalingPolicy.Properties.TargetTrackingScalingPolicyConfiguration
- This controls the metric to base scaling off of, and what target utilization to try to maintain.
There are two valid ECS specific values for PredefinedMetricType
:
ECSServiceAverageCPUUtilization
- Monitor the CPU utilizationECSServiceAverageMemoryUtilization
- Monitor the memory utilization
WARNING
Be careful about scaling based on memory utilization because with most application runtime frameworks memory is not correlated with utilization. Most applications don't release memory after load decreases. Instead they keep memory allocated in case they need to use it again. For this reason average memory utilization does not work with target tracking scaling because there is not a linear relationship between load and memory utilization.
Usage
You can deploy the template via the AWS CloudFormation web console, or by running an AWS CLI command similar to this:
aws cloudformation deploy \
--stack-name scale-my-service-name \
--template-file target-tracking-scale.yml \
--capabilities CAPABILITY_IAM \
--parameter-overrides ClusterName=development ServiceName=my-web-service
Cleanup
You can delete the auto scaling configuration by tearing down the CloudFormation stack with:
aws cloudformation delete-stack --stack-name scale-my-service-name
See Also
- If you are running a smaller service, or want to have more precise control over how your service scales in response to load then consider setting up a step scaling policy.