Monday, December 14, 2009

Amazon Elastic Load Balancing (ELB) is pretty cool

Using this service costs $0.025/hour so it may make sense to just run HAProxy yourself on a EC2 instance, but then you have to worry about fault tolerance/recovery if that instance fails. The ELB cost is small in comparison to running a cluster of EC2 instances and "outsourcing" as much of your system as possible to AWS (e.b., SimpleDB, Elastic Load Balancing, Relational Database Server, EBS, etc.) can certainly reduce both the complexity of your application architecture and also your implementation costs.

Here are my notes for a simple ELB setup for an AMI that contains a Rails web application:
export EC2_PRIVATE_KEY=pk-....pem  # different on your system
export EC2_CERT=/Users/markw/.ec2/cert-...pem # different on your system

ec2run -k gsg-keypair ami-e767ds71
ec2run -k gsg-keypair ami-e767ds71

Note: specifying "gsg-keypair" matches later doing ssh -i ~/.ssh/id_rsa-gsg-keypair ...

elb-create-lb MarkTest123 --headers --listener "lb-port=80,instance-port=4242,protocol=HTTP" --availability-zones us-east-1b

elb-configure-healthcheck MarkTest123 --headers --target "HTTP:4242/ping" --interval 30 --timeout 3 --unhealthy-threshold 2 --healthy-threshold 2

elb-register-instances-with-lb MarkTest123 --headers --instances i-X,i-Y

* substitute real EC2 IDs for i-X and i-Y, and this works great!

ssh -i ~/.ssh/id_rsa-gsg-keypair ubuntu@ec2-63-212-55-241.compute-1.amazonaws.com
create a file public/ping - put a line of text in it
start Rails on port 4242

* note: repeat for other EC2 instance. In production, obviously you would want to start your Rails apps using /etc/init.d/...

Then, hitting this URL works: http://marktest123-1645979628.us-east-1.elb.amazonaws.com

Teardown:

elb-delete-lb MarkTest123
and delete EC2 instances
It took me a little while to get ELB working, so I hope that my notes save you some time.

1 comment:

Shlomo said...

Thanks for spelling out these steps clearly.

You'll want to create a DNS CNAME alias pointing, say, www.marktest.com to marktest123-1645979628.us-east-1.elb.amazonaws.com .

For some deep-dive detail into how ELB works and how to test that it's scaling for you, see my article here:

http://clouddevelopertips.blogspot.com/2009/07/elastic-in-elastic-load-balancing-elb.html