Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement custom health checks for EC2 instances without using an ELB?

Scenario:

  • I'm running an EC2 instance behind an auto-scaling group, but I'm not using an ELB.
  • Inside the EC2 instance, a docker container with a web server is running.

I would like to add a simple health check that the web server does still respond, so if the docker container goes down, the auto scaling group can replace the instance.

From what I see, only the ELB supports custom health checks. As I don't need an ELB, I wonder if it makes sense to run the health check inside the EC2 instance with a cron job. If the web server does not respond (locally), it can set the health status like this:

export INSTANCE=$(curl http://169.254.169.254/latest/meta-data/instance-id)
export AWS_DEFAULT_REGION=$(curl http://169.254.169.254/latest/dynamic/instance-identity/document|grep region|awk -F\" '{print $4}')
aws autoscaling set-instance-health --instance-id $INSTANCE --health-status Unhealthy

I think, it should work, but it looks a bit complicated, though. Is there a better way to implement custom health checks (without using an ELB)?

like image 219
Philipp Claßen Avatar asked May 15 '17 09:05

Philipp Claßen


People also ask

Is it possible to integrate a custom health check system in AWS?

This extends your health checks by using a combination of custom health checks, Amazon EC2 status checks, and Elastic Load Balancing health checks, if enabled. You can send the instance health information directly to Amazon EC2 Auto Scaling using the AWS CLI or an SDK.

Which AWS service can perform health checks on Amazon EC2 instances?

On the Amazon EC2 Auto Scaling console, you can view the status (healthy or unhealthy) of your warm pool instances. You can also view their health status using the AWS CLI or one of the SDKs. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ , and choose Auto Scaling Groups from the navigation pane.

Can ASG work without ELB?

Yes, you can use Auto Scaling without Elastic Load Balancing. However, it means that you don't have a common entry point, so it isn't ideal if you will be, say, hosting a website.

Do you need a load balancer for EC2?

If your application is built within the Amazon Elastic Compute Cloud (Amazon EC2) Classic network, you should use Classic Load Balancer. If you need to deploy and run third-party virtual appliances, you can use Gateway Load Balancer.


1 Answers

In 2017, there is no direct support from AWS, only the API to set the health of an EC2 instance. Thus, the technique described in the question is the recommended way:

  • Implement a custom health check (could be a shell script or whatever you choose) and run it periodically (via cron or whatever you choose)
  • Use the autoscaling set-instance-health API to communicate the result to the auto-scaling group

AWS documentation on custom health checks:

If you have custom health checks, you can send the information from your health checks to Auto Scaling so that Auto Scaling can use this information. For example, if you determine that an instance is not functioning as expected, you can set the health status of the instance to Unhealthy. The next time that Auto Scaling performs a health check on the instance, it will determine that the instance is unhealthy and then launch a replacement instance.

Use the following set-instance-health command to set the health state of the specified instance to Unhealthy:

aws autoscaling set-instance-health --instance-id i-123abc45d –-health-status Unhealthy

like image 156
Philipp Claßen Avatar answered Oct 08 '22 19:10

Philipp Claßen