Setting up an Application using Auto Scaling that can scale and load balance dynamically

We have seen a general introduction of Auto Scaling in the previous article. In this article we will learn about setting up an application using Auto Scaling that can scale and load-balance dynamically without any human intervention.

Prerequisites:

Before going further into the article, you should be able to setup a load balancer, create EC2 instances, creating an AMI, able to Route 53 to configure DNS to point to an elb and working with the AWS management console.

Auto Scaling ensures that desired number of instances is always available to handle load on your application. This way auto scaling equips your infrastructure so that your application always has the right amount of capacity to handle stable to varying traffic demands. Also auto scaling makes your application highly available and fault tolerant. As said earlier auto scaling comes with no additional cost and you pay only for the AWS resources that you actually use.

We will look at the steps involved in setting up your first application using Auto Scaling after learning about certain building blocks that must be used to setup Auto Scaling.

Auto Scaling Components:

There are three components of Auto Scaling.

Groups:

This defines the collection of your AWS EC 2 instances. They are organized into groups for easy management. When creating an auto scaling group, minimum, maximum and desired number of instances have to be specified so that your auto scaling group would never cross the specified limit.

Launch Configurations:

This is a template used by your auto scaling group which defines the instance type, Amazon Machine Image ID, key pair, security groups and block device mapping used by your instances.

Scaling Plans:

Auto scaling scales in and out based on the parameters set in the specified scaling plan. This is how auto scaling scales dynamically based on the load demand.

Setting up your first application using Auto Scaling that dynamically scales and load-balances:

An Auto scaling group can be associated with an elastic load balancer so that the load balancer automatically distributes incoming web traffic to instances in the group.

We will be setting up the application only using the AWS Management Console for the sake of this article. However, auto scaling can also be setup using AWS CLI and AWS Tools for Windows PowerShell.

Create a Launch Configuration:

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. On the navigation bar at the top of the screen, select the region that you used when creating your load balancer.
  3. On the navigation pane, under Auto Scaling, choose Launch Configurations. If you don’t have any Auto Scaling resources, you see a welcome page; choose Create Auto Scaling group.
  4. Choose Create launch configuration.
  5. On the Choose AMI page, select your custom AMI.
  6. On the Choose Instance Type page, select a hardware configuration for your instance, and then choose Next: Configure details.
  7. On the Configure Details page, do the following:
    • For Name, type a name for your launch configuration.
    • (Optional) To securely distribute credentials to your EC2 instance, select your IAM role.
    • (Optional) If you need to connect to an instance in a nondefault VPC, for Advanced Details, IP Address Type, choose Assign a public IP address to every instance.
    • (Optional) To specify user data or a configuration script for your instance, for Advanced Details, User data, paste your configuration script.
    • Choose Skip to review.
  8. On the Review page, choose Edit security groups. Follow the instructions to choose an existing security group, and then choose Review.
  9. On the Review page, choose Create launch configuration.
  10. On the Select an existing key pair or create a new key pair page, select one of the listed options.
    Select the acknowledgment check box, and then choose Create launch configuration.
    Warning: Do not choose Proceed without a key pair if you need to connect to your instance.
  11. The Launch configuration creation status page displays the status of your newly created launch
    configuration. Choose Create an Auto Scaling group using this launch configuration.

Create an Auto Scaling Group:

  1. On the Configure Auto Scaling group details page, do the following:
    • For Group name, type a name for your Auto Scaling group.
    • For Group size, type the initial number of instances for your Auto Scaling group.
    • If you selected an instance type for your launch configuration that requires a VPC, such as a T2 instance, you must select a VPC for Network. Otherwise, if your account supports EC2-Classic and you selected an instance type that doesn’t require a VPC, you can select either Launch into EC2-Classic or a VPC.
    • If you selected a VPC in the previous step, select one or more subnets from Subnet. If you selected EC2-Classic instead, select one or more Availability Zones from Availability Zone(s).
    • For Advanced Details, select Receive traffic from Elastic Load Balancer(s) and then do one of the following:
      • [Classic Load Balancers] Select your load balancer from Load Balancers.
      • [Target groups] Select your target group from Target Groups.
    • For Advanced Details, select Receive traffic from Elastic Load Balancer(s) and then select your load balancer from Load Balancers.
    • (Optional) To use Elastic Load Balancing health checks, choose ELB for Advanced Details, Health Check Type.
    • Choose Next: Configure scaling policies.
  2. On the Configure scaling policies page, select Keep this group at its initial size, and then choose Review.
  3. Review the details of your Auto Scaling group. You can choose Edit to make changes. When you are finished, choose Create Auto Scaling group.

Verify if the load balancer is attached to the Auto Scaling Group you just created:

  1. Select your Auto Scaling group.
  2. On the Details tab, Load Balancers shows any attached load balancers and Target Groups shows any attached target groups.
  3. On the Details tab, Load Balancers shows any attached load balancers.
  4. On the Activity History tab, the Status column shows you the status of your Auto Scaling instances. While an instance is launching, its status is In progress. The status changes to Successful after the instance is launched.
  5. On the Instances tab, the Lifecycle column shows the state of your Auto Scaling instances. After an instance is ready to receive traffic, its state is InService.

The Health Status column shows the result of the health checks on your instances.

By following the above steps, you can successfully launch your application using Auto Scaling. Using Auto Scaling with a load balancer adds more benefits to your application architecture like meeting dynamic load demands and scaling when necessity arises without any human intervention. We will take a look at configuring alarms during any scaling events in the next article. Stay tuned!