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)':
Next name the image (accept all defaults) and click ‘Yes, Create':
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:
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:
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:
Name the balancer something like ‘WPLoadBalancer’ and continue:
Accept the defaults here but be aware that normally you would make well-thought decisions here:
In this step select the two WordPress instances that we have running:
Check your choices in the overview and continue if it is correct:
Now you see the Load Balancer in your overview. Note it takes some time before the two instances are ‘In Service':
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:
sudo nano index.html
In the index.html enter the following text for one server:
<h1>Welcome to the first instance</h1>
and for the other:
<h1>Welcome to the second 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: