Sub topic 10
Launch Templates as the Blueprint for Instances
This chapter assumes you know what an EC2 instance, an AMI and a security group are, and that you can run AWS CLI commands. It covers the launch template, which tells an Auto Scaling group what kind of instance to start.
What a launch template holds
A launch template is a saved set of EC2 launch parameters. An Auto Scaling group reads it every time it needs a new instance, so every instance comes from the same blueprint.
Typical settings:
- The AMI ID.
- The instance type.
- Security group IDs.
- An instance profile (the IAM role the instance uses).
- A key pair, if you need SSH access.
- User data, a script that runs at first boot.
- Block device mappings and tags.
The template does not choose subnets. The Auto Scaling group does that through its --vpc-zone-identifier setting, which is how instances end up spread across Availability Zones.
Versions
A launch template is versioned. Each version is immutable, so to change a setting you create a new version. The group refers to a version in one of three ways:
| Reference | Meaning |
|---|---|
| A number such as `3` | Always uses that exact version |
| `$Latest` | The newest version you have created |
| `$Default` | The version you marked as default |
Pinning a number is predictable but needs a group update for each change. $Latest is convenient, but a new version can affect later launches before you have tested it. $Default lets you create a version first and promote it only after testing.
Changing the template does not alter running instances. Only new launches use the new version. To replace existing instances, use instance refresh (covered in a later chapter).
Creating a template
The Auto Scaling group in this tutorial uses a template named web-lt. Create it with the AWS CLI:
aws ec2 create-launch-template \
--launch-template-name web-lt \
--version-description "initial web server" \
--launch-template-data '{
"ImageId": "ami-0123456789abcdef0",
"InstanceType": "t3.micro",
"SecurityGroupIds": ["sg-0123456789abcdef0"],
"TagSpecifications": [{
"ResourceType": "instance",
"Tags": [{"Key": "Name", "Value": "web"}]
}]
}'Replace the AMI and security group IDs with values from your account and Region. To add a new version based on version 1 with a different instance type:
aws ec2 create-launch-template-version \
--launch-template-name web-lt \
--source-version 1 \
--version-description "larger instance" \
--launch-template-data '{"InstanceType": "t3.small"}'Fields you leave out are copied from the source version. To make it the default:
aws ec2 modify-launch-template \
--launch-template-name web-lt \
--default-version 2Design choices
- Bake or bootstrap. Installing software in user data keeps the AMI generic but slows each launch. A prebuilt AMI starts faster, which matters when scaling out under load.
- Security groups. The instance security group should allow traffic from the load balancer's security group, not from the whole internet.
- Instance profile. Give instances only the permissions they need, such as reading configuration from a parameter store.
- Health endpoint. User data or the AMI must start the application so that
/healthresponds. Otherwise the target group marks new instances unhealthy.
Exercise
You create version 2 of web-lt with a new AMI. The Auto Scaling group references $Default, and version 1 is still the default. What happens to new and existing instances, and what do you do to roll out the change?
Show answer
Nothing changes yet. Existing instances keep running their original configuration, and new launches still use version 1. Set version 2 as the default with aws ec2 modify-launch-template --default-version 2, so new launches use it. Then run an instance refresh to replace the existing instances.