Cloud Zone is brought to you in partnership with:

Pascal is a senior JEE Developer and Architect at 4Synergy in The Netherlands. Pascal has been designing and building J2EE applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Specialties: JEE XML Web Services Mule ESB Maven Cloud Technology Pascal is a DZone MVB and is not an employee of DZone and has posted 56 posts at DZone. You can read more from them at their website. View Full User Profile

Load Balancing with Amazon Elastic Load Balancer

01.22.2013
| 5968 views |
  • submit to reddit

In the previous posts I set up a WordPress server by using several Amazon’s services. In this post I add the next AWS feature, the Elastic Load Balancer. Since we made our EC2 server running WordPress ‘stateless’ it is quite easy to load balance it. To do so we will create an AMI out of our (customized) WordPress server (with the SNS setup) and use this AMI to set up a second instance of our WordPress server. Put a load balancer in front and that’s it.
So first create a custom AMI of our current EC2 WordPress server. To do this we select the EC2 instance in the overview and right-click. Then select the option ‘Create Image (EBS AMI)’:
Screen Shot 2012-12-25 at 14.38.06
Next name the image (accept all defaults) and click ‘Yes, Create’:
Screen Shot 2012-12-25 at 14.39.31
This will stop the server so a snapshot can be made and the image will be created. The server will boot again and you will receive a mail it is started (at least if you set up the SNS as I described here).
When the image is created you will see an AMI in the overview:
Screen Shot 2012-12-25 at 14.55.57

Lets test the AMI by launching it as a second instance. Select the AMI and click ‘Launch’:
Screen Shot 2012-12-25 at 15.00.13

In the following steps name the instance differently and use the same security group and key pair as the original one. At the end of the wizard you should have something like this:
Screen Shot 2012-12-25 at 15.03.09
If all is correct then launch the instance. When you received the mail it is started make sure you can access the server by SSH and the web application is up and running.

Now lets create the load balancer in front of these two instances. Go to the AWS EC2 management page and click the ‘Load Balancers’ menu item:
Screen Shot 2012-12-30 at 17.26.54
Name the balancer something like ‘WPLoadBalancer’ and continue:
Screen Shot 2012-12-30 at 17.29.51
Accept the defaults here but be aware that normally you would make well-thought decisions here:
Screen Shot 2012-12-30 at 17.30.13
In this step select the two WordPress instances that we have running:
Screen Shot 2012-12-30 at 17.32.54
Check your choices in the overview and continue if it is correct:
Screen Shot 2012-12-30 at 17.33.11
Now you see the Load Balancer in your overview. Note it takes some time before the two instances are ‘In Service’:
Screen Shot 2012-12-30 at 17.43.57

Next we make a distinction in the homepage of the two servers and simply refresh the page in our browser so we see which server is processing the request. Login to both servers with SSH and enter the following command:
cd /opt/bitnami/apache2/htdocs/
sudo nano index.html
In the index.html enter the following text for one server:
<h1>Welcome to the first instance</h1>

Now look up the public DNS of the load balancer and open it in a browser window and refresh. If you don’t see any difference stop the server that is showed and refresh your browser. Of course there are better ways to test the load balancing but I just want to show how simple it is to set up with AWS:
Screen Shot 2012-12-30 at 20.09.44
Screen Shot 2012-12-30 at 20.37.27
Published at DZone with permission of Pascal Alma, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)