Control instance retention with instance lifecycle policies
Instance lifecycle policies provide protection against Amazon EC2 Auto Scaling terminations when a termination lifecycle action is abandoned. Unlike lifecycle hooks alone, instance lifecycle policies are designed to ensure that instances move to a retained state when graceful shutdown procedures don't complete successfully.
When to use instance lifecycle policies
Use instance lifecycle policies when graceful shutdown of your application is not optional but mandatory and failed shutdowns require manual intervention. Common use cases include:
-
Stateful applications that must complete data persistence before termination.
-
Applications requiring extended draining periods that may exceed the maximum lifecycle hook timeout of 48 hours.
-
Workloads handling sensitive data where failed or incomplete cleanup could result in data loss or corruption.
-
Mission-critical services where abrupt shutdown causes availability impact.
For more information on how to gracefully handle instance termination, see Design your applications to gracefully handle instance termination.
How instance lifecycle policies work with termination lifecycle hooks
Instance lifecycle policies work in combination with termination lifecycle hooks, not as a replacement. The process follows several stages:
-
Termination lifecycle actions execute. When Amazon EC2 Auto Scaling selects an instance for termination, your termination lifecycle hooks are invoked and the instance enters the
Terminating:Waitstate to begin executing the termination lifecycle actions. -
Graceful shutdown attempt begins. Your application, either running on the instance or via a control plane, receives the terminatioin lifecycle action notification and begins graceful shutdown procedures such as draining connections, completing in-progress work, or transferring data.
-
Termination lifecycle actions complete. A termination lifecycle action can complete with
CONTINUEorABANDONresult. -
The instance lifecycle policy evaluates the situation. Without an instance lifecycle policy configured, the instance proceeds to termination immediately even if the termination lifecycle action was completed with
ABANDONresult. With an instance lifecycle policy configured to retain instances onTerminateHookAbandon, the instance moves to a retained state if the termination lifecycle action was completed withABANDONresult. -
Retained instances await manual action. Instances in retained states continue to incur standard Amazon EC2 charges. These instances don't count toward your Auto Scaling group's desired capacity, so Auto Scaling launches replacement instances to maintain the desired size. Auto Scaling features such as instance refresh and max instance lifetime will also ignore retained instances. This allows you to complete cleanup procedures manually, recover data, or investigate why automated shutdown failed before manually terminating the instance.
-
Manual termination occurs. After you complete the necessary actions on the retained instance, you need to call the
TerminateInstanceInAutoScalingGroupAPI to terminate the instance.