Running a website or application doesn’t have to be costly. In fact, there are ways to save money on resources while still providing users with a great experience. One example is through AWS EC2 Autoscaling.
EC2 Auto Scaling is a service that can help automatically scale your Amazon EC2 capacity up or down according to the conditions you define. And in this tutorial, you’ll learn how to set up and apply a basic EC2 Auto Scaling configuration on AWS to your applications.
Read on and ensure your application always has resources without paying more than you need!
Prerequisites
This tutorial comprises hands-on demonstrations. To follow along, you’ll need an AWS account, and a free tier account would suffice.
Creating a Launch Template
Before jumping into AWS EC2 Auto Scaling, you first need a way to launch an EC2 instance, a launch template (similar to launch configuration). A launch template contains all the information required to launch an EC2 instance, such as the instance type, subnet, and security groups.
AWS recommends using a launch template, providing more flexibility and advanced features than a launch configuration. EC2 Auto Scaling uses a launch template to launch new instances when scaling up.
To create a launch template:
1. Open your favorite web browser, and sign in to the AWS Management Console.
2. Search for EC2 launch templates in the search bar and select Launch templates from the results under Features. Doing so opens the EC2 launch templates page.
3. Next, select a region from the dropdown list (top-right), and click Create launch template to initiate creating a launch template.
You can use auto-scaling in any region that supports EC2 Auto Scaling, but this tutorial uses US East (N. Virginia).
4. Now, configure the new launch template’s name and description with the following:
- Launch template name – required – Provide a name for your launch template (ec2-autoscaling-template-demo). The name must be unique within the region, not longer than 128 characters, and doesn’t contain special characters other than hyphens (-), underscores (_), and periods (.).
- Template version description – Input a description for your launch template (ec2 autoscaling template demo), and not longer than 255 characters.
- Auto Scaling guidance – Tick the Provide guidance to help me set up a template that I can use with EC2 Auto Scaling option to enable auto-scaling guidance. This option makes creating a launch template compatible with EC2 Auto Scaling easier.
5. Scroll down to the Launch templates contents section, and choose application and OS images for your launch template with the following:
- Application and OS Images (Amazon Machine Image) – Select a boot image for your template, but this tutorial uses an Amazon Linux 2 AMI. Your EC2 instances will be launched based on the selected boot image.
You can select any boot image you prefer, but an Amazon Linux 2 AMI is recommended since it’s free tier eligible.
- Architecture – Select an architecture for your template, but this tutorial uses x86-64 (64-bit).
6. Next, set the Instance type and the Key par (login) for your launch template as follows:
- Instance type – Select the instance type for your template. But this tutorial’s choice is t2.micro since it’s free tier eligible.
Consider other factors when selecting an instance type, such as CPU and memory requirements, since some instance types cost more than others.
- Key pair (login) – Select Don’t include in launch template since this is not needed for auto-scaling.
When a key pair is specified, EC2 Auto Scaling launches your instances with the key pair.
7. Under Network settings, choose the Select existing security group option and specify a Security groups setting for your EC2 instances. Doing so makes EC2 Auto Scaling launch your instances with this security group.
Note that you can choose the Create security group option if you wish to create a new security group for your EC2 instances.
8. Keep other default settings, and click Create launch template (lower-right) under the Summary toggle list to create your launch template.
9. Finally, click Create Auto Scaling group under the Create an Auto Scaling group from your template section to continue.
Don’t close the launch template page since you’ll create your Auto Scaling group in the following section, one at a time.
Creating an AWS EC2 Auto Scaling Group
Now that you have an EC2 launch template, you’ll need to create an AWS EC2 Auto Scaling group to contain your EC2 instance. AWS EC2 Auto Scaling has features that let you use health check replacements and scaling policies.
You can create an Auto Scaling group with multiple instances if you like. But in this tutorial, you’ll create a single instance Auto Scaling group to demonstrate how Auto Scaling works.
1. Configure the Auto Scaling group with the following while still on the Create launch template page:
- Auto Scaling group name – Input a name for your Auto Scaling group (ec2-autoscaling-group-demo). The name must be unique within your AWS Region with no more than 255 characters in length.
- Launch template – Select your EC2 launch template from the dropdown list (ec2-autoscaling-template-demo).
- Click Next to continue.
2. Next, leave all default settings as is, but select an existing VPC and subnet for your Auto Scaling group, and click Next to continue.
Specifying these settings makes your Auto Scaling group know where to launch your EC2 instances.
3. Keep the default settings, and click Next to continue.
4. Now, configure the Group size of your Auto Scaling group with the following:
Capacity | Limit | Details |
Desired | 1 | Set the number of instances you want in your Auto Scaling group. The desired capacity must be greater than or equal to the minimum capacity and less than or equal to the maximum capacity. |
Minimum | 1 | Set the minimum number of instances in your Auto Scaling group. The value must be greater than or equal to 1 and less than or equal to the maximum capacity. |
Maximum | 1 | Set the maximum number of instances you want in your Auto Scaling group. |
5. Scroll down, keep the default settings, and click Skip to review to continue.
- Scaling policies – Keep the None option selected since you’ll use a simple scaling policy. This policy increases the desired capacity of your Auto Scaling group by one EC2 instance when the older instance in the group has been terminated.
- Instance scale-in protection – Keep this option disabled since you don’t need it in this tutorial. Enabling this option lets you control whether your Auto Scaling group can terminate your instance when scaling in.
6. Lastly, review your Auto Scaling group settings, and click Create auto scaling group (bottom-right) to create your Auto Scaling group.
Once created, you’ll see your Auto Scaling group listed, as shown below.
Testing an AWS EC2 Auto Scaling Group
Great! You’ve managed to create an AWS EC2 Auto Scaling group to contain your EC2 instance. But how do you know your Auto Scaling group works?
You’ll verify that your AWS EC2 Auto Scaling group has launched one EC2 instance. This instance is the one you specified (Desired capacity) in step four of the “Creating an AWS EC2 Auto Scaling Group” section.
1. Tick the checkbox next to your newly-created Auto Scaling group and click the Activity tab to see all activities in your Auto Scaling group.
Under the Activity history section at the bottom, you will see the Description of the most recent event in your Auto Scaling group. In this case, the description says Launching a new EC2 instance, with the Successful status and instance ID (i-09b699064f6e06070).
Copy the instance ID, as you’ll need it to verify your instance in the following step.
2. Navigate to the EC2 dashboard, and compare the instance ID you copied in step one with any running instances.
Seeing both instance IDs match indicates your Auto Scaling group has successfully launched an EC2 instance.
3. Back in the Auto Scaling group page, click on the Instance management tab, and you’ll see all information about your EC2 instances in one place. The most critical piece of information on this tab are:
- The Lifecycle column – You can see that the EC2 instance in your Auto Scaling group is in the InService state, which signifies the instance is available to handle requests.
- The Health status column – You can see that the health check status of your EC2 instance is Healthy, which indicates the instance is passing all health checks.
Terminating an EC2 Instance
Now that you’ve verified that your Auto Scaling group is working, you can test the Auto Scaling group further by terminating the EC2 instance within. Remember, terminating an instance in the group causes the Auto Scaling group to launch a new EC2 instance as a replacement automatically.
In real-life cases, you’d use an Auto Scaling group to scale up in response to changing conditions automatically. When? For example, one of your NGINX web servers is overloaded with requests and is in danger of going down.
But for now, you’ll test your Auto Scaling group by terminating the EC2 instance manually to simulate what would happen if an instance fails.
1. Navigate to your instances, and tick the checkbox next to the instance to select it.
Once selected, click on the Instance state dropdown menu → Terminate instance to terminate the selected instance.
2. Next, click Terminate when prompted to confirm terminating the instance.
Below, you can see a message that indicates the EC2 instance has been successfully terminated.
The default cooldown period for an Auto Scaling group is 300 seconds (5 minutes), which means that it will take the Auto Scaling group at least five minutes to launch a new instance after you terminate the old one.
3. Finally, check the Activity history in the Auto Scaling group while waiting for the new instance to launch.
As shown below, you’ll see that a new EC2 instance has been launched automatically. This output confirms your Auto Scaling group is working as expected.
Conclusion
Handling the load for your application is an essential part of keeping it running smoothly. Luckily, with AWS EC2 Auto Scaling, you can ensure your application always has the needed resources to handle changing traffic patterns.
And in this tutorial, you’ve learned how to create an Auto Scaling group, launch instances into the group, and scale the group in response to changing conditions. At this point, you can now confidently create AWS EC2 Auto Scaling groups for your applications.
With this newfound knowledge, why not create auto scaling groups with AWS CloudFormation for even more convenience? You will thank yourself later when your load increases, and you don’t have to worry about manually provisioning more resources!