Using an Elastic Load Balancer with a single EC2 instance on AWS

Solution 1:

Amazon's Elastic Load Balancer (ELB) distributes requests to healthy instances that have been assigned to it. It does not restart or modify those instances (or their number). It determines 'healthy' instances via a health check - typically polling a given location.

What you are asking for is to 'maintain 1 healthy instance' - that is an auto scaling task. Auto scaling will allow you to define a group of instances (typically including an AMI to launch, the instance type, one or more availability zones to launch the instances in, and the number of instances to maintain (minimum/maximum)), as well as policies by which to scale up and down with. An autoscaling policy returns an ARN (Amazon Resource Name - a reference to a resource).

Once you have your auto scaling group setup, all you need to do is trigger your scaling policies when an instance becomes unhealthy. If you look closely at the health check that you setup with ELB, you will notice that you can setup an alarm - and that alarm is actually a Cloudwatch alarm.

You can setup your own Cloudwatch alarms, or set them up through ELB's health check - just specify the --alarm-actions to trigger the auto-scaling ARN when your unhealthy node criteria is met.

ELB isn't technically required in this setup - auto scaling will do the job on its own. What ELB does do for you is provide a DNS address you can access your instance(s) by (and also some sort of error message when a backend is unavailable). (With auto scaling on its own, you would need to re-associate your elastic IP with the new instance when it launches (which can be scripted)).

Finally, just to clarify: CloudFlare is not an AWS service - it is a CDN (and is somewhat well known for mitigating DDoS attacks). Amazon's equivalent service is CloudFront - you don't need either of them for restarting instances. What you do need is CloudWatch - Amazon's monitoring service). The free tier does cover both Cloudwatch and a few alarms.