Launch Templates are the modern, more powerful successor to Launch Configurations, offering a richer feature set and greater flexibility for launching EC2 instances.

Here’s a look at how they work and why you’d want to switch.

Why the Change?

Launch Configurations are a legacy feature. They are stateless and immutable, meaning once created, they cannot be modified. Any change requires creating a new configuration. This is cumbersome and error-prone. Launch Templates, on the other hand, are stateful and versioned, allowing for modifications and rollbacks. They also integrate with other AWS services like AWS Systems Manager and Fleet Manager.

Launch Configurations in Action

Imagine you need to launch a fleet of web servers. With Launch Configurations, you’d first define the AMI, instance type, security groups, and a user data script for bootstrapping.

aws autoscaling create-launch-configuration \
    --launch-configuration-name my-old-web-servers \
    --image-id ami-0abcdef1234567890 \
    --instance-type t3.micro \
    --security-groups sg-0123456789abcdef0 \
    --user-data file://bootstrap-webserver.sh

This configuration is then attached to an Auto Scaling group. If you need to update the AMI or change the instance type, you’d create a new Launch Configuration and update the Auto Scaling group to use it, triggering a replacement of instances.

Launch Templates: The Evolution

Launch Templates offer a more granular approach. You can define various parameters, including instance type, AMI, security groups, EBS volumes, network interfaces, and user data. Crucially, they support versioning.

Let’s create a Launch Template:

aws ec2 create-launch-template \
    --launch-template-name my-new-web-servers-template \
    --version-description "Initial version for web servers" \
    --launch-template-data '{
        "ImageId": "ami-0abcdef1234567890",
        "InstanceType": "t3.micro",
        "NetworkInterfaces": [
            {
                "DeviceIndex": 0,
                "AssociatePublicIpAddress": true,
                "Groups": ["sg-0123456789abcdef0"]
            }
        ],
        "UserData": "'$(base64 bootstrap-webserver.sh)'"
    }'

Notice the UserData is base64 encoded. This is a common pattern.

Migrating from Launch Configurations to Launch Templates

The migration process involves creating a Launch Template that mirrors your existing Launch Configuration and then updating your Auto Scaling group.

  1. Identify your Launch Configuration:

    aws autoscaling describe-launch-configurations --query 'LaunchConfigurations[*].{Name:LaunchConfigurationName, ImageId:ImageId, InstanceType:InstanceType, SecurityGroups:SecurityGroups, UserData:UserData}'
    

    Let’s say you find my-old-web-servers with ami-0abcdef1234567890, t3.micro, sg-0123456789abcdef0, and some UserData.

  2. Create a new Launch Template: You’ll need to decode the UserData from your existing configuration if it’s present and base64 encode it for the new template.

    aws ec2 create-launch-template \
        --launch-template-name my-migrated-web-servers-template \
        --version-description "Migrated from my-old-web-servers LC" \
        --launch-template-data '{
            "ImageId": "ami-0abcdef1234567890",
            "InstanceType": "t3.micro",
            "NetworkInterfaces": [
                {
                    "DeviceIndex": 0,
                    "AssociatePublicIpAddress": true,
                    "Groups": ["sg-0123456789abcdef0"]
                }
            ],
            "UserData": "'$(echo "YOUR_DECODED_USERDATA" | base64)'"
        }'
    

    Replace YOUR_DECODED_USERDATA with the actual content of your user data script.

  3. Update your Auto Scaling Group: You’ll associate the new Launch Template with your Auto Scaling group.

    aws autoscaling update-auto-scaling-group \
        --auto-scaling-group-name my-web-servers-asg \
        --launch-template LaunchTemplateName=my-migrated-web-servers-template,Version=1
    

    This command tells the ASG to use version 1 of your new Launch Template for future instance launches. To update existing instances, you’ll need to perform a rolling update or a health check replacement.

    aws autoscaling start-instance-refresh \
        --auto-scaling-group-name my-web-servers-asg \
        --strategy Rolling \
        --preferences "MinHealthyPercentage=90"
    

Key Advantages of Launch Templates

  • Versioning: Roll back to previous configurations easily.
  • Modifiability: Update templates without recreating them.
  • Parameter Overrides: Specify different parameters (e.g., instance type) for specific instances when launching them from a template.
  • Integration: Seamlessly works with AWS Systems Manager, Fleet Manager, and other modern AWS services.
  • Mixed Instance Policies: Essential for using Spot Instances with On-Demand instances in a single Auto Scaling group.

One of the most powerful, yet often overlooked, features of Launch Templates is the ability to define multiple versions and then select a specific version for an Auto Scaling group’s update policy. This allows for complex deployment strategies, such as gradually rolling out a new template version to a subset of instances before committing to a full rollout, or having a fallback version readily available.

The next step in modernizing your EC2 deployments is exploring how to leverage Spot Instances with Launch Templates.

Want structured learning?

Take the full Ec2 course →