Scalr: Find farm’s load balancer hostname


As you can’t point the root of a domain to an EC2 load balancer yet, we’ve decided to have a small Apache instance with an elastic ip attached to it and have it proxy all the incoming traffic to the load balancer, which in turn distributes to the actual web instances we’re running.

This way we’re able to point both the root domain (.domain.com) and any of its subdomains (www.?) to the Apache proxy and still have the application load balanced by ELB.

The only issue with this is, I haven’t found any way of programatically getting the ELB hostname on a Scalr instance, and without this, the Apache proxy would be pointless as it wouldn’t know where to direct the traffic.

So I put together this little script which installs Amazon’s ELB api tools, and using those in combination with the $FARMID variable already available from the Scalr environment, I’m able to find my load balancer.

# Download and setup ELB tools
cd ~/
curl -L -o "elb.zip" "http://ec2-downloads.s3.amazonaws.com/ElasticLoadBalancing.zip"
unzip elb.zip
 
mv ElasticLoadBalancing-1.0.9.3 /usr/local/aws/ec2-elb-tools
 
echo "export AWS_ELB_HOME=/usr/local/aws/ec2-elb-tools" >> ~/.profile
echo 'export PATH="$PATH:$EC2_ELB_HOME/bin"' >> ~/.profile
 
. ~/.profile
. /etc/aws/farmconfig
 
LOAD_BALANCER_HOSTNAME=`elb-describe-lbs | grep $FARMID | awk '{ print $3}'`
cat > /etc/apache2/sites-available/proxy.conf <<-PROXY
<VirtualHost *:80>
	ProxyRequests Off
	ProxyVia On
 
	ServerName                      $PROJECT_HOSTNAME
	ServerAlias                     $PROJECT_HOSTNAME_ALIAS
 
	<Directory proxy:*>
		Order deny,allow
		Allow from all
	</Directory>
 
	<Proxy *>
		AddDefaultCharset off
		Order deny,allow
		Allow from all
	</Proxy>
 
	ProxyPass / http://$LOAD_BALANCER_HOSTNAME/
	ProxyPassReverse / $LOAD_BALANCER_HOSTNAME/
 
</VirtualHost>
PROXY

The PROJECT_HOSTNAME and PROJECT_HOSTNAME_ALIAS variables are passed as instance parameters when configuring the role.

Do note that this solution only works when you have 1 load balancer in a farm.

Topics: Development Tags: ,
Comments: none so far

Post a Comment

Your email is never shared. Required fields are marked *

*
*