Skip to main content

Create an Auto Scaling Group and Application Load Balancer in AWS

Hands-On Lab

 

Photo of Christophe Limpalair

Christophe Limpalair

Training Architect

Length

01:00: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.

Here is the user-data required for this lab:

#!/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

For Windows users using PuTTY, here are instructions for using it to SSH. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html

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.

Create 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.

Creating 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: ALB
    • 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 of "ALB-SG".
  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: ALB-TG
    • Protocol: HTTP
    • Port: 80
    • Target type: instance
  16. In the Health checks section, set the following values:
    • Protocol: HTTP
    • Path: /
  17. In the Advanced health check settings section, set the following values:
    • Port: traffic port
    • Healthy threshold: 2
    • Unhealthy threshold: 2
    • Timeout: 5
    • Interval: 30
    • Success codes: 200
  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 ALB selected, copy the DNS name listed in the Description section below.
  23. Paste it into a new browser tab, which will result in an error. Leave this tab open.

Creating an Auto Scaling Group

Create Launch Configuration

  1. Back in the AWS console tab, select Auto Scaling Groups in the left-hand menu.

  2. Click Create Auto Scaling group.

  3. On the AMI page, select the Amazon Linux AMI (not Amazon Linux 2 AMI).

  4. Leave t2.micro selected, and click Next: Configure Instance Details.

  5. On the Create Launch Configuration details page, set the following values:

    • Name: LC-ASG
    • Purchasing option: Leave un-checked
    • IAM role: None
    • Monitoring: Leave un-checked
  6. Expand the Advanced Details section.

  7. In the User data box, enter the following bash script:

    #!/bin/bash
    yum update -y
    yum install -y httpd
    echo 'Hello from Linux Academy Linux Academy' > /var/www/html/index.html
    service httpd start
  8. Click Next: Add Storage.

  9. Leave the values as-is, and click Next: Configure Security Group.

  10. On the Create Launch Configuration security group page, select Create a new security group.

  11. Give it a Security group name of "ASG-LC-SG", and select the listed VPC.

  12. Click Add Rule, and set the following values:

    • Type: HTTP
    • Protocol: TCP
    • Port Range: 80
    • Source: Anywhere, 0.0.0.0/0
  13. Click Review.

  14. On the review page, click Create launch configuration.

  15. In the key pair pop-up, select Create a new key pair.

  16. Give it a name of "labec2key".

  17. Click Download Key Pair.

  18. Once it's downloaded, click Create launch configuration.

Create Auto Scaling Group

  1. On the Create Auto Scaling Group details page, set the following values:
    • Group name: LAB-ASG
    • Group size: 2
    • Network: Select the listed VPC
    • Subnet: us-east-1a and us-east-1b
  2. Expand the Advanced details section, and set the following values:
    • Load Balancing: Check the Receive traffic from one or more load balancers box
    • Target Groups: ALB-TG
    • Health Check Type: ELB
  3. Click Next: Configure scaling policies.
  4. Select Use scaling policies to adjust the capacity of this group.
  5. Change it to scale between 2 and 5 instances.
  6. In the Scale Group Size section, set the following values:
    • Name: Scale Group Size
    • Metric type: Average CPU Utilization
    • Target value: 80
  7. Click the Scale the Auto Scaling group using step or simple scaling policies link.
  8. In the Increase Group Size section, set the following values:
    • Execute policy when: Click Add new alarm
      • In the Create Alarm dialog, set the following values:
        • Send a notification to: Un-check
        • Whenever: Average of CPU Utilization
        • Is: >= 80 Percent
        • For at least: 1 consecutive period(s) of 1 minute
      • Click Create Alarm.
    • Take the action: Add 1 instances when 80 <= CPUUtilization < +infinity
  9. In the Decrease Group Size section, set the following values:
    • Execute policy when: Click Add new alarm
      • In the Create Alarm dialog, set the following values:
        • Send a notification to: Un-check
        • Whenever: Average of CPU Utilization
        • Is: <= 50 Percent
        • For at least: 1 consecutive period(s) of 1 minute
      • Click Create Alarm.
    • Take the action: Remove 1 instances when 50 >= CPUUtilization > -infinity
  10. Click Next: Configure Notifications.
  11. We'll skip this for now, and click Next: Configure Tags.
  12. We aren't adding any tags, so click Review.
  13. Click Create Auto Scaling group.
  14. Once it's successfully created, click View your Auto Scaling groups.
  15. Right-click Instances in the left-hand menu to open it in a new tab and monitor it for when they're finished provisioning.
  16. Refresh the browser tab with the DNS name, which should result in a slightly better error (503) than before.
  17. Once the instances enter a running state, copy and paste each of their public IPs into new browser tabs.
  18. After a few minutes, refresh the browser tabs with the instance IP addresses, and you should now see a Linux Apache test page.
  19. Refresh the load balancer DNS browser tab, which should now also display the Linux Apache test page.

Testing Our Application Load Balancer and Auto Scaling Group

  1. In the EC2 instances browser tab, select the one in us-east-1b.

  2. Click Actions and select Instance State > Reboot.

  3. In the dialog, click Yes, Terminate.

  4. After a couple minutes, refresh the table to make sure its state changed to terminated.

  5. After another minute or so, we should see a new instance spun up to replace it.

  6. Select the us-east-1a instance, and click Connect at the top.

  7. Copy the chmod command in the dialog.

  8. Open a terminal window, change to your downloads directory, and paste in the command.

  9. Back in the AWS console, copy the ssh command in the connect dialog, and paste it into the terminal session.

  10. In the terminal, install the stress tool:

    sudo yum install -y stress
  11. Try to max out the CPU:

    stress --cpu 1 --timeout 300
  12. In the AWS console, select the other instance, and click Connect.

  13. Copy its ssh command.

  14. Open a terminal window, change to your downloads directory, and paste in the ssh command.

  15. Install the stress tool:

    sudo yum install -y stress
  16. Try to max out the CPU:

    stress --cpu 1 --timeout 300
  17. Back in the AWS console, refresh the instances table to see it's added another instance.

Conclusion

Congratulations on completing this hands-on lab!