Share Testing Your AWS Elastic Load Balancer
Tuesday, July 27th, 2010
Vijay Ramachandran asked me, via twitter, how to test if an Amazon Elastic Load Balancer is really doing it’s job. Because 140 characters really isn’t sufficient space to handle this answer, I’ve created this post. Feel free to use any of this in any of your environment.
First, I’ll assume you’ve covered some of the basics with ELB.
- Scaling Out eith EC2, CloudWatch, AutoScaling and Elastic Load Balancing
- 3 Amazon Elastic Load Balancer Tips
- Elastic Load Balancing in Multiple Zones
- Elastic Load Balancing Commands
The default configuration you’ll end up with following my guides above is a stateless system that distributes the requests more or less evenly across all configured servers. However, when you do it the first time, it’s nice to see that it’s actually doing what you think it should be. The steps are simple
- Verify each instance is working as expected
- Verify the load balancer is distributing the requests across multiple instances
- Verify the instances are working behind the load balancer
1. Verify each instance is working
This is far and away the easiest step. You can simply access each machine by the amazon assigned IP address for that specific instance and ensure that it’s doing what you expect. The only potential issue here is you might jump from one machine to a different machine if you are not watching your URL. For example, if you are on ec2-123-123-123-123.compute-1.amazonaws.com, access your application at that address and ensure it works as expected, if it jumps to a domain name because you’ve hard coded a link somewhere, you may not be testing the new server at all.
2. Verify the load balancer is distributing the requests across multiple instances
To test that requests are being distributed across multiple machines, I use a test file. I generate my test file automatically by running the following script as part of the boot-up routine. This simply saves the instance-id from the metadata into a text file. If you are uncomfortable placing this information in the web root, you can optionally place it behind basic authentication, put it into a script that hashes it (md5 or sha1) or some other application based logic to access it.
/usr/local/bin/curl http://169.254.169.254/latest/meta-data/instance-id > /var/www/html/instance-id.txt |
Check the path for curl and the web root for your local system and adjust accordingly. This should work from RedHat flavored distributions.
Once you’ve run this on each of your instances, you can tell that requests are being distributed to both machines by simply requesting your load balancer address and verifying that it changes. (Obviously replace the following request with the correct address for your machine.)
http://applicationservers-123456789.us-east-1.elb.amazonaws.com/instance-id.txt
3. Verify the instances are working behind the load balancer
Now for the last and final test. Confident that your requests are being distributed across both machines, test that your application works as expected. First under the Amazon assigned name, applicationservers-123456789.us-east-1.elb.amazonaws.com in this example, then under your CNAME’d alias.
If everything still works, you can assume all is good.
4. Bonus Check
If you really, really, really want to know… you can also verify using your access logs. Check in /var/log/httpd/access_log or wherever your web server logs are kept to see that requests are being distributed to each machine.
DNS Tips:
1. Never use the real IP returned from dig or nslookup as an A record in DNS unless you automate checking it (and even still I wouldn’t) because the actual IP changes from time to time. Only use CNAME entries.
2. If you are using GoDaddy’s DNS tool, you can’t CNAME the root of a domain (ie .example.com). For this case I use one instance as a permanent instance with an elastic IP and point the root A record for my domains to this. I then assign www. as a CNAME for the load balancer’s AWS assigned domain. Last but not least, I use .htaccess and mod_rewrite to ensure requests are sent to www.example.com. This ensures traffic is being sent to the load balancer address.


