How can I automate the replacement of unhealthy Amazon EC2 instances in an Elastic Beanstalk environment?
Last updated: 2020-04-24
I want to automate the replacement of unhealthy Amazon Elastic Compute Cloud (Amazon EC2) instances in my AWS Elastic Beanstalk environment.
By default, the health check configuration of your Auto Scaling group is set as an EC2 type that performs a status check of EC2 instances. To automate the replacement of unhealthy EC2 instances, you must change the health check type of your instance's Auto Scaling group from EC2 to ELB by using a configuration file.
Keep the following in mind:
- Status checks cover only an EC2 instance's health, and not the health of your application, server, or any Docker containers running on the instance.
- If your application crashes, the load balancer removes the unhealthy instances from its target. However, your Auto Scaling group doesn't automatically replace the unhealthy instances marked by the load balancer.
- By changing the health check type of your Auto Scaling group from EC2 to ELB, you enable the Auto Scaling group to automatically replace the unhealthy instances when the health check fails.
Note: For more information on creating and deploying .ebextensions, see How do I customize my Elastic Beanstalk environment using .ebextensions?
The following steps apply to environments with load balancers only.
1. Create a folder named .ebextensions in the root directory of your source bundle.
2. Create a resource-based .ebextension called a .config file. See the following example:
Example .ebextensions/autoscaling.config =============================================== Resources: AWSEBAutoScalingGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: HealthCheckType: ELB HealthCheckGracePeriod: 300 ===============================================
Note: HealthCheckGracePeriod refers to the amount of time, in seconds, that Amazon EC2 Auto Scaling waits before checking the health status of an EC2 instance that's come into service.
3. Create a zip file for your updated application source bundle, and then deploy your application.
Note: You can also deploy your application using eb deploy.
Confirm that the health check type of your Auto Scaling group is set to ELB
1. Open the Amazon EC2 console.
2. In the navigation pane, choose Auto Scaling Groups.
3. For Filter, enter the environment ID of your Auto Scaling group, and then choose your Auto Scaling group from the list of results.
4. On the Details tab of your Auto Scaling group, confirm that Health Check Type is set to ELB.