Implementing an Auto Scaling Group and Application Load Balancer in AWS

Hands-On Lab

 

Photo of Adrian Cantrill

Adrian Cantrill

Training Architect

Length

01:30:00

Difficulty

Intermediate

In this AWS hands-on lab, we will integrate two powerful AWS services: Elastic Load Balancers and Auto Scaling groups. Specifically, we will create an Auto Scaling group of EC2 instances operating as web servers, and we'll configure an Application Load Balancer to load balance between the instances inside that Auto Scaling group. After everything is set up, we will simulate stress tests on the EC2 instances to confirm the Auto Scaling group works as expected. This experience is good practice for building highly available and cost-efficient applications on AWS.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Implementing an Auto Scaling Group and Application Load Balancer in AWS

Introduction

In this hands-on lab, we will integrate two powerful AWS services: Application Load Balancers and Auto Scaling groups.

Solution

Log in to the live AWS environment using the credentials provided. Make sure you're in the N. Virginia (us-east-1) region throughout the lab.

The sudo command will be required to run the commands below.

Create an Application Load Balancer

  1. Navigate to EC2.
  2. Select Load Balancers in the left-hand menu.
  3. Click Create Load Balancer.
  4. In the Application Load Balancer card, click Create.
  5. In the Basic Configuration section, set the following values:
    • Name: LABALB
    • Scheme: internet-facing
    • IP address type: ipv4
  6. Leave the settings in the Listeners section as-is.
  7. In the Availability Zones section, select the listed VPC.
  8. Select us-east-1a and us-east-1b.
  9. Click Next: Configure Security Settings.
  10. Click Next: Configure Security Groups.
  11. For Assign a security group, select Create a new security group.
  12. Give it a Security group name and Description of "ALBSG".
  13. Leave the custom TCP rule set.
  14. Click Next: Configure Routing.
  15. In the Target Group section, set the following values:
    • Target group: New target group
    • Name: ALBTG
    • Target type: Instance
    • Protocol: HTTP
    • Port: 80
  16. Leave the Health checks section settings as-is.
  17. In the Advanced health check settings section, change the Healthy threshold to "2".
  18. Click Next: Register Targets.
  19. Leave the settings as-is.
  20. Click Next: Review.
  21. Click Create.
  22. On the load balancer dashboard, with the ALB selected, copy the DNS name listed in the Description section.
  23. Paste it into a new browser tab, which will result in an error.

Create a Launch Template

Create SSH Key Pair

  1. Navigate to EC2 > Network & Security > Key Pairs.
  2. Click Create Key Pair.
  3. In the dialog, give it a Key pair name of "ALBASG".
  4. Click Create.
  5. Once it's created, it will automatically download. Make sure you know where it's saved on your computer, as we'll need it later.

Create Security Group for EC2 Instances

  1. In the left-hand menu, select Security Groups.
  2. Copy the Group ID listed for the ALBSG we created earlier, and paste it into a text file, as we'll need it in a minute.
  3. Click Create Security Group, and set the following values:
    • Security group name: ec2web
    • Description: ec2web
    • VPC: The listed lab VPC
  4. Click Add Rule, and set the following values:
    • Type: SSH
    • Source: Custom, 0.0.0.0/0
  5. Click Add Rule, and set the following values:
    • Type: SSH
    • Source: Custom, ::/0
  6. Click Add Rule, and set the following values:
    • Type: HTTP
    • Source: Custom, and paste the security group ID of ALBSG you previously copied
  7. Click Create.

Create EC2 Instance Launch Template

  1. Select Launch Templates in the left-hand menu.

  2. Click Create launch template.

  3. Set the following values:

    • Launch template name: LABLT
    • Template version description: LABLT
  4. In the Launch template contents section, click Search for AMI.

  5. In the Search for AMI dialog, in the AMI dropdown, select Amazon Linux 2 AMI 64-bit (x86).

  6. Click Select AMI.

  7. Set the following values:

    • Instance type: t3.micro
    • Key pair name: ALBASG
    • Network type: VPC
    • Security Groups: ec2web
  8. Expand the Advanced details section, and paste the following into the User data box:

    #!/bin/bash
    yum update -y
    yum install -y httpd
    yum install -y wget
    cd /var/www/html
    wget https://raw.githubusercontent.com/linuxacademy/content-aws-csa2019/master/lab_files/07_hybrid_scaling/ASGandALB/index.html
    wget https://raw.githubusercontent.com/linuxacademy/content-aws-csa2019/master/lab_files/07_hybrid_scaling/ASGandALB/pinehead.png
    service httpd start
  9. Click Create launch template and then Close.

Create Auto Scaling Group

  1. Select Auto Scaling Groups in the left-hand menu.
  2. Click Create Auto Scaling group.
  3. Select Launch Template.
  4. Select LABLT from the table.
  5. Click Next Step.
  6. On the Create Auto Scaling Group page, set the following values:
    • Group name: LABASG
    • Group size: 2
    • Network: Select the listed VPC
    • Subnet: us-east-1a and us-east-1b
  7. Click Next: Configure scaling policies.
  8. Leave Keep this group at its initial size selected.
  9. Click Next: Configure Notifications.
  10. Click Next: Configure Tags.
  11. Click Review.
  12. Click Create Auto Scaling group.

Ensure Application Load Balancer Is Ready

  1. Select Load Balancers in the left-hand menu.
  2. Make sure the listed load balancer's state is active. If not, refresh the table until it is.

Enable Group Metrics Collection

  1. Select Auto Scaling Groups in the left-hand menu.
  2. Click the Monitoring tab.
  3. Click Enable Group Metrics Collection.

Edit the Auto Scaling Group to Allow Scaling and Use the ALB

  1. Click Actions > Edit.
  2. Set the following values:
    • Max: 6
    • Target Groups: ALBTG
  3. Click Save.

Configure Auto Scaling Group Scaling Policies

  1. Select the Scaling Policies tab.
  2. Click Add policy.
  3. Click Create a simple scaling policy, and set the following values:
    • Name: SCALEOUT
    • Take the action: Add 1 instances
    • And then wait: 300 seconds before allowing another scaling activity
    • Execute policy when: Click Create new alarm
      • In the Create Alarm dialog, set the following values:
        • Send a notification to: Un-check
        • Whenever: Average of CPU Utilization
        • Is: >= 40 Percent
        • For at least: 1 consecutive period(s) of 5 minutes
        • Name of alarm: HIGHCPU
      • Click Create Alarm and Close.
  4. Click Create.
  5. Click Add policy again.
  6. Click Create a simple scaling policy, and set the following values:
    • Name: SCALEIN
    • Take the action: Remove 1 instances
    • And then wait: 300 seconds before allowing another scaling activity
    • Execute policy when: Click Create new alarm
      • In the Create Alarm dialog, set the following values:
        • Send a notification to: Un-check
        • Whenever: Average of CPU Utilization
        • Is: <= 20 Percent
        • For at least: 1 consecutive period(s) of 5 minutes
        • Name of alarm: LOWCPU
      • Click Create Alarm and Close.
  7. Click Create.

Test Horizontal Scaling

  1. Select Instances in the left-hand menu.

  2. Right-click one of the instances, and click Connect.

  3. Copy the chmod command listed in the dialog.

  4. Open a terminal session, and change to your downloads directory.

  5. Run the chmod command to change the permissions on the key pair we downloaded earlier.

  6. Copy the ssh connection string listed in the connection dialog in the AWS console.

  7. Run the connection string in the terminal session to connect to the instance.

  8. Install the stress test application:

    sudo amazon-linux-extras install epel -y  
    sudo yum install -y stress
  9. Run the stress test on the EC2 instance:

    stress --cpu 2 --timeout 300

    (Optionally, use 3000 for timeout.)

  10. On the instances page in the AWS console, refresh the instances table.

  11. Click the Monitoring tab to see the increased CPU utilization.

  12. Navigate to the Auto Scaling group page.

  13. Click the Activity History tab to see there should be a new instance being launched.

Conclusion

Congratulations on completing this hands-on lab!