Certopolis

AWS Certified DevOps Engineer – Professional

EC2 Auto Scaling lifecycle hooks: CONTINUE vs ABANDON (DOP-C02)

A lifecycle hook holds an instance in a wait state while it launches or terminates, so a custom action can run. The EC2 Auto Scaling User Guide gives it one hour by default. On launch, ABANDON terminates and replaces the instance. On termination, both CONTINUE and ABANDON let the instance terminate.

When does a lifecycle hook run?

Launch and termination hooks apply to all launches and terminations in the group, whether automatic or started by calls such as SetDesiredCapacity or TerminateInstanceInAutoScalingGroup. They also apply to replacements made by instance refresh, maximum instance lifetime and Capacity Rebalancing, and to instances entering and leaving a warm pool.

Hooks do not run when you attach or detach instances, move instances in or out of standby, or delete the group with the force delete option.

The default heartbeat timeout is one hour. An instance can stay in a wait state for at most 48 hours or 100 times the heartbeat timeout, whichever is smaller. A lifecycle hook does not stop a Spot Instance interruption.

What do CONTINUE and ABANDON do?

For a launching instance, CONTINUE means the custom action succeeded and the instance can go into service. ABANDON means it failed, and Auto Scaling can terminate and replace the instance.

For a terminating instance, both results allow the termination. ABANDON stops any remaining actions, such as other lifecycle hooks. CONTINUE lets the other hooks complete. Termination hooks are best effort by default: if one times out or is abandoned, the instance is terminated immediately.

An instance lifecycle policy changes that. With a policy set to retain instances on TerminateHookAbandon, an instance whose termination action ends in ABANDON moves to a retained state. Retained instances keep incurring EC2 charges and do not count toward desired capacity, so Auto Scaling launches replacements.

How does the custom action get notified?

Auto Scaling sends lifecycle event notifications to Amazon EventBridge without a notification target on the hook. AWS recommends EventBridge or a user data script over Amazon SNS or Amazon SQS as the target. A rule with the source aws.autoscaling and the detail-type EC2 Instance-launch Lifecycle Action matches launch hooks for a group, and can invoke a Lambda function or another target.

A user data script needs no notification target. To finish the hook from the instance, the script runs complete-lifecycle-action, and the instance's IAM role must allow that call. SNS and SQS targets need the topic or queue ARN and an IAM role that gives Auto Scaling access to it. The console does not show the field for an SNS or SQS target.

With a launch hook, the health check grace period starts when the instance reaches InService. A common use is to finish bootstrap scripts before the instance is registered with the load balancer at the end of the hook.

Where the exam catches people

  • Believing ABANDON on a termination hook keeps the instance: both results let it terminate. Retention needs an instance lifecycle policy with TerminateHookAbandon.
  • Believing termination hooks run when a group is force deleted: they do not. They also do not run on detach or standby.
  • Believing a hook needs an SNS topic or SQS queue to notify anything: Auto Scaling already sends the events to EventBridge.
  • Believing a hook can hold an instance indefinitely: the limit is 48 hours or 100 times the heartbeat timeout, whichever is smaller.
  • Believing a lifecycle hook delays a Spot interruption: it does not.

Written from these sources

Try 3 questions on this

Pick an answer to see why each option is right or wrong.

  1. A company runs Amazon EC2 Auto Scaling groups for the same application in us-east-1 and eu-west-1. Each new instance must be registered in the company's asset inventory before it receives traffic. An AWS Lambda function that registers an instance and then calls CompleteLifecycleAction is already deployed in both Regions. The DevOps team must route each launch to the function in the group's own Region, using the notification path that AWS recommends for this purpose. Which combination of steps will meet these requirements? (Select TWO.)

    Right. In the game that’s Renown for your Monitoring and Logging building.

    Not that one: B,C is right. In the game your Monitoring and Logging building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    A hook created without a notification target still emits an EC2 Instance-launch Lifecycle Action event to EventBridge each time an instance enters Pending:Wait. A rule in each group's own Region, matching that detail-type for the group, invokes that Region's function, which registers the instance and completes the action before the instance is put in service.

    Written from docs.aws.amazon.com/autoscaling/ec2/userguide/prepare-for-lifecycle-notifications.html

  2. A company runs a payment reconciliation service on an Amazon EC2 Auto Scaling group. A termination lifecycle hook on the group sends an event through Amazon EventBridge to an AWS Systems Manager Automation runbook. The runbook copies the instance's local journal file to Amazon S3, then completes the lifecycle action. Auditors require that when the copy fails, the instance is not destroyed, so that an engineer can recover the journal by hand later. Today an instance whose copy fails is terminated within minutes. What should a DevOps engineer do to meet these requirements?

    Right. In the game that’s Renown for your Monitoring and Logging building.

    Not that one: A is right. In the game your Monitoring and Logging building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    So the result alone can never keep the instance. What keeps it is an instance lifecycle policy that retains instances whose termination action ended in ABANDON. With that policy on the group, the runbook's ABANDON on a failed copy moves the instance to a retained state instead of terminating it, the group launches a replacement, and the engineer can recover the journal before terminating the retained instance.

    Written from docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html

  3. A company releases a log-heavy service with blue/green deployments of whole Amazon EC2 Auto Scaling groups, and its instances buffer log events on local disk. Each group has a termination lifecycle hook that sends its event through Amazon EventBridge to an AWS Systems Manager Run Command document. The document flushes the buffer to Amazon S3 and then completes the lifecycle action with CONTINUE, and this works during ordinary scale-in. After traffic moves to a new group, a final pipeline action must remove the old group. Every old instance must flush its buffer before it is terminated, and the removal must not need any manual step. How should the DevOps engineer build the final pipeline action to meet these requirements?

    Right. In the game that’s Renown for your Monitoring and Logging building.

    Not that one: D is right. In the game your Monitoring and Logging building would lose a little Renown, and this question would come back later.

    Show the full explanation and doc quotes

    And deleting a group without force delete succeeds only once the group has no instances. So the action scales the old group to zero, which terminates each instance through the hook so that the Run Command flush completes, waits for the group to empty, and then deletes it without ForceDelete. Everything stays inside the pipeline.

    Written from docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html

Ask for something