Business Challenge
The last post ended on a recommendation it did not explain: AWS says to use lifecycle hooks rather than a long health check grace period. That turns out to be a statement about ordering, and once you see the ordering the advice is obviously right.
The same mechanism on the termination side invites an assumption that is wrong, and wrong in the direction that loses data.
“The lifecycle hook puts the instance into a wait state (Pending:Wait) and
then performs a custom action.” Then, after it completes:
“The instance enters the InService state and the health check grace period
starts.”
So the bootstrap wait happens outside the window the grace period covers. The grace
period exists to tolerate an instance that is InService but not ready; a launch hook
stops it being InService until it is ready. That is why the documented answer to a
zero-second grace period is a hook. A launch hook can reduce or eliminate the need for a grace
period by completing initialization before the instance enters InService —
though both can be used together where additional stabilization time is genuinely needed after
it enters service.
“By default, termination lifecycle hooks operate on a best-effort basis. If a termination lifecycle hook times out, or is abandoned, Amazon EC2 Auto Scaling proceeds with terminating the instance immediately.”
A hook written to flush a buffer, upload logs or drain a queue does not guarantee any of that happened. It guarantees you were asked. If your code fails, times out, or reports failure, the instance goes anyway.
On launch, the two results mean what you would expect: continue puts the instance into service, abandon means “we can terminate and replace the instance”. On termination they collapse:
“If an instance is terminating, both abandon and continue allow the instance to terminate. However, abandon stops any remaining actions, such as other lifecycle hooks, and continue allows any other lifecycle hooks to complete.”
Abandon is not a veto. It is a request to skip the remaining hooks on the way out.
Architecture
Where the wait states sit
| Phase | Sequence |
|---|---|
| Scale out | Pending → Pending:Wait → Pending:Proceed → registered with the load balancer → InService, then the grace period starts |
| Scale in | deregistered from the load balancer → Terminating:Wait → Terminating:Proceed → terminated |
| Root volume replacement | ReplacingRootVolume:Wait → RootVolumeReplaced → Pending → Pending:Wait → InService |
One ordering detail is easy to miss and matters for the load-balancer use case: “However, before the instance reaches the InService state, if the Auto Scaling group is associated with an Elastic Load Balancing load balancer, the instance is registered with the load balancer, and the load balancer starts checking its health.”
Registration happens at the end of the hook, not at the start of the launch. That is what makes a
launch hook effective at keeping traffic away from a half-built instance — the instance is not
a target until the hook says so. It also means the load balancer's own health checks begin before
InService, which is the reason the grace period exists at all for groups that do not use
hooks.
Two clocks, and the one you cannot extend past
“Instances can remain in a wait state for a finite period of time. The default timeout for a lifecycle hook is one hour (heartbeat timeout). There is also a global timeout that specifies the maximum amount of time that you can keep an instance in a wait state. The global timeout is 48 hours or 100 times the heartbeat timeout, whichever is smaller.”
With the default one-hour heartbeat, 100 heartbeats would be 100 hours, so the ceiling is the 48-hour figure — two days, and no amount of heartbeating gets past it. The heartbeat is the renewable clock: “If you need more time, you can restart the timeout period by recording a heartbeat.” The global timeout is not renewable, and the instance lifecycle policy documentation names that ceiling as a reason to reach for retention instead — “Applications requiring extended draining periods that may exceed the maximum lifecycle hook timeout of 48 hours”.
What expiry does is not fixed, and this is the part to configure deliberately. When a lifecycle hook
times out, Auto Scaling applies the hook's configured DefaultResult, described as
“The action the Auto Scaling group takes when the lifecycle hook timeout elapses or if an
unexpected failure occurs”, with valid values CONTINUE and
ABANDON. The API reference is explicit about which you get if you say nothing:
“The default value is ABANDON.”
For a launch hook, ABANDON causes the instance to be terminated and replaced, whereas
CONTINUE allows the launch process to proceed. So launching an instance after an
unsuccessful bootstrap is not the universal default — it is what you get only with
CONTINUE set. The user guide's walkthrough describes that path, which is worth knowing
when the narrative and the API reference are read side by side.
On the termination side the distinction collapses, because both results permit termination. The
configured DefaultResult therefore changes a great deal on launch and almost nothing on
the way out — except which later hooks still run.
What retention actually is
To prevent an Auto Scaling termination from proceeding when a termination lifecycle action is
abandoned, configure an instance lifecycle policy to retain instances on
TerminateHookAbandon. This provides an opportunity to retry or complete cleanup but does
not guarantee graceful shutdown or prevent data loss from other failure mechanisms. The scope is
stated narrowly in the documentation:
“Instance lifecycle policies provide protection against Amazon EC2 Auto Scaling
terminations when a termination lifecycle action is abandoned.”
The behaviour is stated as a direct comparison:
“Without an instance lifecycle policy configured, the instance proceeds to
termination immediately even if the termination lifecycle action was completed with ABANDON
result. With an instance lifecycle policy configured to retain instances on
TerminateHookAbandon, the instance moves to a retained state if the termination
lifecycle action was completed with ABANDON result.”
So ABANDON changes meaning depending on whether a policy exists. Without one it is a
hint that stops later hooks. With one it is the signal that preserves the instance. The same API call
with the same argument, two different outcomes, decided by group configuration.
Retention is not free, and the documentation is unusually direct about the bill: “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.”
Read that carefully: a retained instance is paid for, outside the group's capacity accounting, and invisible to the two mechanisms that would otherwise eventually clean it up. A policy that retains on every abandoned shutdown, in a group with a flaky shutdown script, accumulates paid instances that nothing will ever remove. Retention is a tool for workloads where a failed shutdown genuinely requires a human, and the human is the part you have to supply.
Getting rid of one is deliberate too: “After you complete the necessary actions on the retained instance, you call the TerminateInstanceInAutoScalingGroup API. This starts a new termination attempt that invokes your termination lifecycle hooks again, so the instance terminates only after you complete the new lifecycle action with the CONTINUE result.” The hooks run again, so the cleanup path has to be idempotent.
Where hooks do not fire at all
The gaps matter more than the coverage, because each one is a path by which an instance leaves without your action running.
- Attach, detach, standby and force delete. The footnote on the availability table is explicit: “Does not apply when you attach or detach instances, move instances in and out of standby mode, or delete the group with the force delete option.” Three of those are things an operator does during an incident, and the fourth is how groups get torn down in a hurry.
- Spot interruption. A hook “does not prevent an instance from terminating in the event that capacity is no longer available, which can happen at any time with a two-minute interruption notice”. If your shutdown needs more than two minutes, Spot is not the place for it regardless of hooks.
- Warm pools. “The stop or hibernate action does not wait for user data to finish running”, so an instance entering a warm pool can be stopped mid-bootstrap unless a launching hook holds it.
Two operational side effects
A broken hook does not fail loudly; it slows the group down. “Amazon EC2 Auto Scaling limits the rate at which it allows instances to launch if the lifecycle hooks are failing consistently, so make sure to test and fix any permanent errors in your lifecycle actions.” So the symptom of a permanently broken launch hook is a group that scales out sluggishly — which looks like a capacity problem, not a code problem.
And hooks interact with the older scaling policies: “In most cases when lifecycle hooks are invoked, scaling activities due to simple scaling policies are paused until the lifecycle actions have completed and the cooldown period has expired.” An hour-long hook plus a cooldown is an hour in which a simple scaling policy does nothing.
Why This Architecture Holds Up
Best-effort on termination is the right default, even though it surprises people
A termination hook that could block indefinitely would turn every scale-in into a potential hang, and a group that cannot shed capacity is a worse failure than a lost buffer flush. Defaulting to proceed anyway keeps the control loop live. The criticism is not the default; it is that the word abandon suggests a veto it does not have.
Separating retention into its own policy is better than a flag on the hook
Retention has consequences the hook author cannot see: a paid instance outside capacity accounting, invisible to instance refresh, awaiting a human. Making it a group-level policy rather than a hook option puts the decision where the cost lands, and forces it to be made once deliberately rather than implied by a return value.
The hook-before-InService ordering is the quiet good design
Putting the wait before InService, and registration at the end of the hook, is what makes
the readiness problem solvable at all. It means readiness is expressed as a state transition rather
than as a timer. The two mechanisms act at different stages, so they compose: the hook covers
initialization before InService, and a grace period, if you keep one, covers
stabilization after it.
Key Architecture Decisions
| Decision | Take | Why |
|---|---|---|
| Readiness on launch | Launch hook for initialization; keep a grace period only for post-service stabilization | The hook waits before InService, so it can reduce or remove the need for a grace period without replacing it |
| Cleanup on termination | Hook for the attempt; add a retention policy where an abandoned action must not proceed | The hook alone is best-effort and both results permit termination; retention covers that scenario, not durability generally |
| Retention policy scope | Only where a failed shutdown genuinely needs a human | Retained instances are billed, outside capacity, and ignored by refresh and max lifetime |
| Heartbeat timeout | Set it to the real action duration, not the default hour | The range is 30 to 7200 seconds; a too-long timeout delays the configured DefaultResult rather than preventing it |
| Shutdown needing more than two minutes | Not on Spot | A hook does not prevent an interruption termination |
| Cleanup script design | Idempotent | Terminating a retained instance invokes the termination hooks again |
The audit worth running
- Every termination hook whose purpose is data safety, checked against whether the group has an instance lifecycle policy. Without one, the hook is advisory.
- Every launch hook paired with a long grace period, checked for whether the grace period is covering genuine post-service stabilization or is just absorbing an unmeasured hook duration. Both together is a legitimate configuration; both together by accident is not.
- Any group scaling out slowly, checked for consistently failing hooks before capacity is blamed.
- Runbooks that detach instances or force-delete groups, which bypass hooks entirely.
Closing Thought
This series has spent several posts on the same shape: a mechanism that looks like a guarantee and is documented as an attempt. Automatic recovery attempts a migration. An aggregate alarm attempts to mean one thing. A termination hook attempts a graceful shutdown. In each case AWS is precise in the documentation and the precision is easy to read past, because the feature name sounds like a promise.
The useful habit is to ask what happens when the mechanism fails, and whether the failure is
permissive or blocking. On termination it is permissive whatever you configure: a hook that never
completes terminates the instance anyway. On launch it depends on the hook's
DefaultResult, and the API default of ABANDON means a timed-out launch hook
terminates and replaces the instance rather than admitting it. Either way the group keeps moving,
which is almost certainly right, and either way the hook is where you express intent rather than
where you enforce it.
Where an abandoned shutdown must not simply proceed, the retention policy is the mechanism that stops it — for that scenario, not as a general durability guarantee. It buys an opportunity to retry or finish cleanup by hand; it does not protect against the other ways an instance can go away, and a Spot interruption or a force delete still bypasses all of this. The price is a billed instance waiting for a person, which is the honest cost of refusing to let one specific failure pass silently.
Reliability — Amazon EC2 Auto Scaling warm pools: what the Stopped, Running and Hibernated pool states cost, why the stop action does not wait for user data, how reuse-on-scale-in changes the lifecycle, and when a warm pool is cheaper than a faster bootstrap.
Official AWS Reference
- Amazon EC2 Auto Scaling lifecycle hooks — that a hook provides one hour by default before the instance transitions, that termination hooks are best-effort and a timeout or abandon proceeds with terminating immediately, the heartbeat and global timeouts including the 48 hours or 100 heartbeats rule, the abandon and continue semantics on each side of the lifecycle, that a hook does not prevent a Spot interruption termination on a two-minute notice, that consistently failing hooks cause Auto Scaling to limit the launch rate, that the health check grace period starts when the instance reaches
InService, the shared notification target and IAM role constraint for hooks created with the group, that hooks do not apply to attach, detach, standby moves or force delete, the warm pool stop or hibernate not waiting for user data, and the pausing of simple scaling policy activities - How lifecycle hooks work in Auto Scaling groups — the
Pending:WaitandPending:Proceedtransitions and the one-hour default with heartbeat restart, that the instance entersInServiceand the health check grace period then starts, that load balancer registration and its health checks begin beforeInService, and theTerminating:WaittoTerminating:Proceedtransition on scale in - Control instance retention with instance lifecycle policies — that the policy protects against terminations when a termination lifecycle action is abandoned, the explicit comparison of behaviour with and without a policy configured to retain on
TerminateHookAbandon, that retained instances keep incurring EC2 charges and are excluded from desired capacity, instance refresh and max instance lifetime, the 48-hour maximum hook timeout as a reason to use retention, and that terminating a retained instance re-invokes the termination hooks and requires aCONTINUEresult - Set the health check grace period for an Auto Scaling group — further reading on the setting a launch hook replaces, covered in #121; no claims in this post are drawn from it
- Complete a lifecycle action in an Auto Scaling group — further reading on
complete-lifecycle-actionand recording a heartbeat; no claims in this post are drawn from it - Amazon EC2 Auto Scaling instance lifecycle — further reading on the full set of lifecycle states; no claims in this post are drawn from it
Comments