Auto Scaling Groups and Launch Templates

Topics Covered

Launch Templates

What a launch template contains

Versions and the default

The AMI is the unit of change

User data and cloud-init

Metadata service settings

Instance requirements instead of types

Tags and propagation

Testing a template before a group uses it

Image pipelines

An Auto Scaling group is a promise that a certain number of healthy instances will exist. To keep that promise, it needs two things: a description of what an instance is, and a rule for how many there should be. The description is the launch template. The rule is the group's capacity settings and scaling policies. This lesson covers both, and then the parts that decide whether the promise holds up in production: health checks, lifecycle hooks, warm pools and instance refresh.

What a launch template contains

A launch template records everything needed to start an instance: the AMI, the instance type or a set of instance requirements, the key pair if any, network interfaces and security groups, the IAM instance profile, block device mappings for the root and data volumes, user data, metadata service options, tags to apply to instances and volumes, and purchase settings such as Spot options. Almost any field can be left out and supplied later, by the group or by the API call that launches the instance.

The older launch configurations did the same job, but they were immutable, could not be versioned, and do not support newer features such as mixed instance types, attribute based selection or IMDSv2 enforcement in the same way. AWS has stopped adding instance types to launch configurations, and new accounts cannot create them. Any group still using one should be migrated, which is a straightforward copy into a template followed by an instance refresh.

Versions and the default

Launch templates are versioned. Each change creates a new numbered version, and the template has a default version and a latest version. A group refers to a template by ID and by a version, which can be a specific number, $Default or $Latest. That choice matters more than it looks.

A group pinned to a specific version launches exactly that configuration until someone changes the group, which makes every change explicit and reviewable. A group set to $Latest launches whatever version was created most recently, so creating a new version, perhaps to test something, silently changes what the next scale out produces. A group set to $Default follows the version marked default, which gives a single explicit switch. Infrastructure as code tools usually create a new version and update the group's reference to it in the same change, which is the cleanest pattern: the version number in the group is then the source of truth for what is running.

Someone created version 5 to try a new AMI. The group following latest began launching it on the next scale out; the pinned group did not notice.

The AMI is the unit of change

There are two broad ways to get software onto an instance: bake it into the AMI, or install it at boot with user data. Baking produces an image with the operating system, runtime, agents and often the application already in place, built by a pipeline such as EC2 Image Builder or Packer. Boot time installation starts from a generic image and runs scripts that download and configure everything.

Baked images launch faster and more predictably, because there is nothing to download and fewer things to fail at boot. They also turn every change into a new image, which is easy to test and roll back. Boot time installation is more flexible but slower, and it depends on package repositories, artifact stores and configuration services being reachable at the moment of scale out, which is exactly when they are most likely to be under load or, during an incident, unavailable. Most mature teams bake everything except the final configuration, which comes from parameter stores or secrets managers at boot, and keep user data to a few lines.

User data and cloud-init

User data is a script or cloud-init document that runs once on first boot. It is limited to sixteen kilobytes, is visible to anyone who can describe the instance or read the metadata service from inside it, and runs as root. Those three facts give the rules: keep it short, never put secrets in it, and have it fetch configuration rather than contain it. A user data script that fails partway through leaves an instance that looks healthy to EC2 but does not serve, which is why the health checks covered later must test the application, not just the operating system.

Metadata service settings

Launch templates carry the instance metadata service options, and these are security settings. IMDSv2, which requires a session token obtained with a PUT request, should be required on every template, with the hop limit set to one for plain instances or two for instances running containers that need the metadata service. IMDSv2 defeats the classic server side request forgery attack in which an application is tricked into fetching the metadata credentials URL. The account level default can enforce IMDSv2 for all new launches, and the template setting makes it explicit for each workload.

Instance requirements instead of types

A launch template, or the mixed instances policy that overrides it in a group, can specify instance requirements instead of an instance type: a range of vCPUs and memory, the processor architecture, whether burstable types are allowed, the generations accepted, and optional limits on price relative to the cheapest matching type. The group then chooses from every type that matches. This is the most effective way to diversify Spot capacity and to adopt new generations automatically. It needs an AMI built for the architecture chosen, so a group that accepts both x86 and Arm needs two templates or overrides, one per architecture.

Tags and propagation

Templates can apply tags to instances, volumes and network interfaces at launch, and groups can propagate their own tags to instances. Cost allocation, ownership and automation all depend on these tags being present from the first second of the instance's life, rather than added afterwards by a script that may lag or fail. A tag policy in AWS Organizations, combined with tags on templates and groups, makes untagged instances rare.

Testing a template before a group uses it

Because a template version is only exercised when a group launches from it, a broken version can sit unnoticed until the next scale out, which is the worst possible time to find out. A cheap safeguard is to launch a single instance from each new version in a pipeline, wait for it to pass the same health check the load balancer uses, then terminate it and only then point the group at the version. The test takes a few minutes and catches the common failures: an AMI from the wrong region, a missing instance profile permission, a security group that blocks the health check port, or a user data script that exits early.

Image pipelines

An image pipeline turns the AMI from a hand crafted artifact into a build output. EC2 Image Builder or Packer starts from a patched base image, applies the same configuration management every time, installs agents and the application, runs tests against the resulting instance, and publishes the AMI with tags recording its source commit and build date. The pipeline runs on every application release and on a schedule, typically weekly, so that operating system patches reach the fleet even when the application has not changed. Sharing the resulting AMIs across accounts, through launch permissions or Image Builder distribution settings, lets one pipeline serve every environment, which is how the image tested in staging becomes, byte for byte, the image running in production.

Old AMIs and their snapshots accumulate quickly and cost money for storage. A retention policy that keeps the last few images per pipeline, plus any still referenced by a template version in use, keeps the catalogue small without deleting something a rollback might need.

Pin the version, bake the image

Point groups at a specific template version and change it deliberately, so creating a test version never changes what production launches. Bake software into the AMI and keep user data to fetching configuration, because the moment of scale out is the worst moment to depend on package mirrors. Require IMDSv2 in every template.