Auto Scaling Groups and Launch Templates

Keep the right number of healthy instances running automatically with launch templates, scaling policies and instance refresh.

Intermediate · about 4 hours · difficulty 3/5

Learn first: Elastic Load Balancing: ALB and NLB

An Auto Scaling group (ASG) keeps a fleet of identical instances at the size you want, replaces unhealthy ones and grows or shrinks with demand. Azure uses Virtual Machine Scale Sets; GCP uses Managed Instance Groups.

Building blocks

  • A launch template defines AMI, instance type, security groups, instance role and user data.
  • The ASG sets minimum, desired and maximum capacity and the subnets (AZs) to spread across.
  • Attach the ASG to an ALB target group and enable ELB health checks so instances failing the app check are replaced.

Scaling policies

  • Target tracking: "keep average CPU at 50 percent" or "1000 requests per target". The simplest and usually best.
  • Step scaling: add capacity in steps based on alarm thresholds.
  • Scheduled scaling: scale up before the 9 am exam result rush, down at night.
  • Predictive scaling: uses history to scale ahead of daily patterns.

Operating ASGs

  • Instance refresh rolls out a new launch template version gradually.
  • Mixed instances policy combines On-Demand and Spot across several instance types.
  • Lifecycle hooks pause launches or terminations to run setup or drain work.
  • Treat instances as cattle, not pets: no manual changes, because any instance can be replaced at any time.

Pitfalls

  • Storing user uploads or sessions on the instance disk, which vanish on scale-in.
  • Scaling on CPU when the real bottleneck is the database.
  • Cooldowns too short, causing the group to flap up and down.

You are ready to move on when your ASG replaces a terminated instance on its own and scales out under a load test.

Key concepts

  • Launch templates
  • Min, desired, max capacity
  • Target tracking scaling
  • Scheduled and predictive scaling
  • ELB health checks in ASGs
  • Instance refresh
  • Mixed instances and Spot
  • Lifecycle hooks

Mini project

Create an ASG of free-tier instances (min 1, max 3) behind an ALB with a target tracking policy at 40 percent CPU. Run a stress tool on one instance and watch it scale out, then terminate an instance manually and watch it get replaced. Set the ASG to zero and delete it, the ALB and the launch template within the same session.

Check yourself

Your app stores uploaded profile photos on the local disk. Why does this break once you add an Auto Scaling group, and where should the photos go?

Free resources