You have a load balancer distributing traffic among several servers. But one question remains: who creates those servers and how many should there be? If you set 10 fixed servers, you overpay at night when there’s little traffic; if you set 2, you get overwhelmed at peak hours. The solution is the Auto Scaling Group (ASG): the component that automatically creates and removes servers according to demand. This is the other half of an elastic architecture.

The problem: demand is not constant

The traffic of almost any application rises and falls:

Traffic of an online store throughout the day:

  High │           ██████
       │        ███      ███
       │      ██            ██
  Low  │ ████                ████
       └────────────────────────────
        00h    12h    18h    24h

If you size for the peak, you waste money most of the time. If you size for the average, you crash at the peaks. The answer is not to have a fixed number: adjust the number of servers in real time. That’s autoscaling, and it’s one of the great advantages of the cloud we saw in Chapter 1 (elasticity).

What is an Auto Scaling Group

An Auto Scaling Group (ASG) is a group of EC2 instances that AWS automatically maintains and adjusts. You define some limits and rules, and the ASG takes care of creating or destroying servers to comply with them.

It is configured with three key numbers:

┌─────────── Auto Scaling Group ───────────┐
│  Minimum:  2 servers  (never less)       │
│  Desired:  3 servers  (right now)        │
│  Maximum: 10 servers  (never more)       │
└───────────────────────────────────────────┘
  • Minimum: the number that will always be there, even if there’s no traffic (guarantees availability).
  • Desired: how many there are at this moment; this is what the ASG adjusts.
  • Maximum: the cap, so a spike (or an error) doesn’t send your bill skyrocketing.

Self-healing: a huge advantage

The ASG doesn’t just scale: it also self-heals. If an instance goes down or fails its health check, the ASG detects it and creates a new one to maintain the desired number.

Desired = 3, but one server goes down:

  Server 1 ✓   Server 2 ✓   Server 3 ✗ (down)
                                      │
                          The ASG detects it and...
                                      ▼
  Server 1 ✓   Server 2 ✓   Server 4 ✓ (new, just created)

This is extremely powerful: combined with the load balancer from the previous subchapter, your application heals itself. If a server dies at 3 a.m., nobody has to get up: the ASG creates a new one and the load balancer starts using it as soon as it’s healthy. This is where user_data from subchapter 12.2 makes sense: every new server self-configures at birth.

Scaling policies: when to create or remove servers

How does the ASG decide when to scale? Through scaling policies based on metrics (CloudWatch data, which we’ll see in Chapter 24). The most common is CPU usage.

Target Tracking: the simplest and recommended

You tell the ASG a target and it does what’s necessary to maintain it. For example: “keep average CPU usage at 50%.”

Policy: keep average CPU at 50%

  CPU rises to 80%  →  ASG ADDS servers  → average CPU drops
  CPU drops to 20%  →  ASG REMOVES servers  → average CPU rises

It’s like a car’s climate control: you say “keep it at 22 degrees” and it turns the air on or off as needed. You don’t worry about the details. For its simplicity, it’s the recommended policy to start with.

Other policies (for reference)

Policy How it works When to use it
Target Tracking Keeps a metric at a target value Default option, the easiest
Step Scaling Adds/removes N servers based on metric steps Finer control over scaling
Scheduled Scales according to a set schedule Predictable spikes (e.g. sales at 9am)

An example of scheduled scaling: a ticket sales website knows that every Monday at 10:00 it releases tickets and gets a flood of users. It schedules the ASG to scale up to 20 servers at 9:55, before people arrive, instead of waiting for CPU to rise.

Metrics: what the decision is based on

Policies react to metrics. The most common:

  • CPU usage: the most common; high CPU = overloaded servers.
  • Number of requests per server: very useful with a load balancer (requests from the Target Group).
  • Memory or network usage.
  • Custom metrics: for example, the number of messages in a queue (we’ll see this with SQS, Chapter 15).

The complete set: load balancer + ASG

Combining the three subchapters, this is the classic elastic architecture:

              Users
                 │
         ┌───────▼────────┐
         │  Load Balancer │  (distributes traffic, subchap. 13.1-13.2)
         └───────┬────────┘
        ┌────────┼────────┐
        ▼        ▼        ▼
   Server   Server   Server   ← Auto Scaling Group
   (the ASG creates/destroys according to demand and repairs them)

The load balancer distributes among the servers present; the ASG adjusts how many there are and keeps them healthy. Together they provide an application that scales and heals itself.

What you should remember

  • An Auto Scaling Group (ASG) creates and removes servers automatically according to demand, within limits: minimum, desired, and maximum.
  • The ASG also self-heals: if a server fails, it creates a new one to maintain the desired number (this is where user_data that self-configures each server shines).
  • Scaling policies decide when to scale based on metrics (the most common is CPU usage).
  • Target Tracking (“keep CPU at 50%”) is the simplest and recommended policy to start with; there are also Step and Scheduled (scheduled scaling for predictable spikes).
  • Load Balancer + ASG = architecture that scales and heals itself.

In the last subchapter of the chapter, we’ll see two advanced techniques to fine-tune autoscaling: warm pools and lifecycle hooks.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved