EC2 Exercise 1.4: Host a Static Webpage behind a Load Balancer

Kerry Sheldon
6 min readMay 9, 2018

--

(This post is part of the AWS for the Bootcamp Grad series. The series consists of exercises to build familiarity with various aspects of the AWS ecosystem. Again, all of these posts are “exercises” for introductory exposure to AWS — they are NOT represented as best practices.)

Background

In this exercise, you will host a website behind a load balancer. The exercise will demonstrate how the load balancer distributes traffic across the multiple EC2 instances that are attached to it. It will also demonstrate how the load balancer behaves when one of the instances becomes unhealthy/unable to respond to requests.

Specifically, you will do the following:

  • Access metadata for an EC2 instance
  • Create multiple EC2 instances that run a shell script on launch
  • Create a load balancer and attach the EC2 instances to it
  • Monitor the behavior of the load balancer when one of the EC2 instances becomes unhealthy

Access Metadata for an EC2 Instance

In the later steps of this exercise, you will attach multiple EC2 instances to a load balancer and observe how traffic is routed to each of the the “attached” instances. To observe that behavior, you will display the IP address of the EC2 instance that served the request.

In this step, you will create an EC2 instance simply to learn how to access EC2 metadata. Follow the steps in the ‘Launch an EC2 Instance’ section of Exercise 1.1 to create an instance.

Within an instance, metadata is available at the following URL: http://169.254.169.254/latest/meta-data/

Before you SSH into your new EC2 instance, try to run the following command from your terminal (note: you will need to kill it with a Ctrl-C because it will hang).

curl http://169.254.169.254/latest/meta-data/

After you kill that command, SSH into the EC2 instance as you have in previous exercises. Then run the same command.

This time, you will get a response listing available meta-data items. For example, you will be able to get the public IP address of the instance with the following command:

curl http://169.254.169.254/latest/meta-data/public-ipv4

Feel free to access other metadata items from the list. Once you are done, exit the SSH session and terminate this EC2 instance from the AWS console. You will be creating new instances in the next section.

Create Multiple EC2 Instances that Run a Shell Script on Launch

In this step, you will create two new EC2 instances from the AWS console simultaneously. You will follow the same steps as described in the ‘Launch an EC2 Instance’ section of Exercise 1.1 — with the exception of the following two changes to Step 3 Configure Instance Details:

  • Enter 2 for “Number of Instances”
  • Expand the “Advanced Details” section and paste the following text into the “User Data” input box:
#!/bin/bash
yum update -y
yum install httpd -y
service httpd start
chkconfig httpd on
cd /var/www/html
echo "<html><body>IP address of this instance: " > index.html
curl http://169.254.169.254/latest/meta-data/public-ipv4 >> index.html
echo "</body></html>" >> index.html

You should recognize many of these commands from the previous exercises. In those exercises, you entered these commands once you SSH’d into the instance. By adding this script as “user data”, these commands will be run when the instance is launched (see documentation here).

A few of these commands that are new/different:

  • #!/bin/bash : if you’ve written a bash script before, you will recognize this line. #! is known as a shebang and this line tells the EC2 instance to use the bash shell to run the rest of the commands (aka the script).
  • NO sudo su : in the previous examples, you ran sudo su within the instance. In this case, the script is run as the root user and this not needed.
  • The final three commands build the index.html file that will be served at the root path of your webpage. The second of these lines is familiar from the previous step in this exercise. This command redirects and appends the output of the curl command into the html file. In this case, the output is the public IP address of the instance.

Complete the rest of the EC2 launch process as you have in previous exercises.

Create a load balancer and attach the EC2 instances

Make note of the availability zone of the EC2 instances that you just created — you will need to know it as you set up your load balancer.

From the EC2 dashboard, select ‘Load Balancers’ from the sidebar on the left. Click ‘Create Load Balancer’, then ‘Create’ an Application Load Balancer.

Step 1: Configure Load Balancer

Enter a name of your choice for the load balancer (e.g. MyLoadBalancer).

Under availability zones, select the availability zone of your EC2 instance and one other of your choice (in this example, this second selection is moot. See here for more information on availability zones for load balancers ).

Click Next: Configure Security Settings.

Step 2: Configure Security Settings

Don’t worry about the warning, click Next: Configure Security Groups.

Step 3: Configure Security Group

Select the Security Group that you’ve been using for creating your EC2 instances.

Click Next: Configure Routing.

Step 4: Configure Routing

In the this step, you are telling the load balancer how to determine if an ‘attached’ instance is healthy and capable of receiving requests (The attached instances are known as targets, and together they are known as a ‘Target Group’).

Enter a name of your choice for the target group (e.g. MyTargetGroup).

The Health Check section defines how the load balancer will determine whether it should route traffic to the instances in the target group. It will route traffic to an instance as long as it is ‘healthy’.

The protocol and path section of this step dictate the type of request that will be made to determine the health of the instance. Accept the default options, meaning that an HTTP request to the root path will be utilized to determine the health of the instance.

In advanced health check settings, you can describe the process that should be followed in order to evaluate the health of the instance. Hover over the information icons to get more details for each setting. You can change the healthy threshold, unhealthy threshold, timeout and interval options to the ‘lowest’ possible setting.

Click Next: Register Targets

Step 5: Register Targets

In the instances section of this page, select both of your EC2 instances and click “Add to registered”. Then click Next: Review and then Create.

Observe the Behavior of the Load Balancer

It will take time for your load balancer to provision. From the Load Balancer dashboard, wait until the State of your new load balancer is ‘active’. Copy/paste the DNS name of the load balancer into your browser. If all went well, you should see the following html: IP address of this instance: <an_ip_address>

Refresh the page multiple times and note how the IP address changes. This demonstrates that the load balancer is routing traffic to both of your instances.

After you’ve done that, SSH into either one of the EC2 instances and run the following commands to remove the index.html file that is served at the root path of your website.

sudo su
rm /var/www/html/index.html

Return to your browser and refresh the page multiple times. Now, you should only see the IP address of the ‘other’ EC2 instance.

As you may recall, the load balancer was configured to check the health of the instance at the root path. Because you deleted the index.html file, the EC2 instance is not sending a success response from the root path, and the instance is considered unhealthy.

Go back to the AWS console. Click on ‘Target Groups’ in the sidebar of the EC2 dashboard. Click on the ‘Targets’ tab and you will see that one of your EC2 instances has an ‘unhealthy’ status. Click on the ‘Monitoring’ tab and can see the graph of Unhealthy Hosts and Healthy Hosts.

Clean Up

Navigate back to the Load Balancers section of the EC2 dashboard. Click Actions → Delete on the Load Balancer you created. Navigate to the Target Groups section. Click Actions → Delete on the Target group you created

Navigate to the instances section of the EC2 dashboard, select both instances, and click on Actions. Select Instance State → Terminate. Confirm that you want to terminate and you’re done.

Next

EC2 Exercise 1.5: Using an EC2 Instance and RDS to Host a Rails app Built on the Instance (coming soon)

--

--

Kerry Sheldon

Software Developer. Graduate of Turing School of Software and Design.