How to make an EC2 instance automatically reboot when an ELB says it is unavailable?

From time to time, the Elastic Load Balancer (ELB) issues one of my servers as unavailable. What's good is hardly a break.

Normally, just reloading the instance through the AWS console fixes the problem.

I would like my EC2 instance to automatically reboot when it becomes unavailable.

What is the best way to do this? (this is a Windows case, but ideally it doesn't matter)

+6
source share
5 answers

I got a cheap copy of EC2 micro and wrote a simple script that lists all of my servers on EC2 and does a GET on each to make sure it works correctly. If the server does not work correctly, my script reloads it. I have a script running once every 10 minutes.

It would be a little easier if I had a service to do this for me, but the system was easily configured and the micro-instance is cheap.

+5
source

Use the autoscale group and set it to 1 live instance. If your instance fails, it will infer a new one from the predefined instance image. It is easy and convenient.

+6
source

Generally speaking, you should have load balancing configured using the auto-scaling group. The auto-scaling group will have information on how your server is configured. For example, it will have ami and a possible user data file that will allow you to configure it without any user interaction. When these two services work for you, the load balancer will terminate your failed instance and immediately start a new instance.

If you are using a Windows instance, you can write a powershell script to configure the server and use CloudInit.NET to run the script when the server starts. The last part of the script should enable load balancing verification.

+3
source

Agree with @agmin that a lambda function is a great way to do this. You can set the CloudWatch alarm to start the SNS theme lambda function.

I have combined this implementation and it seems to work well. It can be configured and configured completely through the command line:

https://github.com/jcowley/elb-instance-reboot

+2
source

Write a lambda function!

It is cheaper than launching a micro-instance, and you can configure it for any period of time. You also get CloudWatch monitoring, so you don’t have to worry about checking the health of your micro-participant separately.

I am using ElasticBeanstalk, which offers the describeInstancesHealth endpoint. Alternatively, you can use the ec2 endpoint of describeInstanceStatus .

Then, if you find a bad instance, ec2 rebootInstances allows rebootInstances to restart one or more instances.

+1
source

Source: https://habr.com/ru/post/910037/


All Articles