Manage retained instances
Monitor and control Amazon EC2 instances that have been moved to a retained state. Use CloudWatch metrics to track retained instances, then manually terminate retained instances after completing your custom actions.
Retained instances do not count toward your Amazon EC2 Auto Scaling group's desired capacity. When an instance enters
a retained state, Auto Scaling launches a replacement instance to maintain the desired capacity.
For example, suppose your Auto Scaling group has a desired capacity of 10. When an instance enters the
Terminating:Retained state, Auto Scaling launches a replacement instance to maintain the desired
capacity of 10. You now have 11 running instances in total: 10 in your active group plus 1 retained
instance. Standard Amazon EC2 charges for all 11 instances will apply until you manually terminate the
retained instance.
Instance lifecycle states of retained instances
Understand how instances transition through lifecycle states when instance lifecycle policies are used. Instances follow a specific path from normal termination through retention to final termination.
When retention is triggered, instances transition through these states:
Terminating- Normal termination beginsTerminating:Wait- Lifecycle hook executesTerminating:Proceed- Lifecycle actions wrap up (whether they succeeded or failed)Terminating:Retained- Hook fails, instance retained for manual intervention
Warm pool instances take different lifecycle state paths depending on the scenario:
Instances scaling back into the warm pool:
Warmed:Pending- Normal warm pool transition beginsWarmed:Pending:Wait- Lifecycle hook executesWarmed:Pending:Proceed- Lifecycle actions wrap up (whether they succeeded or failed)Warmed:Pending:Retained- Hook fails, instance retained for manual intervention
Instances being terminated from the warm pool:
Warmed:Terminating- Normal termination beginsWarmed:Terminating:Wait- Lifecycle hook executesWarmed:Terminating:Proceed- Lifecycle actions wrap up (whether they succeeded or failed)Warmed:Terminating:Retained- Hook fails, instance retained for manual intervention
Monitor retained instances
Because retained Amazon EC2 instances incur costs and require manual intervention, monitoring them is essential. Amazon EC2 Auto Scaling provides several CloudWatch metrics to track retained instances.
Enable group metrics to track retained instances:
aws autoscaling enable-metrics-collection \ --auto-scaling-group-namemy-asg\ --metricsGroupTerminatingRetainedInstances
The available metrics are:
-
GroupTerminatingRetainedInstancesshows the number of instances in theTerminating:Retainedstate. -
GroupTerminatingRetainedCapacityshows the capacity units represented by instances in theTerminating:Retainedstate. -
WarmPoolTerminatingRetainedCapacitytracks retained instances terminating from the warm pool. -
WarmPoolPendingRetainedCapacitytracks retained instances returning to the warm pool.
You can also check your Amazon EC2 Auto Scaling group's scaling activities to understand why instances
were retained. Look for termination activities with StatusCode: Cancelled and
status reason messages indicating lifecycle hook failures:
aws autoscaling describe-scaling-activities \ --auto-scaling-group-namemy-asg
We recommend creating CloudWatch alarms on these metrics to alert you when instances enter a retained state. This helps you track cost implications and ensures you don't forget to clean up instances that require manual intervention.
Terminate retained instances
After completing your custom actions, terminate your retained instances by calling the TerminateInstanceInAutoScalingGroup API:
aws autoscaling terminate-instance-in-auto-scaling-group \ --instance-idi-1234567890abcdef0\ --no-should-decrement-desired-capacity