Register EC2 Instances to more than one Elastic Load Balancer

For whatever reason, you may need to use more than one Elastic Load Balancer with your cluster of EC2 Instances; using a temporary domain that requires SSL for example (because you can only use one SSL certificate per ELB) where you need to point more than one domain with different SSL certificates to the same pool of web servers.

However, when creating an ELB you’ll notice that you cannot select any EC2 Instances that have been already assigned to another ELB. You can do this via the ELB API tools though.

Elastic Load Balancer API Tools

You need to download these to a computer with Java 1.5 or higher installed. You then need to set an environmental variable for where Java is located, which looks something like below; you can type this into the terminal:

export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/
export AWS_ELB_HOME=/home/ubuntu/ElasticLoadBalancing-1.0.14.3

If it worked, go into the bin directory of the ELB API tools and run:

./elb-cmd

This should list the commands available. Then you need to create a credential file, which you can do by copying the template provided:

cp credential-file-path.template credential-file-path
export AWS_CREDENTIAL_FILE=/path/to/credential-file-path

You need to put your super-secret Amazon credentials in here… Afterwards you should be able to do things like list your ELBs:

./elb-describe-lbs --headers

The output should look something like this (if you’ve got 2 load balancers)

LOAD_BALANCER  one  one.us-east-1.elb.amazonaws.com  2011-09-27...
LOAD_BALANCER  two  two.us-east-1.elb.amazonaws.com  2011-08-20...

If this lists your ELBs then you’re almost there! Then you’ve got to make sure the EC2 API Tools are working with your super-duper-secret credentials. You will need to following bash variables set:

export EC2_KEYPAIR=/where/the/key/is
export EC2_PRIVATE_KEY=/where/the/private/key/for/the/certificate/is
export EC2_CERT=/where/the/certificate/is
export JAVA_HOME=/where/is/the/java/jvm
export AWS_CREDENTIAL_FILE=/where/you/put/the/credential-file-path
export AWS_ELB_HOME=/where/you/put/the/elb-tools

You can either set these in something like .bash_profile or .bashrc or just execute them in your current bash session. Then, once all that’s working, you can register the EC2 Instances with the load balancer:

./elb-cmd elb-register-instances-with-lb \
load_balancer_name --headers --instances instance_1, instance_2, instance_3

The output of which should look something like:

INSTANCE_ID INSTANCE_ID
INSTANCE_ID instance_one
INSTANCE_ID instance_two
INSTANCE_ID intance_three

You can then end up with more than one ELB pointing to the same pool of web servers, meaning that you can have different domains pointing to each ELB, with their own SSL certificates, pointing to exactly the same code.

Configuring the Elastic Load Balancer

Once the EC2 Instances are registered with the ELB, you will need to assign the Availability Zones for those Instances with the ELB. This is because whilst they would normally be setup in the wizard, because you added them manually it will assign a single default Availability Zone, and you will need to make sure the Zones match your Instances.

Once the Availability Zones are set you will have to wait until the Instances are registered, and then all things being well the ELB should report them as healthy.

Resources I found useful:

3 comments

  • Database Load Balancer can be of different types and these are quite essential for achieving database sharding in order go for scalability of databases .The Elastic Database Load balancer seems to be perfect to achieve the desired results.

    • I don’t think we can use generic ELB for a sharded database that you mentioned. This will require a proxy layer that understands the data sharding logic. It usually the proxy service that comes with the database platform itself.