- Lab
- A Cloud Guru
Use Application Load Balancers for Web Servers
Load balancing automatically distributes your incoming traffic across multiple targets, such as EC2 instances, containers, and IP addresses, in one or more Availability Zones. In this lab, we configure an Application Load Balancer to distribute network traffic to two EC2 instances. We then enable stickiness, so that once a server is contacted, the user is always sent to that server. This ensures our legacy application continues to work despite not supporting distributed logins. By the end of this lab, the user will understand how to create an Application Load Balancer and enable sticky sessions.
Path Info
Table of Contents
-
Challenge
Observe the Provided EC2 Website and Create a Second Server
NOTE: While we generally recommend folloiwng the steps in the lab as shown, if you have trouble with using any of the public IPs referrenced in the lab, you can try using the public DNS name instead (Please be aware that DNS propogation delays are possible with this approach).
Observe the website configured on the existing EC2 server. Using the bootstrap script provided, boot another EC2 server.
For your new EC2 use the following settings:
- For your AMI, use Ubuntu Server 22.04 LTS.
Note: If you get a popup window that tells you some of your current settings will be changed, click on Confirm changes.
- Under Network settings, enable the option to Auto-assign Public IP.
- For the security groups, select the one with EC2SecurityGroup in its name (not the default security group).
- Add the following bootstrap script:
#!/bin/bash # Update and install necessary packages sudo apt-get update -y sudo apt-get install -y apache2 unzip # Fetching the token for IMDSv2 TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` # Starting HTML file echo '<html><center><body bgcolor="black" text="#39ff14" style="font-family: Arial"><h1>Load Balancer Demo</h1><h3>Availability Zone: ' > /var/www/html/index.html # Using the token to fetch metadata echo $(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/availability-zone) >> /var/www/html/index.html echo '</h3> <h3>Instance Id: ' >> /var/www/html/index.html echo $(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id) >> /var/www/html/index.html echo '</h3> <h3>Public IP: ' >> /var/www/html/index.html echo $(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4) >> /var/www/html/index.html echo '</h3> <h3>Local IP: ' >> /var/www/html/index.html echo $(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4) >> /var/www/html/index.html # Ending HTML file echo '</h3></html> ' >> /var/www/html/index.html # - Ensure the Apache2 service is enabled and started. sudo systemctl enable apache2 sudo systemctl start apache2
- For your AMI, use Ubuntu Server 22.04 LTS.
-
Challenge
Create an Application Load Balancer
Create a load balancer called LegacyALB with a target group called TargetGroup, add the two EC2 servers as targets, and observe how the load balancing works in your web browser.
-
Challenge
Enable Sticky Sessions
Configure the load balancer to use stickiness, and observe how the load balancer now only directs the user to a single EC2 server (as expected).
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.