Skip to content

Code for rebooting instances sitting behind a target group via a Lambda function, based on their Cloudwatch metrics

Notifications You must be signed in to change notification settings

heywoodg/rebootEC2instances

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Automatically Reboot EC2 Instances Gracefully

This is a AWS lambda function that will automatically but gracefully reboot EC2 instances based on the the memory consumption. This is useful when you have a memory leak on instances that are in a TG (target group), and you need a graceful way to deal with them.

The code is Python, and will use Boto3 to get the healthy instances that are in the specified target groups, and then add them to a list. For each instance, it will check the there are at least 5 or more before continuing. This is a failsafe to prevent taking instances out of the TG when it may adversely affect the ASGs (autoscaling group) ability to handle the load. Once done, it will get the "Memory % Committed Bytes In Use" CloudWatch metric from each one (assuming that it has been enabled on the instance).

Once it has built a list of instances and memory usage, it will select the instance with the highest memory usage, and if it is higher than 65%, it will prepare to reboot that instance.

To do the reboot, it will:

  1. Remove the instance from the target groups
  2. Wait for an amount of time equal to the target group deregistration delay (wait for draining to complete basically)
  3. Reboot the instance
  4. Wait 120 seconds
  5. Add the instance back into the target groups.

The Lamdba function can then be run on a scheduled (every 15 minutes for example) to monitor and remediate hosts with high memory consumption.

About

Code for rebooting instances sitting behind a target group via a Lambda function, based on their Cloudwatch metrics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages