How can I automate the replacement of unhealthy Amazon EC2 instances in an Elastic Beanstalk environment?
Last updated: 2021-04-22
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. You must use a configuration file to automate the replacement of unhealthy EC2 instances. The configuration file changes the health check type of your instance's Auto Scaling group from EC2 to ELB.
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 from EC2 to ELB, you enable the Auto Scaling group to automatically replace the unhealthy instances. The unhealthy instances are replaced 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.
Use a configuration file to change the health check type of your instance's Auto Scaling group to ELB
1. Create a folder named .ebextensions in the root directory of your source bundle.
2. Create a resource-based .ebextension called a .config file. For example:
Example .ebextensions/autoscaling.config =============================================== Resources: AWSEBAutoScalingGroup: Type: AWS::AutoScaling::AutoScalingGroup Properties: HealthCheckType: ELB HealthCheckGracePeriod: 300 ===============================================
Note: HealthCheckGracePeriod refers to the time 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.