ASG and ELB Practical

Learn how to create an ASG, connect the application to an ELB, and perform ASG activities.

We'll cover the following

Introduction

One of the most common examples of using ASGs and ELBs is provisioning an ASG to create a scalable application on EC2 instances in private subnets.

  • Private subnets are in a VPC that can’t be reached from the internet. Any instances or services deployed in private subnets are only accessible within the VPC. We create an ELB in the public subnet and serve the application using the ELB domain name system (DNS). This ensures that our application instances are private but the application is still accessible from the internet.

  • Public subnets are in a VPC that can be reached from the internet.

Let’s see this in action.

Practical

In the following widget, we’ll:

  • Create a key pair.
  • Create two security groups for ALB and EC2 instances in the ALB.
  • Create a launch template for the ASG.
  • Create a load balancer, listener, and target group.
  • Create the ASG using the launch template and target group we created.
    • The minimum size of an ASG is the minimum number of instances that we want to have for our app. An ASG will never scale below this number.
    • The maximum size of an ASG is the maximum number of instances that we want to have for our app. An ASG won’t scale beyond this number of instances.
    • Desired capacity is the current number of instances we want in the ASG. It can change this value to scale between the maximum and minimum sizes.
  • Wait for the instance initialization to complete and see the web page from the ALB DNS. We can see the web page on our browser as well. Note that the ALB will get the response randomly from any one of the EC2 instances, so the web page content will change when the page is refreshed.
  • Add a target tracking scaling policy to keep the average CPU utilization of the EC2 instances in ASG to 50. Our desired capacity is minimum capacity + 1. Therefore, we can see that the ASG will scale in (i.e., terminate one EC2 instance and decrease the desired capacity by one.)
    • ASG will never scale below the min capacity.
  • Explicitly terminate one of the EC2 instances in ASG. This decreases the current number of EC2 instances from the desired capacity. The ASG automatically replaces the terminated instance with a fresh EC2 instance.

Get hands-on with 1200+ tech skills courses.