DOP-C02 Practice Question 15: ASG Termination Lifecycle Hook
AWS Certified DevOps Engineer – Professional (DOP-C02) · Test 2 · question 5 of 10
Instances in an Amazon EC2 Auto Scaling group process jobs from a queue and buffer application logs locally. When a target tracking policy scales in, instances are terminated before they finish their current job and before their logs are uploaded to Amazon S3, so the data is lost. A drain script on the instance handles both tasks and takes up to 10 minutes. The team wants the script to run on every instance that scale-in selects, termination to proceed as soon as the script finishes, and the scaling policy to stay the only thing that decides when to scale in.
What should the team do?
Show answer and explanation
Answer:
A termination lifecycle hook pauses an instance in a wait state after scale-in selects it and before it is terminated. Amazon EC2 Auto Scaling sends an event to Amazon EventBridge, and a rule can start the custom action. The docs give this exact example: invoke a Lambda function, or connect to the instance, "to download logs or other data before the instance is fully terminated". The heartbeat timeout (one hour by default) caps the wait. Setting it above the script's 10 minutes leaves room to finish, and calling complete-lifecycle-action ends the wait early so termination proceeds as soon as the work is done. The scaling policy still picks which instances go and when. Note that termination hooks are best-effort: if the hook times out or is abandoned, the instance is terminated anyway.
Option A controls something else. Deregistration delay lets in-flight *load balancer* requests drain from a target. It does not run a script, it does nothing for queue workers pulling jobs, and it does not make Auto Scaling wait for logs to be uploaded.
Option C relies on the operating system's shutdown sequence, which gives no guaranteed 10-minute window. The instance is powered off on EC2's schedule, not when the script finishes, so a long drain can be cut short. The launch hook only puts the script in place. Nothing gives it time to run at termination.
Option D takes the decision away from the scaling policy. Scale-in protection stops Auto Scaling from terminating protected instances during scale-in, so the group would stop shrinking unless each instance chose to terminate itself. That breaks the requirement that the policy alone decides when to scale in.
Reference: Amazon EC2 Auto Scaling lifecycle hooks