Auto Scaling & Load Balancing
Traffic is rarely steady. A results portal is quiet for months and then overwhelmed on results day; a shop is busiest on Black Friday. Elastic Load Balancing spreads requests across many servers, and Amazon EC2 Auto Scaling adds or removes servers automatically, so your app stays fast when busy and cheap when quiet.
Scaling up vs scaling out
- Vertical scaling (scaling up): move to a bigger instance type. Simple, but there is a ceiling, it usually needs a restart, and one server is still a single point of failure.
- Horizontal scaling (scaling out): add more instances of the same size behind a load balancer. There is no practical ceiling, and losing one server does not take the app down.
Cloud architectures favour scaling out. That works best when servers are stateless: they keep no session data or uploads on their own disk, but store them in shared services such as a database, S3 or a cache, so any server can handle any request.
Elastic Load Balancing
| Type | Works at | Use it for |
|---|---|---|
| Application Load Balancer (ALB) | HTTP and HTTPS (layer 7) | Websites and APIs; can route by path (/api/*) or host name |
| Network Load Balancer (NLB) | TCP, UDP and TLS (layer 4) | Very high throughput, static IP addresses, non-HTTP protocols |
| Gateway Load Balancer | Network packets (layer 3) | Inserting third-party firewalls and inspection appliances |
An ALB has three main parts:
- Listeners wait for connections on a port, such as 443 for HTTPS, using a certificate from AWS Certificate Manager.
- Rules decide where each request goes, for example
/api/*to the API servers and everything else to the web servers. - Target groups are the sets of servers (or containers, IPs or Lambda functions) that receive requests. The load balancer runs health checks, such as requesting
/healthevery 30 seconds, and stops sending traffic to targets that fail.
Auto Scaling groups
An Auto Scaling group (ASG) keeps a fleet of identical instances running. You give it:
- A launch template: the AMI, instance type, security group, IAM role and user data for new instances.
- Subnets in two or more Availability Zones; the ASG spreads instances evenly across them.
- Three numbers: minimum (never fewer), maximum (never more, which caps your bill) and desired capacity (how many right now).
- Scaling policies that change the desired capacity.
The ASG also heals: if an instance fails its health check, it is terminated and replaced automatically.
Types of scaling policy
- Target tracking: "keep average CPU at 50%". The ASG adds instances when CPU rises above the target and removes them when it falls. This is the easiest place to start.
- Step scaling: add or remove a set number of instances when a CloudWatch alarm crosses thresholds.
- Scheduled scaling: change capacity at known times, such as before results are released.
- Predictive scaling: uses past traffic patterns to scale ahead of regular daily or weekly peaks.
Example: preparing a results portal
Assume a launch template results-web, two subnets in different AZs and an ALB target group already exist. Create the group (the IDs and ARN are placeholders):
aws autoscaling create-auto-scaling-group \
--auto-scaling-group-name results-asg \
--launch-template 'LaunchTemplateName=results-web,Version=$Latest' \
--min-size 2 --max-size 8 --desired-capacity 2 \
--vpc-zone-identifier "subnet-0aaa1111bbbb2222c,subnet-0ddd3333eeee4444f" \
--target-group-arns arn:aws:elasticloadbalancing:eu-west-1:123456789012:targetgroup/results-tg/0123456789abcdef \
--health-check-type ELB --health-check-grace-period 120
Notice the single quotes around the launch template argument. In bash, $Latest inside double quotes would be treated as an (empty) shell variable, and AWS would receive Version= with no value. Single quotes pass the literal text $Latest, which means "always use the newest template version".
Add a target tracking policy with a small JSON file, cpu50.json:
{
"PredefinedMetricSpecification": {
"PredefinedMetricType": "ASGAverageCPUUtilization"
},
"TargetValue": 50.0
}
aws autoscaling put-scaling-policy --auto-scaling-group-name results-asg \
--policy-name keep-cpu-50 --policy-type TargetTrackingScaling \
--target-tracking-configuration file://cpu50.json
And schedule extra capacity before results are released (times are in UTC; 05:00 UTC is 08:00 in Nairobi):
aws autoscaling put-scheduled-update-group-action --auto-scaling-group-name results-asg \
--scheduled-action-name results-morning \
--start-time "2026-12-10T05:00:00Z" \
--min-size 6 --max-size 20 --desired-capacity 10
Remember to schedule a second action that brings the numbers back down afterwards.
Costs and clean-up
A load balancer is billed for every hour it exists, even with no traffic, plus usage-based charges. And if you terminate an instance that belongs to an Auto Scaling group, the group simply launches a replacement. To clean up, delete the Auto Scaling group (which terminates its instances), then delete the load balancer and target group.
Key points
- Scale out with more stateless instances rather than up with one bigger server.
- An ALB routes HTTP(S) requests to healthy targets using listeners, rules and target groups.
- An Auto Scaling group keeps between min and max instances across several AZs and replaces unhealthy ones.
- Target tracking is the simplest policy; add scheduled scaling for known peaks.
- Delete the ASG first, then the load balancer, to stop charges.
Tip: use the ← and → keys to move between pages.
