Amazon EC2 Auto Scaling is the AWS service that adds and removes EC2 instances based on what an application needs, and it is the way to scale horizontally on EC2. An EC2 instance is a virtual computer you rent from AWS by the hour, where your application runs. For example, an online store can have three instances in the morning and ten on the night of a big sale, and Auto Scaling makes that change without anyone asking for it by hand.
There are two ways to give an application more capacity, that is, to let it serve more people, and they differ in what happens when something fails. Scaling vertically means replacing the server with a larger one, so there is still only one and, if it fails, everything goes down. Scaling horizontally means adding more identical servers, so a failure only takes away part of the capacity. For example, if the store runs on four identical instances and one fails, the other three keep selling.
How does an Auto Scaling group work?
The instances that Auto Scaling manages live in an Auto Scaling group, which is defined by three numbers:
- The minimum, which is the number of instances the group never goes below.
- The maximum, which is the number of instances the group never goes above.
- The desired capacity, which is the number of instances the group wants to have right now and which always stays between the minimum and the maximum.

Scaling policies, which are the rules that decide when to grow or shrink, move the desired capacity, for example to 4, always between a minimum of 2 and a maximum of 8. With those numbers, the store never goes below 2 instances even if nobody buys anything, and never goes above 8 even if a lot of visits arrive, which also puts a cap on what it pays.
Each instance the group launches comes from a launch template, a sheet that says what every new instance has to look like. The launch template defines the AMI, the instance type (its size), the security group, the role (the permissions the instance has inside AWS) and the user data. For example, thanks to the launch template, the store's tenth instance starts exactly like the first one, with the same program and the same rules.
Distribution across zones
An Auto Scaling group works with several Availability Zones, which are groups of separate AWS buildings inside a Region, and when it launches instances it tries to distribute them evenly, choosing the zone that has the fewest. If a zone fails, the group launches instances in the others, and when the zone comes back, it rebalances, that is, it evens out the number of instances per zone again. Also, if an instance is considered unhealthy, the group replaces it to keep the desired capacity. For example, if the store has 4 instances in two zones and one zone goes down, the group launches 2 in the remaining zone, and when the failed zone comes back it spreads them 2 and 2 again.
Group health checks
A health check is a periodic test that asks whether an instance is healthy. By default, the group only uses EC2 health checks, which check whether the instance is on and whether its machine responds. With that, an instance can be in running (on) with the program that delivers the website hung, and even though the load balancer, the service that spreads visits across instances, takes it out of traffic, the group sees it as healthy and leaves it alone. If you turn on the Elastic Load Balancing health check in the group, the group also replaces the instances that the load balancer reports as unhealthy. For example, if the store's program freezes on one instance, the load balancer detects it because the page doesn't respond, and with this health check turned on the group swaps that instance for a new one.
Stateless applications
Horizontal scaling only works if the application can handle an instance appearing or disappearing at any moment. That is why the reliability pillar (REL05-BP06) asks for stateless systems, where nothing depends on data stored on a server's disk or in its memory between one request and the next. A session is what the application remembers about a user while they use it, such as that they already signed in or what they have in their cart. If the application has sessions, they are stored outside the server, for example in Amazon ElastiCache or in Amazon DynamoDB, two AWS services that store data so any instance can read it. That way, if the group removes the instance that was serving a store customer, another instance reads their cart and the customer doesn't lose what they had picked.
Which scaling policies exist?

A scaling policy is the rule that decides when the group's desired capacity changes. These policies work with metrics, which are numbers AWS measures about your resources, such as how busy an instance is. Auto Scaling has four types of policy:
- Target tracking, where you choose a metric and a target value, for example 50% average CPU for the group, and the policy creates the alarms and adjusts the capacity to stay close to that value.
- Step scaling, where you create the alarm, an alert that fires when a metric crosses a limit, and define steps based on how far the metric went past it, with more instances for larger breaches. For example, the store adds one instance if the CPU goes a little over the limit and three if it goes far over it.
- Scheduled scaling, which uses actions with a date and time, one-time or recurring, that change the desired capacity and, if you want, the minimum and the maximum, and which is meant for changes you know about in advance. For example, the store raises its capacity the night before a special sale it has already announced.
- Predictive scaling, which analyzes history to find daily or weekly patterns and launches capacity before the load arrives, and which is meant for cyclical traffic, the kind that repeats the same way every day or every week, and for applications that take a long time to start. For example, if the store gets more visits every day at lunchtime, predictive scaling adds instances a little before.
AWS says it "strongly" recommends target tracking for metrics such as average CPU or the load balancer's requests per target. The predefined target tracking metrics include ASGAverageCPUUtilization (the group's average CPU), ASGAverageNetworkIn and ASGAverageNetworkOut (the data coming in and going out over the network) and ALBRequestCountPerTarget (the requests each instance receives from the load balancer).
The target tracking metric
The target tracking metric has to change in proportion to capacity, so if you double the instances, the metric should drop by half. Average CPU meets this condition. For example, if 4 of the store's instances are at 80% CPU and the group raises them to 8, each one ends up close to 40%, because the same work is spread across more of them. On the other hand, the total number of requests the load balancer receives doesn't meet it, because it doesn't drop when there are more instances, and neither does the total number of visible messages in an SQS queue.
How do you scale the consumers of a queue?
Because the total number of messages in a queue doesn't work for target tracking, the Auto Scaling documentation proposes another metric for EC2 instances that process messages from an SQS queue. Those instances are called consumers, because they take the messages from the queue and do the work they ask for. This metric is called backlog per instance, and it is the number of visible messages divided by the instances in service.

For example, with 1,500 messages and 10 instances, the backlog is 150 messages per instance. If each message takes 0.1 seconds and the acceptable latency is 10 seconds, each instance can have 100 messages waiting, and that is the target of the target tracking policy. Because there are 150, the group grows until it is back to 100 messages per instance, that is, up to 15 instances.
cloud path tip: the backlog target is calculated by dividing the acceptable latency by the time one message takes. AWS recommends calculating the metric with metric math inside the policy, which is cheaper than publishing it yourself.
On the exam
Auto Scaling questions usually describe a symptom and ask for the adjustment that fixes it. When "users lose their session when the group removes instances", the answer is to move the session to ElastiCache or DynamoDB, and when "the load balancer marks it unhealthy and the group doesn't replace it", the answer is the ELB health check in the group. To choose the policy, an "event with a known date" points to scheduled scaling, and a "daily pattern" with a "slow startup" points to predictive. Finally, if the group scales the consumers of a queue, the correct metric is the backlog per instance and not the total number of messages. For example, if a question says the store's customers lose their cart when the group shrinks, the correct option stores the session outside the instances.
Full account and new experience account
Amazon EC2 Auto Scaling is in the free plan of the new experience account, the simplified way to open an AWS account, and in it you can spread the group across the zones of us-east-2. However, in that account you can't mix Spot Instances into the group, because its policies deny launching Spot Instances, something you can do in a full account. For example, in the new experience the store's group can only use regular instances.
Next