Set Up AWS Auto Scaling with Launch Templates for EC2

Introduction

Amazon Web Services (AWS) offers powerful tools to help businesses manage their cloud infrastructure. One of the most useful tools is Auto Scaling, which automatically adjusts the number of EC2 (Elastic Compute Cloud) instances based on your application’s needs. This ensures you always have the right amount of computing power without manual intervention.

In this article, we will explain how to create AWS Launch Templates and use them with an Auto Scaling Group to manage your EC2 instances.

Here are the steps.

Step 1. Creating an EC2 Launch Template.

  • Log in to AWS Console and go to the EC2 Dashboard.
  • On the left-hand side of the EC2 Dashboard, find Launch Templates and click on it.
    Launch template
  • Click on the Create Launch template button.
    Click create template
  • Fill out the details for your instance.
    • Template Name: Give your template a name, like MyLaunchTemplate.
      Name
    • AMI (Amazon Machine Image): Select the default Amazon Linux 2 or Ubuntu.
      Ubuntu
    • Instance Type: Choose t2.micro (Free Tier eligible).
    • Key Pair: Choose or create a key pair (used for SSH access).
      Key Pair
    • Security Group: Set up a security group to allow SSH or HTTP access.
  • User Data (Optional): You can provide a simple user data script, like
    #!/bin/bash
    echo "Hello World" > /home/ec2-user/hello.txt
    
  • Save the Template Once you’ve filled out the details, click Create Launch Template.
    Click create template

Step 2. Setting Up Auto Scaling Group.

Now that you’ve created the Launch Template, it’s time to set up the Auto Scaling Group (ASG). An Auto Scaling Group automatically adjusts the number of instances based on your needs. For example, if the load increases, Auto Scaling can add more EC2 instances.

How to Create an Auto-Scaling Group?

  • In the EC2 Dashboard, scroll down to the Auto Scaling section on the left menu.
  • Click on Auto Scaling Groups.
    Autoscaling group
  • Click Create Auto Scaling group.
    Click create group
  • Firstly, Name your Auto Scaling Group, like MyAutoScalingGroup.
  • Select the launch template you created in Step 1.
    Group name
  • Now, Choose your VPC (virtual private cloud), subnet (the network your instances will run on), and availability zones.
    VPC
  • Define your group's desired capacity and scaling limits. You can optionally add automatic scaling to adjust the size of your group.
    Desired Capacity
  • Once everything is set, click Create Auto Scaling Group.
    Click create
  • Here, you can see the Auto Scaling Group.
    Created

Step 3. Testing Auto Scaling.

  • In the EC2 Dashboard, click on instance.
    Click instances
  • Here, an instance has been automatically created, which was done by the Auto Scaling Group.
    Running insatnce
  • Now click on the action button and select terminate notion to delete the instance.
    Terminate
  • After terminating the instance just click on the refresh button, and it will automatically create a new instance again.
    Scaling

Note. If you want to create more instances, you can increase the desired capacity number in the Auto Scaling Group by clicking the Edit button.

Conclusion

AWS Auto Scaling is a powerful feature that ensures your applications always have the right amount of compute power by automatically adjusting the number of EC2 instances based on demand. By creating a Launch Template and setting up an Auto Scaling Group, you can efficiently manage your resources, reduce manual intervention, and optimize costs. Testing the Auto Scaling Group demonstrates its reliability in maintaining desired capacity, even when instances are terminated. With AWS Auto Scaling, you can achieve seamless scalability and high availability for your applications.


Similar Articles