Mar 30, 2024
7 Min read

Introduction to Amazon EC2 auto scaling

In this tutorial, we will use Amazon EC2 auto scaling to recover an EC2 instance from failure.

Prerequisites

You need an account with privileges to create an EC2 instance, key pairs, EC2 launch templates, and Auto scaling groups on AWS.

What is EC2 auto scaling

EC2 is the basic computing unit in AWS.

EC2 auto scaling is a feature that helps you to run the desired number of EC2 instances at any time to match the capacity requirement of your workloads.

EC2 auto scaling can recover EC2 instances from failures by creating new instances to replace the failed instances.

Auto scaling can also help you to optimize your AWS bill by maintaining the optimum number of EC2 instances to match the capacity demand of your applications. Auto scaling will launch new EC2 instances when the capacity demand increases and will terminate existing instances when the demand decreases.

EC2 auto scaling is implemented via Auto scaling groups.

What is an auto scaling group

An auto scaling group is a grouping of EC2 instances governed by two types of auto scaling controls.

  1. Health checks ensure the desired number of EC2 instances are always running by replacing failed EC2 instances with new instances
  2. Target tracking scaling policies scale the number of instances by monitoring CPU utilization, network bandwidth, load balancer requests count, etc.

How to create an auto scaling group

EC2 auto scaling uses a launch template to create new EC2 instances. A launch template pre-defines a set of parameters for creating EC2 instance.

Create an EC2 launch template

We will use the default VPC for this tutorial. But you can use any other VPC if you wish.

Log in to the AWS account. Select your preferred region from the top menu and select EC2 from the Services menu to go to the EC2 console.

  1. In the left navigation menu, under Instances click on Launch Templates. Creating a new launch template.

    Click on the Create launch template button.

  2. Type in my-app for the Launch template name. Type in v1.0 for the Version.

  3. In the Applications and OS images section, click on the Quick start tab. Select the AMI Ubuntu Server 22.04 LTS which is free tier eligible.

  4. Select instance type as t2.micro in the Instance type section. This instance type is also eligible for AWS free tier.

  5. In the Key pair section we must define an SSH key pair to be configured in the EC2 instances created with this template. If you have a previously-created key pair, select it from the drop-down list.

    If not, click on Create new key-pair. to create a new set of SSH keys. In the Create Key Pair dialog box, type in my-app-key-pair for the name. Leave the default values for other parameters and click on Create Key Pair. button.

    AWS will prompt you to download the pem file. Save it in a secure location. You’ll need it to SSH into the EC2.

  6. In the Network settings section, leave out the subnet field. We will select the subnets when we are creating the scaling group.

  7. From the Security groups drop-down menu, select the default security group or any previously created security group of the VPC that you wish to launch this EC2.

  8. Click on Create Launch Template to create the template.

Create the auto scaling group

Let’s create an auto scaling group using this launch template we created just now.

On the EC2 console, click on Auto Scaling Groups under Auto Scaling menu. Click on Create Auto Scaling Group. You will get a 7-step wizard to create a new auto scaling group.

  1. Type inmy-app. for the Auto scaling group name.

    Select my-app launch template from the Launch template drop-down menu. Since there’s only one version of the template, this version will be selected automatically. If we have multiple versions of the launch template, we can select the required version here. Auto scaling activity history

    Click on Next.

  2. In the Network section, select the desired VPC from the VPC drop-down list.

    You can select multiple subnets from the Availability zones and subnets drop-down list. Since we are creating a scaling group for one EC2 instance, let’s select just one subnet and click on Next

    If you plan to have more than one EC2 in your auto scaling group you can select multiple subnets in different availability zones. Then AWS will evenly distribute your EC2 instances across these subnets.

  3. We will not be using load balancing so keep the No load balancer option in the Load balancing section.

    We are also not using VPC Lattice, so keep No VPC Lattice Service option in the VPC Lattice integration options section.

    In the Health checks section, AWS automatically enables all health checks for the EC2.

    So, click on Next to go to the next step.

  4. In the Group size section, set the desired capacity as 1. This is the number of EC2 instances you need to be up and running continuously.

    This section also has two more parameters.

    Min desired capacity is the minimum number of EC2 instances that must be retained when scaling in. Max desired capacity is the maximum number of EC2 instances that can be running when scaling out.

    Since our Desired capacity is set to 1, the Min desired capacity and Max desired capacity is also set to 1.

    We will not use target tacking policies in this tutorial so keep the No scaling policies in the Automatic scaling section.

    Keep the option No policy in the Instance maintenance policy section so that the scaling group will launch a new EC2 instance when a health check fails in the existing instance.

    Click on Next.

  5. You can configure AWS SNS notifications here. We will not do it now.

    Click on Skip to review to skip Step-6 and go to the last step.

  6. Review the parameters you just set and click on the Create auto scaling group button to create the auto scaling group.

It will take us to the Auto scaling groups section in the EC2 console.

AWS will now be launching new EC2 instances to match the criteria of our new auto scaling group. Wait a few minutes and click on the refresh button at the upper menu to refresh the status.

The instance count would be updated as 1.

Auto scaling in action

Go to the EC2 dashboard and select Instances from the left navigation menu. A new instance would have been created for our auto scaling group.

Select the instance and click on Terminate instance from the Instance state drop-down menu.

Wait for about 1 minute and click the refresh button.

Auto scaling group creates new EC2 instance

A new instance would be created to replace the terminated instance.

Check the Auto scaling group logs

Go to the Auto Scaling Groups in EC2 dashboard.

Click on the my-app auto scaling group to view the details.

Click on the Activity tab.

Auto scaling activity history

Check the activity history and read the log of activities to check what had happened when we terminated the EC2 instance.

Delete the auto scaling group

We have successfully created an auto scaling group to recover our EC2 from failure. Let’s delete the resources we created so we will not be overbilled.

Go to the Auto Scaling Groups in the EC2 dashboard. Select my-app auto scaling group. In the Actions drop-down menu click on Delete. Confirm deletion, and AWS will delete the auto scaling group including the instances belonging to it.

Click on the Instances menu and verify the instances are terminated.

Wrapping up

In this tutorial, we created an EC2 instance with auto scaling.

EC2 auto scaling takes care of creating new instances to match our capacity requirements.

But, it’s our responsibility to deploy the application on the new EC2 instances when they are created. There are two approaches to do that.

  1. Create a new AMI with our application pre-installed and use that AMI in the launch template
  2. Deploy our application on the new EC2 once it enters the running state.

We’ll explore these options in upcoming tutorials.