Friday, May 15, 2015

Configuring and testing AWS Elastic Load Balancer

Load balancing is an essential component for the scalability and fault tolerance of web applications. Major cloud computing providers have different offerings for load balancing.
In this post I'll explore AWS's (Amazon Web Services) ELB (Elastic Load Balancing) feature, and test it to see how it distributes the load on front-end web servers, and in case of unavailability of one of the front-end servers, how traffic is directed to the healthy instance(s).

I'll use Linux based image, but the concepts apply to Windows images. I assume that the reader has the basic knowledge on how to create an AWS account and create EC2 (Elastic Compute Cloud) virtual machine. If not, don't worry, following the steps below will give you a good understanding.

So the experiment goes as follows:

1- Create a base image for front-end web servers: 


  1. Go to AWS console and select "Launch Instance", from the list of images, select "Ubuntu Server 14.04 LTS".
  2. Complete the wizard till you reach the "Configure Security Group" step. In this is the step we select the proper ports we need AWS to open. Select SSH (22) to connect to the instance to configure it, and HTTP (80) to serve web traffic.
  3. When you're prompted to select the key pair, make sure to choose an existing one you have already downloaded or create a new one and keep it in a safe place.
  4. Then Launch the instance.

Note: When I first stared using AWS, and being from a windows background, the term "Security Group" was a bit confusing to me, it's about firewall rules not security groups in the sense of Active Directory Groups.

2- Configure Apache web server

The image does not have a web server installed by default, so I'll SSH into the instance and install it.
If you're using MAC or Linux, you should be able to run SSH directly. For Windows users, you can use Putty.
  1. Copy the public IP of the running instance you just created.
  2. Use SSH to connect using this command:  ssh -l ubuntu -i . for example: ssh -l ubuntu -i mykey.pem . note that ubuntu is the username for the image we created this machine from. the .pem file acts as a password.
  3. Now we are inside the instance. It's time to install and configure Apache:

sudo su
apt-get install apache2
sudo a2enmod cgi
service apache2 restart

The above commands simply do the following:
  • Elevate privileges to run as a super user to be able to install software.
  • Install apache using the package manager.
  • Enable CGI, I'll show you why later
  • Restart apache so that CGI configuration takes effect.

Now it's time to test the web server. Visit http://INSTANCE_IP and you should be welcomed with the default apache home page.

3- Create a script to identify the running instance

To test ELB, I need to identify which instance served the request just by looking into the response to a web request. Now I have 2 options: Create static pages on each web fron-end or create some dynamic content that identifies the instance. And I prefer the latter option as I'll use the same image for all front-ends.
EC2 has a nice feature called instance metadata. It's an endpoint accessible from within EC2 instances that can be called to get information about it. From SSH terminal try:


A list of available meta-data will be shown:


Appending any of them to the URL will show the value. For example:


And I'll use these two meta-data items to identify the instances by showing them within a bash script and then serve it from apache. cd into /usr/lib/cgi-bin

cd /usr/lib/cgi-bin

This is the default location that apache uses to serve CGI content. That's why I enabled CGI in a previous step.
in that folder I'll create a bash script that shows the output of the meta-data. use any text editor. For example run nano in the command line and paste the below script:


echo "Content-type: text/text"
echo ''
echo 'Host name:'
echo ''
echo 'Public IP:'

If using nano, ctrl+X, y. save as

Now we need to grant execute permission on this file:

chmod 755 /usr/lib/cgi-bin/

To test the configuration, browse to http://INSTANCE_IP/cgi-bin/
My results look like:

Host name:
Public IP:

Note: I'm not advising using bash scripts in production web sites. It just was the easiest way to spit out info returned from the meta-data endpoints with minimal effort.

4- Create 2 more front-ends

Now we have an identifiable instance. Let's create more of it.
  1. Stop the instance from the management console
  2. After the instance has stopped, right click -> image -> create image.
  3. Choose and appropriate name and save.
  4. Navigate to AMI (Amazon Machine Image) and check the creation status of the image.
  5. Once the status is available click launch
  6. In the launch instance wizard, select to launch 2 instances
  7. Select the same security group as the one used before, it will have both 22 and 80 ports open.
  8. Start the original instance. 
  9. Now we have 3 identical servers.
  10. Using the IP address of any instance, navigate to the CGI script, for example:
Note that most probably the IP of the first instance is now different after restart.

5- Create an ELB instance

  1. In AWS console, navigate to "Load Balancers".
  2. Click "Create Load Balancer"
  3. Make sure it's working on port 80
  4. Select the same security group
  5. In the health check, in the ping path, enter "/". This means that ELB will use the default apache page for health check. In production, it might not be a good idea to make your home page the health check page.
  6. For quick testing, make the "Healthy Threshold" equal to 3.

Now a bit of explanation is required. This configuration tells ELB to check for the healthiness of a front-end instance every 30 seconds. A check is considered successful if the server responds in 5 seconds.
If a healthy instance does not respond with that period for 2 consecutive failures, it's considered unhealthy. And similarly, an unhealthy instance is considered healthy again if it responds to the check 3 consecutive times.

Now select the 3 instances to use for load balancing. And wait until the ELB instance is created and the 3 instances in the "instances" tab are shown InService.

Now in the newly create ELB, select the value of the DNS name (like and navigate to the URL of the metadata page. My url looked like:

The data displayed in the page will belong to the instance that actually served the request. Refresh the page and and see how the response changes. In my case ELB worked in a round robin fashion and the responses where:

Host name:
Public IP:

Host name:
Public IP:

Host name:
Public IP:

Inspect the network response using F12 tools and note the headers:

HTTP/1.1 200 OK
Content-Type: text/text
Date: Sat, 16 May 2015 19:12:38 GMT
Server: Apache/2.4.7 (Ubuntu)
transfer-encoding: chunked
Connection: keep-alive

Note: nothing special as there is no session affinity.

6- Bring an instance down

Now, let's simulate an instance failure. Let's simply stop the apache service on one of the 3 front-ends. So ssh into one of the 3 instances and run:

sudo service apache2 stop

Refresh the page pointing to the ELB url, note that after a few seconds, you only get responses from the 2 running instances. After about 1 minute, the instance is declared OutOfService in the Instances tab of ELB.


7- Bring it back!

This time, turn on apache service by running:

sudo service apache2 start

Wait about one and half minutes, the instance is back to InService status and you start to get responses from it.
The "Healthy Hosts ( Count )" graph shows a very good representation of what happened:

8- Turn them all off!

They are costing you money, unless you are still under the free tier. It's recommended to terminate any EC2 and ELB instances that are no longer used.

If you intend to leave some instances alive, it's recommended to de-register the instance from ELB when shut down:



In this post, we've seen ELB in action using its basic settings. The round robin load balancing worked great and health check made our site available to users by eliminating unhealthy instances.
This works great with web applications that don't require session affinity, for applications that require it, well, that's another post.


Suresh Triapthi said...

Thanks for providing this informative information. it is very useful you may also refer-

AWS on premise said...

Thanks for sharing such a useful information with all of us. It is nicely explain with steps.

meghanasmily03 said...

I am following your blog from the beginning, it was so distinct & I had a chance to collect conglomeration of information that helps me a lot to improvise myself. I hope this will help many readers who are in need of this vital piece of information. Thanks for sharing & keep your blog updated.Regards aws jobs in hyderabad.

aws jobs in hyderabad said...

I admire the valuable information you offer in your articles. I will bookmark your blog and have my friends check up here often. I am quite sure they will learn lots of new stuff here than anybody else! Regards aws jobs in hyderabad.

Nandhini said...

Thanks for taking time to share this post. It is really useful. Continue sharing more like this.
Amazon Web Services Training in Chennai

bhargava sai said...

students of this generation are very lucky because amazon is going to provide more career oppurtunities in cloud platform. students are not able to identify the best institutes to learn amazon web services. here are some of the top aws training in hyderabad institutes with excellent lecturures in the aws field

bhargava sai said...
This comment has been removed by a blog administrator.
Devikala said...

Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much.
QA QC courses in Chennai
Best Non Destructive Testing Courses in Chennai


Thank you a lot for providing individuals with a very
spectacular possibility to read critical reviews from this site.

AWS Training in Bangalore

AWS Training in Bangalore

Devikala said...

Such a great post. Thanks for sharing. NDT Certification in Chennai
Non Destructive Testing Course in Chennai

Anonymous said...

I admire the valuable information you offer in your articles.

online automation testing training

kevingeorge said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

Amazon Web Services Training in Chennai

rakesh manu said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Aws Training in Hyderabad

samvel sam said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. aws training in chennai

Nicole kristen said...

Thanks for this great share. This post is much helpful for us.Excellent Blog! This is very helpful for who wants to learn professional Education.Thank you. Please keep Going. I am waiting for your more posts like this or related to any other informative topic.
AWS Elastic Load Balancing

bhargava sai said...

brillant piece of information, I had come to know about your web-page from my friend hardkik, chennai,i have read atleast 9 posts of yours by now, and let me tell you, your webpage gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanx a million once again, Regards

best aws training in hyderabad

ajay. chinna said...

Thanks for sharing the info, Salesforce is the best platform for all organizations to perform the multiple tasks at a time
Best Salesforce Training
Best Salesforce Online Training

Lathika Honey said...

Thanks For Sharing Valuable Information, Very Useful For U Mulesoft Certification Training

Ananya Krishnan said...

IT leaders want to be able to ensure high-availability during periodic and seasonal peaks, but that often requires an overinvestment in infrastructure to ensure capacity can meet demand. Excess capacity causes overspending and waste.

aws training in OMR | aws certification training in Chennai | aws course in chennai what is the qualification

john jersy said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

mounika sharma said...

Informative blog thanks for providing such a great information.
For more information about aws please click here: AWS Training In Hyderabad

LindaJasmine said...

The Blog is really brilliant. Thanks for the information provided.
AWS Training in Chennai
Best AWS Training in Chennai
AWS Training
AWS course in Chennai
Aws Certification in Chennai
Amazon Web Services Training in Chennai
AWS Training center Chennai

Rohil singh said...

Very interesting blog which helps me to get the in depth knowledge about the technology, Thanks for sharing such a nice blog..
Good discussion.
Six Sigma Training in Abu Dhabi
Six Sigma Training in Dammam
Six Sigma Training in Riyadh

likitha said...

Nice post,and good information Thanks for sharing
further check it once at AWS Online Training

mahesh chemmala said...

Thanks for providing such a great information.
AWS Online Training

jyothi kits said...

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
Oracle DBA Online Training

Oracle Rac Online Training

Surendra SriyaEdu said...

Amazon has a simple web services interface that you can use to store and retrieve any amount of data, at any time, from anywhere on the web. Amazon Web Services (AWS) is a secure cloud services platform, offering compute power, database storage, content delivery and other functionality to help businesses scale and grow.For more information visit.
aws online training
aws training in hyderabad
amazon web services(AWS) online training
amazon web services(AWS) training online

Xplore IT Corp said...

Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
dot net course training in coimbatore
php training in coimbatore

Neelima said...

Nice post, good information, this information is very useful, you explained a lot about this topic. We provide this AWS training with realtime exports and 100% placement program. For more details visit our website AWS Training

Akshitha Naidu said...

I just want to say that all the information you have given here is awesome...great and nice blog thanks for sharing..Thank you very much for this one
Aws training In Hyderabad

sumathi s said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
fire and safety course in chennai

Swetha Gauri said...

Nice blog has been shared by you. it will be really helpful to many peoples who are all working under the technology. Thank you for sharing this blog.
safety course in chennai

zara said...

One of the most preferred professional course AWS Training in Chennai

Muralidhara Raju Indukuri said...

Cloud computing is one of the famous course and know about aws training in hyderabad

alex said...

Want to play at the most popular online casino? Then come to us and abrai your winnings as soon as possible. excellent online slot machines Come in and try your luck.

Belago said...

Лучшый профиль для светодиодной ленты в СНГ вы можете купить у нас в Ekodio

Kamila said...

I like you post.It is very usfull to all.
In this post having more usefull information and improve our knowledge.Can you share more valuable information present Technology.
Dot Net Training in Chennai
Selenium Testing Training in Chennai
Software Testing Training Institute in Chennai
Core Java Training in Chennai
PHP Course in Chennai

Sanvi said...

Thanks for sharing this blog with us. I really enjoy reading your post. Keep Doing.

AWS Training in Hyderabad

service care said...

I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating for us.
lg mobile service center
lg mobile service chennai
lg mobile repair