Scenario:
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)?
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.
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.
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.
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.
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:
autoscaling set-instance-health
API to communicate the result to the auto-scaling groupAWS 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With