Sub topic 18
Scaling other services with Application Auto Scaling
This chapter assumes you know how an EC2 Auto Scaling group uses a target tracking policy and CloudWatch alarms to change capacity. It also assumes you can run AWS CLI commands. The goal is to apply the same ideas to services that have no launch template or instances of their own.
What Application Auto Scaling is
EC2 Auto Scaling manages a group of instances that you describe with a launch template. Application Auto Scaling is a separate API (application-autoscaling). It changes a numeric capacity setting on another AWS service. The service itself does the work of adding or removing capacity.
Supported resources include:
- Amazon ECS service desired task count
- DynamoDB table and global secondary index read and write capacity (provisioned mode only)
- Aurora replica count
- Lambda provisioned concurrency
- SageMaker endpoint variants
- Spot Fleet target capacity
- Several others, such as EMR, ElastiCache, Amazon Keyspaces and MSK storage
You can list every namespace in the [Application Auto Scaling documentation](https://docs.aws.amazon.com/autoscaling/application/userguide/). The list grows over time.
The three identifiers
Every resource is addressed by three values:
| Field | Meaning | ECS example |
|---|---|---|
| Service namespace | Which service owns the resource | `ecs` |
| Resource ID | Which resource, in a service-specific format | `service/my-cluster/web-service` |
| Scalable dimension | Which numeric property to change | `ecs:service:DesiredCount` |
A resource and dimension pair is called a scalable target. You must register it before you attach any policy. Registration also sets the minimum and maximum capacity, which work like the min and max of an Auto Scaling group.
How the pieces connect
Example: scaling an ECS service
Assume a cluster named my-cluster with a service named web-service. First register the target:
aws application-autoscaling register-scalable-target \
--service-namespace ecs \
--resource-id service/my-cluster/web-service \
--scalable-dimension ecs:service:DesiredCount \
--min-capacity 2 \
--max-capacity 10The first call creates the service-linked role AWSServiceRoleForApplicationAutoScaling_ECSService if it does not exist. Next, save this target tracking configuration as ecs-policy.json:
{
"TargetValue": 60.0,
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ECSServiceAverageCPUUtilization"
},
"ScaleOutCooldown": 60,
"ScaleInCooldown": 300
}Then attach it:
aws application-autoscaling put-scaling-policy \
--service-namespace ecs \
--resource-id service/my-cluster/web-service \
--scalable-dimension ecs:service:DesiredCount \
--policy-name cpu-60 \
--policy-type TargetTrackingScaling \
--target-tracking-scaling-policy-configuration file://ecs-policy.jsonThe service now creates and manages the CloudWatch alarms. Do not edit those alarms by hand, because the service can overwrite your changes.
The cooldown fields are the main difference from EC2 target tracking. EC2 Auto Scaling uses instance warmup. Here you set ScaleOutCooldown and ScaleInCooldown in seconds on the policy. A long scale-in cooldown with a short scale-out cooldown is a common pattern. It adds capacity quickly and removes it slowly.
Choosing metrics
Predefined metric types depend on the service. Common ones are:
ECSServiceAverageCPUUtilizationandECSServiceAverageMemoryUtilizationfor ECSALBRequestCountPerTargetfor ECS services behind an Application Load Balancer, with aResourceLabelbuilt the same way as in the EC2 exampleDynamoDBReadCapacityUtilizationandDynamoDBWriteCapacityUtilizationfor DynamoDBLambdaProvisionedConcurrencyUtilizationfor Lambda
If no predefined metric fits, use a CustomizedMetricSpecification that points at any CloudWatch metric. The metric must change in inverse proportion to capacity. Doubling the tasks should roughly halve the value. Queue depth alone does not meet this rule. A backlog per task figure does.
Scheduled actions
Scheduled actions change the minimum and maximum at set times. They suit predictable load such as a weekday morning peak:
aws application-autoscaling put-scheduled-action \
--service-namespace ecs \
--resource-id service/my-cluster/web-service \
--scalable-dimension ecs:service:DesiredCount \
--scheduled-action-name weekday-morning \
--schedule "cron(0 8 ? * MON-FRI *)" \
--timezone "Europe/London" \
--scalable-target-action MinCapacity=4,MaxCapacity=10At 08:00 on weekdays the floor rises to 4. Target tracking still adjusts capacity between 4 and 10 afterward. If current capacity is below the new minimum, the service scales up to meet it.
Gotchas
- DynamoDB on-demand tables do not use this. Only provisioned capacity is scaled, and scaling is not instant. Sudden bursts can still throttle before capacity catches up.
- Out-of-range capacity gets corrected. If you set an ECS desired count above the registered maximum, the next evaluation moves it back inside the range.
- Scale-in can be disabled per policy. Set
DisableScaleIntotruein the target tracking configuration to let the policy add capacity but never remove it. - Multiple policies can conflict. Scale-out wins if any policy asks for more capacity. Scale-in happens only when all target tracking policies agree.
- Deregistering removes automation, not the resource.
deregister-scalable-targetdeletes the policies and scheduled actions. The resource keeps its current capacity.
Checking what happened
When scaling does not behave as expected, read the scaling activity log. It records each change and the reason for it:
import boto3
client = boto3.client("application-autoscaling")
def print_scaling_activities(resource_id):
response = client.describe_scaling_activities(
ServiceNamespace="ecs",
ResourceId=resource_id,
ScalableDimension="ecs:service:DesiredCount",
MaxResults=10,
)
for activity in response["ScalingActivities"]:
print(activity["StartTime"], activity["StatusCode"], activity["Description"])
print(" cause:", activity["Cause"])
print_scaling_activities("service/my-cluster/web-service")Typical findings include a metric with no data points, a target already at its maximum, or a service-linked role that was removed.
Exercise
A DynamoDB table uses provisioned capacity with a target tracking policy on read utilization at 70 percent. You register the table with min 5 and max 100, then later switch the table to on-demand mode. What happens to the scaling policy, and what would you register to scale a different dimension such as write capacity?
Show answer
The policy no longer has any effect, because Application Auto Scaling scales only provisioned capacity and on-demand tables have no capacity setting to change. To scale writes on a provisioned table, register a second scalable target with the dimension dynamodb:table:WriteCapacityUnits and attach a separate policy using DynamoDBWriteCapacityUtilization. Each dimension has its own min, max and policies.