Lifecycle Hooks
What Is a Lifecycle Hook?
Lifecycle hooks enable you to flexibly control addition and removal of ECS instances in AS groups and manage the lifecycle of ECS instances in AS groups. Figure 1 shows the instance lifecycle if no lifecycle hook is added to an AS group. Figure 2 shows the instance lifecycle if a lifecycle hook is added to an AS group.
When the AS group scales in or out, the added lifecycle hooks are triggered, the scaling action is suspended, and the instance being added or removed is put into a wait state, as shown in 2 and 6 in Figure 2. During this period of time, you can perform some custom operations on the instance. For example, you can install or configure software on an instance being added to the AS group. After completing the custom operations, you can resume the suspended scaling action if either of the following occurs:
- The timeout duration ends.
Assume that you have set the timeout period to 3,600s by referring to Table 1. The suspended scaling action will be automatically resumed if the timeout duration (3,600s) ends.
- A callback action is performed to move the instance out of the wait state. For details, see Performing a Callback Action.
This section describes how to add, modify, and delete a lifecycle hook and how to perform a callback action.
How Lifecycle Hooks Work
After you add lifecycle hooks to an AS group, they work as follows:
- Adding an ECS instance to an AS group
When an instance is initialized and added to an AS group, a lifecycle hook of the Instance adding type is automatically triggered. The instance enters the Pending addition state, that is, the instance is suspended by the lifecycle hook. If you have configured a notification object, the system sends a message to the object. After receiving the message, you can perform custom operations, for example, installing software on the instance. The instance remains in a wait state until you complete the custom operations and perform a callback action (see Performing a Callback Action) or the timeout duration ends. After the instance moves out of a wait state, the specified default callback action will take place.
- Continue: The instance will be added to the AS group.
- Abandon: The instance will be deleted and a new instance will be created.
If you have configured multiple Instance adding lifecycle hooks, all of them will be triggered when an instance is added to the AS group. If the default callback action of any lifecycle hook is Abandon, the instance will be deleted and a new instance will be created. If the default callback action of all lifecycle hooks is Continue, the system waits until the last hook finishes its suspension before adding the instance to the AS group.
- Removing an instance from an AS group
When an instance is removed from an AS group, the instance enters the Removing state. After a lifecycle hook is triggered, the instance enters the Pending removal state. The system sends messages to the configured notification object. After receiving the message, you can perform custom operations, such as uninstalling software and backing up data. The instance remains in the wait state until you finish the custom operations and perform the default callback operation or the timeout duration ends. After the instance moves out of a wait state, the specified default callback action will take place.
- Continue: The instance is removed from the AS group.
- Abandon: The instance is removed from the AS group.
If multiple lifecycle hooks are configured, Continue indicates that the instance continues to wait for other hooks to time out. The instance is removed from the AS group only when the default callback action of all hooks is Continue. If the default callback action of any lifecycle hook is Abandon, the instance will be directly removed from the AS group.
Application Scenarios
- Instances newly added to an AS group need to be initialized before they are bound to a load balancer listener. Initialization means software is installed and configured and the instance is fully ready to process requests.
- To remove an instance from an AS group, it must be unbound from the load balancer listener, stop accepting new requests, and finish processing any accepted requests.
- Before instances are removed from an AS group, you may need to back up data or download logs.
- Other scenarios where custom operations need to be performed.
Constraints
- You can add, modify, or delete a lifecycle hook when the AS group does not perform a scaling action.
- Up to five lifecycle hooks can be added to one AS group.
Adding a Lifecycle Hook
A lifecycle hook can be added to suspend scaling actions and put instances to the wait state. During this wait period, you can perform custom operations, such as installing or configuring software.
- Log in to the AS console.
- Click the name of the AS group for which you want to add a lifecycle hook. The AS group overview page is displayed.
- Click the Lifecycle Hooks tab and then click Add Lifecycle Hook.
- In the displayed Add Lifecycle Hook dialog box, set the parameters listed in Table 1. Figure 3 Adding a Lifecycle Hook
Table 1 Parameter description Parameter
Description
Example
Hook Name
Specifies the lifecycle hook name. The name contains 1 to 32 characters, including only letters, digits, underscores (_), and hyphens (-).
we12_w
Hook Type
Specifies the lifecycle hook type. The value can be Instance adding or Instance removal. Instance adding puts the instance being added to an AS group to the Pending addition state. Instance removal puts the instance being removed from an AS group to Pending removal state.
Instance adding
Default Callback Action
Specifies the action that the system takes when an instance moves out of a wait state.
The default callback action for an Instance adding lifecycle hook can be Continue or Abandon:- Continue: If multiple lifecycle hooks are configured for the AS group, and the default callback action of all the hooks is Continue, the system will continue to add the instance to the AS group until all lifecycle hooks time out.
- Abandon: If multiple lifecycle hooks are configured for the AS group, and the default callback action of one lifecycle hook is Abandon, the system will delete the instance and create another one without waiting for the remaining lifecycle hooks to time out.
The default callback action for an Instance removal lifecycle hook can be Continue or Abandon:- Continue: If only one lifecycle hook is configured for the AS group, the system will remove the instance from the AS group. If multiple lifecycle hooks are configured for the AS group, and the default callback actions of all the hooks are Continue, the system will continue to remove the instance from the AS group until all lifecycle hooks time out.
- Abandon: If multiple lifecycle hooks are configured for the AS group, and the default callback action of one lifecycle hook is Abandon, the system will continue to remove the instance from the AS group without waiting for the remaining lifecycle hooks to time out.
Continue
Timeout Duration (s)
Specifies the amount of time for the instances to remain in a wait state. The value ranges from 60s to 86,400s.
You can extend the timeout duration or perform a Continue or Abandon action before the timeout duration ends. For more information about callback actions, see Performing a Callback Action.
3600
Topic
Specifies a notification object for a lifecycle hook. (For details, see Creating a Topic). When an instance is suspended by the lifecycle hook, the system sends a notification to the object. This notification contains the basic instance information, your custom notification content, and the token for controlling lifecycle actions. An example notification is as follows:
{ "service": "AutoScaling", "tenant_id": "93075aa73f6a4fc0a3209490cc57181a", "lifecycle_hook_type": "INSTANCE_LAUNCHING", "lifecycle_hook_name": "test02", "lifecycle_action_key": "4c76c562-9688-45c6-b685-7fd732df310a", "notification_metadata": "xxxxxxxxxxxxx", "scaling_instance": { "instance_id": "89b421e4-5fa6-4733-bf40-6b07a8657256", "instance_name": "as-config-kxeg_RM6OCREY", "instance_ip": "192.168.0.202" }, "scaling_group": { "scaling_group_id": "fe376277-50a6-4e36-bdb0-685da85f1a82", "scaling_group_name": "as-group-wyz01", "scaling_config_id": "16ca8027-b6cc-45fc-af2d-5a79996f685d", "scaling_config_name": "as-config-kxeg" } }-
Notification Message
After a notification object is configured, the system sends your custom notification to the object.
-
- Click OK.
The added lifecycle hook is displayed on the Lifecycle Hooks page.
Performing a Callback Action
When an instance is in the wait state, you can manually perform a callback operation to end the wait state.
- Log in to the AS console.
- Click the name of the target AS group on the Instance Scaling page. The Overview page is displayed.
- Click the Instances tab.
- Locate the instance that has been suspended by the lifecycle hook and click Pending addition or Pending removal in the Lifecycle Status column, as shown in Figure 4.
Callback actions can only be performed on instances that have been suspended by a lifecycle hook.
- In the displayed Added Hook dialog box, view the suspended instance and all the lifecycle hooks, and perform callback actions on lifecycle hooks, as shown in Figure 5.
Callback actions include:
- Continue
- Abandon
- Extend timeout
If you have performed custom operations before the timeout duration ends, select Continue or Abandon to complete the lifecycle actions. For details about Continue and Abandon, see Table 1.
If you need more time for custom operations, select Extend timeout. Then, the timeout duration will be extended by 3,600s.
Modifying a Lifecycle Hook
You can modify the attributes of a lifecycle hook, including the hook type, default callback action, and timeout duration.
- Log in to the AS console.
- Click the name of the AS group for which you want to modify a lifecycle hook on the Instance Scaling page. The Overview page is displayed.
- Click the Lifecycle Hooks tab.
- On the Lifecycle Hooks tab, locate the row that contains the target lifecycle hook and click Modify in the Operation column. Figure 6 Modifying a Lifecycle Hook
- In the displayed dialog box, modify the required parameters by referring to Table 1. You can modify the parameters such as the hook name, hook type, default callback action, and timeout duration.

- Click OK.
Deleting a Lifecycle Hook
If you no longer need a lifecycle hook, you can delete it to release the lifecycle hook quota.
- Log in to the AS console.
- Click the name of the AS group for which you want to delete a lifecycle hook on the Instance Scaling page. The Overview page is displayed.
- On the Lifecycle Hooks tab, locate the row that contains the target lifecycle hook and click Delete in the Operation column. Figure 7 Deleting a lifecycle hook
- In the displayed dialog box, click OK.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot



