Lifecycle hooks pause scaling activities triggered by Auto Scaling, placing Elastic Compute Service (ECS) or Elastic Container Instance (ECI) instances in a wait state. This pause allows you to perform custom actions. You can use this time to meet specific deployment needs, such as pre-installing software. The scaling activity only resumes after the lifecycle hook's timeout period ends. This topic explains how to create, modify, delete, enable, and disable lifecycle hooks.
Prerequisites
You have created a scaling group. For more information, see Configure a scaling group or CreateScalingGroup.
(Optional) To receive notifications when a lifecycle hook is triggered, first create a Message Service (MNS) topic or queue. For more information, see the following topic.
Create a lifecycle hook
A lifecycle hook is a tool that helps you manage the lifecycle of ECS or ECI instances within a scaling group. For more information, see Lifecycle hooks. You can create up to 10 lifecycle hooks for each scaling group. This section shows how to create a lifecycle hook.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
Find the desired scaling group and use one of the following methods to open the scaling group details page.
Click the ID of the scaling group in the Scaling Group Name/ID column.
Click Details in the Actions column.
In the upper part of the details page, click the Lifecycle Hook tab.
In the upper-left corner of the page, click Create Lifecycle Hook.
On the Create Lifecycle Hook page, configure the lifecycle hook parameters.
The following table describes the lifecycle hook parameters.
Parameter
Description
Name
The name of the lifecycle hook cannot be changed after creation. The name must be 2 to 64 characters long, start with a letter or a number, and can contain periods (.), underscores (_), and hyphens (-).
Scaling Activity
Triggers the lifecycle hook when the specified scaling activity occurs, placing the instance in a wait state. The following options are supported:
Scale-in Event
Scale-out Event
Timeout Period
The timeout period, in seconds. The value must be an integer from 30 to 21600 (6 hours). Default: 1800.
NoteSet a reasonable timeout period based on the time required to complete the actions for the lifecycle hook. If the timeout expires before the actions finish, Auto Scaling applies the Default action.
Default Execution Policy
Single lifecycle hook scenario
The action to perform after the wait state ends. The following options are supported:
Continue: During a scale-out event, the new ECS or ECI instance is added to the scaling group. During a scale-in event, the ECS or ECI instance is removed from the scaling group and released.
Reject: During a scale-out event, the new ECS or ECI instance is released and not added to the scaling group. During a scale-in event, the ECS or ECI instance is removed from the scaling group and released.
Roll Back: During a scale-out event, the new ECS or ECI instance is released and not added to the scaling group. During a scale-in event, the instance that was being removed is added back to the scaling group.
Multiple lifecycle hooks scenario
If multiple lifecycle hooks are triggered simultaneously, the final outcome of the scaling activity depends on the Scaling Activity type.
Scale-in event: If a lifecycle hook with the Continue action completes, Auto Scaling waits for the other hooks to complete. If any hook completes with the Reject or Roll Back action, Auto Scaling immediately ends the wait state for all other hooks. The final action is determined by the policies of the completed hooks. A rollback only occurs if at least one completed hook has a Rollback action. If the completed hooks only have Reject action, the scale-in continues.
Scale-out event: The scale-out only continues if all lifecycle hooks finish with the Continue action. If any hook completes with the Reject or Roll Back action, the system immediately ends the wait state for the remaining hooks, stops the scale-out, and performs a rollback. For scale-out event, the Rollback and Reject actions have the same effect.
(Optional) OOS template notification scenario
This scenario only applies if the lifecycle hook is configured to use a CloudOps Orchestration Service (OOS) template for notifications. If the OOS template execution finishes before the timeout period ends, the next action depends on the OOS template's execution result:
Success: For a scale-out event, the ECS or ECI instance is added to the scaling group. For a scale-in event, the instance stops serving traffic in the scaling group.
Failure: For a scale-out event, the ECS or ECI instance is directly released. For a scale-in event, the instance stops serving traffic in the scaling group.
The outcome of an instance "stopping to serve traffic" depends on the following conditions:
If the instance was created by Auto Scaling or was manually added to the scaling group, and the scaling group's Instance Reclaim Mode is set to Release, the instance is removed from the scaling group and released.
If the instance was created by Auto Scaling or was manually added to the scaling group, and the scaling group's Instance Reclaim Mode is set to Economical Mode, the instance is stopped but remains in the scaling group. For more information, see Configure a scaling group.
If an instance was manually added to the scaling group, it is only removed from the group and continues to run independently.
Send Notification When Lifecycle Hook Takes Effect
Configure notifications or other automated actions to run when the lifecycle hook is triggered. The following options are supported:
No Notification: This is the default option.
MNS Topic: Select an MNS topic. You can also specify a notification identifier. Auto Scaling includes this identifier in each message, helping you categorize notifications.
MNS Queue: Select an MNS queue. You can also specify a notification identifier. Auto Scaling includes this identifier in each message, helping you categorize notifications.
OOS Template: Select a template type and configure its parameters.
Template types include Public Templates and My Templates. Auto Scaling uses the selected template to perform custom operations on the ECS instance.
NoteTo use My Templates, you must first create a template. For more information, see Create a template.
For more information about public templates, see Public templates.
Set the parameters as needed. To set the OOS RAM role, note the following:
The RAM role must trust the OOS service. This means the trusted entity for the RAM role is an Alibaba Cloud service, and the trusted service is CloudOps Orchestration Service. For more information, see Procedure.
The RAM role must have permissions to operate on resources such as ECS, ECI, and Auto Scaling. To get the required policy, click View the permission policy that is required to execute the template. For more information about how to grant permissions to a RAM role, see Create a RAM role for OOS and grant permissions.
NoteFor more examples of how to configure template parameters, see Overview of best practices for lifecycle hooks and OOS templates.
Event Bus: To identify and handle corresponding events, select an event bus. Auto Scaling then generates different types of lifecycle notifications when a scaling activity occurs in the scaling group.
Click OK.
After you create the lifecycle hook, you can view it on the Lifecycle Hooks tab of the scaling group details page.
When Auto Scaling triggers a matching scaling activity, the instance's state changes to pending. The following figure shows an ECS instance in the pending state.

Modify or delete a lifecycle hook
You can modify or delete lifecycle hooks as your business needs change.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
On the Scaling Groups page, find the scaling group that you want to manage, and click Details in the Actions column.
At the top of the scaling group details page, click the Lifecycle Hooks tab.
Modify or delete the lifecycle hook.
Modify a lifecycle hook
If the properties of a lifecycle hook no longer meet your business needs, you can modify one or more properties without creating a new one.
Find the lifecycle hook that you want to manage, and click Modify in the Actions column.
Modify the configuration of the lifecycle hook, and then click OK.
You cannot modify the name of the lifecycle hook. For a description of other parameters, see Step 7.
Delete a lifecycle hook
Delete lifecycle hooks that you no longer need to free up your quota. You can also delete a lifecycle hook while a scaling activity is paused.
ImportantDeleting a lifecycle hook that has an instance in a wait state immediately ends the wait state for that instance.
Find the lifecycle hook or hooks that you want to delete.
To delete a single hook: Click Delete in the Actions column for the lifecycle hook.
To delete multiple hooks: Select the lifecycle hooks that you want to delete, and at the bottom of the lifecycle hook list, click Delete.
In the dialog box that appears, click OK.
Disable or enable a lifecycle hook
By default, a lifecycle hook is in the Effective state after you create it. You can disable or re-enable the lifecycle hook for a scaling group as needed.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
On the Scaling Groups page, find the scaling group that you want to manage, and click Details in the Actions column.
At the top of the scaling group details page, click the Lifecycle Hooks tab.
Disable or enable the lifecycle hook.
Disable a lifecycle hook
If a lifecycle hook is not temporarily needed, you can disable it.
Find the lifecycle hook that you want to manage, and click Disable in the Actions column.
In the dialog box that appears, click OK.
Enable a lifecycle hook
If you need to use a lifecycle hook for a scaling group, you can re-enable one that has been disabled.
Find the lifecycle hook that you want to manage, and click Enable in the Actions column.
In the dialog box that appears, click OK.
References
CreateLifecycleHook: Creates one or more lifecycle hooks.
ModifyLifecycleHook: Modifies a lifecycle hook.
DeleteLifecycleHook: Deletes a lifecycle hook.
CompleteLifecycleAction: Ends the wait state of a scaling activity before the timeout period expires.