Creating an Application Load Balancer from the AWS CLI

Hands-On Lab

 

Photo of Craig Arcuri

Craig Arcuri

AWS Training Architect II in Content

Length

01:00:00

Difficulty

Advanced

In this Learning Activity, students will learn to create an Application Load Balancer from the Command Line Interface. It only takes five commands, offers some speed advantages, and can ultimately be scripted. From the CLI, students can create and configure the ALB, Target Groups, Targets, Listeners, and Health Checks. There is a specific sequence for creating the Application Load Balancer, meaning fewer configuration issues get introduced. This Learning Activity will provide students with the opportunity to perform some realistic troubleshooting on the ALB.

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.

Creating an Application Load Balancer from the AWS CLI

Introduction

In this hands-on lab, we will create an Application Load Balancer from the command line interface.

Solution

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

Log in to the live AWS environment using the credentials provided, and make sure you're in the N. Virginia (us-east-1) region. We'll mostly use the AWS console for monitoring throughout the lab while we run commands in the CLI.

Creating an Application Load Balancer from the CLI

  1. In the AWS console, navigate to EC2.
  2. Click the running instances link.

Create First Instance

  1. On the instances page, click Launch Instance.
  2. On the AMI page, select the Amazon Linux 2 AMI.
  3. Leave t2.micro selected, and click Next: Configure Instance Details.
  4. On the Configure Instance Details page:
    • Network: Leave default
    • Subnet: us-east-1b
    • Auto-assign Public IP: Enable
  5. Click Next: Add Storage, Next: Add Tags, and Next: Configure Security Group.
  6. Click to Select an existing security group.
  7. Select the one provided in the table (not the default security group).
  8. Click Review and Launch, and then Launch.
  9. In the key pair dialog, select Create a new key pair.
  10. Give it a Key pair name of "albLab".
  11. Click Download Key Pair, and then Launch Instances.
  12. Click View Instances.

Create Second Instance

  1. On the instances page, click Launch Instance.
  2. On the AMI page, select the Amazon Linux 2 AMI.
  3. Leave t2.micro selected, and click Next: Configure Instance Details.
  4. On the Configure Instance Details page:
    • Network: Leave default
    • Subnet: us-east-1a
    • Auto-assign Public IP: Enable
  5. Click Next: Add Storage, Next: Add Tags, and Next: Configure Security Group.
  6. Click to Select an existing security group.
  7. Select the one provided in the table (not the default security group).
  8. Click Review and Launch, and then Launch.
  9. In the key pair dialog, select Choose an existing key pair and make sure albLab is selected.
  10. Click Launch Instances.
  11. Click View Instances, and give the instances a few minutes to enter the running state.

Log in to Admin Instance

  1. Select the AdminInstance listed.

  2. Copy the public IP address for it.

  3. In the terminal window, log in to the admin instance via SSH:

    ssh linuxacademy@<PUBLIC IP OF ADMININSTANCE>

    The password is 123456.

  4. Verify that the admin instance has the AWS CLI:

    aws elbv2 help

    Hit q to quit out of it.

  5. Let's set the region. Run:

    aws configure
  6. Then, set the following values:

    AWS Access Key ID [None]:
    AWS Secret Access Key [None]:
    Default region name [None]: us-east-1
    Default output format [None]:

Create the Load Balancer

  1. In the AWS console, select one of the instances we created.
  2. Copy its subnet ID in the Description section below, and paste it into a text file.
  3. Repeat this process for the other instance.
  4. Click Security Groups in the left-hand menu.
  5. Select the one we set for our instances (not the default one).
  6. Copy its group ID in the Description section below, and paste it into a text file.
  7. In the terminal, enter the following command (replacing <SUBNET 1 ID>, <SUBNET 2 ID>, and <SECURITY GROUP ID> with the IDs you just copied):

    aws elbv2 create-load-balancer --name alblab-load-balancer  --subnets <SUBNET 1 ID> <SUBNET 2 ID> --security-groups <SECURITY GROUP ID>

Create a Target Group

  1. In the AWS console, right-click Load Balancers in the left-hand menu to open it in a new browser tab.

  2. Copy its VPC ID and paste it into a text file.

  3. Also, copy and paste its ARN into a text file, as we'll need it later.

  4. In the terminal, enter the following command (replacing <VPC ID> with the ID you just copied):

    aws elbv2 create-target-group --name my-targets --protocol HTTP --port 80  --vpc-id <VPC ID>
  5. In what's returned, copy and paste the TargetGroupArn into a text file for the next command.

Register the Targets

  1. In the AWS console, navigate to our EC2 instances.
  2. Select one of the instances we created.
  3. Copy its instance ID in the Description section below, and paste it into a text file.
  4. Repeat this process for the other instance.
  5. In the terminal, enter the following command (replacing <TARGET GROUP ARN> with the TargetGroupArn you just copied, as well as replacing <INSTANCE 1 ID> and <INSTANCE 2 ID> with the instance IDs):
    aws elbv2 register-targets --target-group-arn <TARGET GROUP ARN>   --targets Id=<INSTANCE 1 ID> Id=<INSTANCE 2 ID>

Create a Listener

  1. In the terminal, enter the following command (replacing <LOAD BALANCER ARN> with the one you copied earlier and <TARGET GROUP ARN> with the TargetGroupArn):

    aws elbv2 create-listener --load-balancer-arn <LOAD BALANCER ARN>  --protocol HTTP --port 80   --default-actions Type=forward,TargetGroupArn=<TARGET GROUP ARN>

Perform a Health Check

  1. Enter the following command (replacing <TARGET GROUP ARN> with the TargetGroupArn)

    aws elbv2 describe-target-health --target-group-arn <TARGET GROUP ARN>

Troubleshooting the Application Load Balancer

  1. In the AWS console, in EC2, click Target Groups in the left-hand menu.
  2. In the Targets tab below, we'll see the status for the instances is unhealthy. To rectify this, we need to configure out instances as web servers.

Configure Instances as Web Servers

Configure First Instance as a Web Server
  1. Log out of the admin instance:

    exit
  2. Make sure you're in your downloads directory.

  3. On the instances page of the AWS console, select one of our instances and click Connect.

  4. Copy the chmod command in the dialog, and run it in the terminal.

  5. In the connection dialog, copy the ssh command, and run it in the terminal.

  6. Run a YUM update:

    sudo yum update -y
  7. Install Apache:

    sudo yum install -y httpd
  8. Ensure the web server starts if the instance is rebooted:

    sudo service httpd start
  9. Automate the web server starting:

    sudo chkconfig httpd on
  10. Log out of the instance:

    exit
Configure Second Instance as a Web Server
  1. On the instances page of the AWS console, select our other instance and click Connect.
  2. Copy the ssh command, and run it in the terminal.
  3. Run the same series of commands:

    sudo yum update -y
    
    sudo yum install -y httpd
    
    sudo service httpd start
    
    sudo chkconfig httpd on

Check Instances and Load Balancer

  1. In the AWS console, select the first instance you configured.
  2. Copy its public IP address and paste it into a new browser tab.
  3. You should see the Apache test page. If not, check the ingress on the instance's security group, which should be HTTP on port 80.
  4. In the AWS console, navigate to our load balancer.
  5. Copy its DNS name and paste it into a new browser tab, which should result in the Apache test page.
  6. In the AWS console, navigate to our target groups.
  7. We'll see the instances' statuses are still unhealthy.
    • The health check for the application balancer is looking for a return code of 200, but it isn't getting it. We need to set up an index.html page for each of our instances to return the 200 code to the application load balancer, causing our health checks to pass.

Configuring EC2 instances and Checking Health Checks

Set Up index.html Pages for Instances

First Instance
  1. In the terminal, where we should still be logged in to one of the instances, enter the following:

    cd /var/www/html
  2. Create the page:

    sudo touch index.html
  3. Set up the file for read and write permissions:

    sudo chmod 777 index.html
  4. Open the file:

    vim index.html
  5. Add the following to the file:

    <html>
     <head>
      <title>ELB Heartbeat</title>
     </head>
     <body>
     ?php echo '<p>OK</p>';  
     </body>
    </html>

    Save and exit the file.

  6. Log out of the instance:

    exit
Second Instance
  1. On the instances page of the AWS console, select our other instance and click Connect.

  2. Copy the ssh command, and run it in the terminal.

  3. Run the same series of commands:

    cd /var/www/html
    
    sudo touch index.html
    
    sudo chmod 777 index.html
    
    vim index.html
  4. Add the following to the file:

    <html>
     <head>
      <title>ELB Heartbeat</title>
     </head>
     <body>
     ?php echo '<p>OK</p>';  
     </body>
    </html>

    Save and exit the file.

  5. Log out of the instance:

    exit

Check Health Checks

  1. In the AWS console, select the admin instance and copy its public IP address.

  2. In the terminal, log in to the admin instance:

    ssh linuxacademy@<PUBLIC IP OF ADMININSTANCE>

    The password is 123456.

  3. In the AWS console, refresh the target groups page to check the health. Both instances should have a healthy status.

  4. In the terminal, run the target health check command:

    aws elbv2 describe-target-health --target-group-arn <TARGET GROUP ARN>
  5. In the AWS console, navigate to our load balancer.

  6. Copy and paste its DNS name into a new browser tab, which should result in our "OK" message.

Conclusion

Congratulations on completing this hands-on lab!