Launch templates and Auto Scaling groups

Intermediate · 12 min

Run a self-healing fleet: versioned launch templates, group capacity, health checks, target tracking scaling, warmup, lifecycle hooks and refreshes.

Before thisInstance metadata and user dataEC2 pricing, Spot and Savings Plans

Tiny Technical Tutorials · 12 min

Launch templates and Auto Scaling groups

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Tiny Technical TutorialsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A launch template is a versioned description of how to launch an instance. An Auto Scaling group keeps a number of them running across zones.
  • The group holds desired capacity between a minimum and maximum and replaces instances that fail, even if you never scale.
  • By default only EC2 status checks count. Turn on load balancer health checks so a broken application gets replaced.
  • Target tracking adds and removes instances to hold a metric near a target. Set an instance warmup so new instances don't distort it.
  • Instances must be disposable: build them from an image and user data, keep state elsewhere and drain on termination.
  • Roll out a new template version with an instance refresh, with health thresholds and rollback.

From a server to a supply of servers

One hand-built server has two problems. When it dies at 3 a.m., someone has to rebuild it. When traffic triples, someone has to add more. Both are the same missing thing: a way to say “keep N of these running” and let software do it.

That takes a launch template, which says what one instance looks like, and an Auto Scaling group (ASG), which says how many and where. The idea is a reconcile loop: compare desired with actual, fix the difference. The general theory is in autoscaling and capacity planning.

The template

aws ec2 create-launch-template --launch-template-name web \
  --launch-template-data '{"ImageId":"ami-...","InstanceType":"t3.micro",
     "MetadataOptions":{"HttpTokens":"required"},
     "IamInstanceProfile":{"Name":"web-app"}}'

Templates are versioned: changes make version 2, and version 1 stays. Groups can pin a number or follow $Latest or $Default. Pin it, so a change is a decision. The older launch configurations are a dead end: accounts created since October 2024 cannot create them.

The group

aws autoscaling create-auto-scaling-group --auto-scaling-group-name web \
  --launch-template 'LaunchTemplateName=web,Version=1' \
  --min-size 2 --max-size 6 --desired-capacity 2 \
  --vpc-zone-identifier "subnet-a,subnet-b" \
  --health-check-type ELB --health-check-grace-period 120

Give it subnets in several zones and it balances across them. Attach a target group and instances register and deregister by themselves.

Terminate one by hand and the group notices it has one fewer than desired and launches another. This alone is worth having, with min = max = desired.

Health

The default check is EC2 status: is the machine running? That says nothing about your application. Turn on ELB health checks so the load balancer’s verdict counts too. The grace period keeps a booting instance from being judged too early; it defaults to 300 seconds in the console and 0 from the CLI or SDKs, a difference that causes endless launch-and-kill loops.

Make the health check honest but narrow. If it tests a shared database, a database blip marks every instance unhealthy at once.

Scaling

Policy Idea
Target tracking hold a metric near a value, like a thermostat
Step add or remove set amounts at alarm thresholds
Scheduled change capacity at known times
Predictive forecast from past patterns and scale ahead

Start with target tracking:

aws autoscaling put-scaling-policy --auto-scaling-group-name web \
  --policy-name cpu50 --policy-type TargetTrackingScaling \
  --target-tracking-configuration '{"TargetValue":50.0,
    "PredefinedMetricSpecification":{"PredefinedMetricType":"ASGAverageCPUUtilization"}}'

Pick a metric that falls when you add instances, such as average CPU or requests per target. EC2 publishes metrics every five minutes by default; enable detailed monitoring for one minute. Set the default instance warmup (off by default) so a booting instance’s numbers are not counted. The group is quick to scale out and cautious to scale in.

Designing for replacement

The group will terminate instances: on failure, on scale in, during refreshes and when Spot capacity is reclaimed. So nothing that matters may live only on one of them. Sessions belong in a shared store, uploads in object storage, logs shipped off the host. A useful test is to terminate a random instance on a quiet afternoon. If anyone has to do something by hand afterwards, you have found the work still to be done.

Changing the fleet

Running instances don’t pick up new templates. An instance refresh replaces them in batches, keeping a healthy percentage you choose, with optional checkpoints and rollback. A maximum instance lifetime forces regular replacement, which keeps images current.

Lifecycle hooks pause an instance on the way in or out so you can finish bootstrapping or drain work; the default wait is up to an hour. A warm pool keeps pre-initialised stopped instances for faster scale-out. A mixed instances policy combines several types with On-Demand and Spot.

About the video

The embed is Tiny Technical Tutorials’ “Create an Auto Scaling Group that Works with an Application Load Balancer”, July 2022, 12 minutes. Its chapters review the load balancer setup, create the group and a launch template, view activity and simulate an unhealthy instance. It builds on the channel’s earlier load balancer video and predates some current features. I judged it from chapters and description, since transcripts couldn’t be fetched. The longer demo in deeper is more recent.

What was run: the template, group and policy calls ran against moto, a local emulator. It replaced a terminated instance instantly and put both instances in one zone despite two subnets, so its scheduling is not AWS’s. No metrics, health checks or scaling happened. Behaviour and defaults are from AWS documentation, checked in November 2026.

Where this fits

A fleet needs one front door that knows which instances are healthy: Elastic Load Balancing.

Check yourself

  1. 01Your web process crashes but the instance passes EC2 status checks. Will the group replace it?

    Not with the default health checks. Enable ELB health checks on the group, so the target group's failing check marks the instance unhealthy.

  2. 02You terminate an instance in a group by hand to save money. What happens?

    The group launches a replacement to get back to desired capacity. Lower the desired capacity instead.

  3. 03Instances take five minutes to start, and the group launches far too many during a spike. What helps?

    Set the default instance warmup to cover the start-up time, and shorten start-up with a pre-built image or a warm pool.

  4. 04You publish template version 2 while the group points at version 1. Do instances change?

    No. The group keeps launching version 1. Point it at version 2 and refresh, and only then are existing instances replaced.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark