as
  1. Help Center
  2. AS Management
  3. Scaling Actions
  4. Managing Lifecycle Hooks

Managing Lifecycle Hooks

Lifecycle hooks enable you to flexibly control creation and removal of ECS instances in AS groups and manage the lifecycle of ECS instances in AS groups. Figure 1 shows the instance lifecycle when no lifecycle hook is added to the AS group.

Figure 1 Instance lifecycle statuses when no lifecycle hook is added to the AS group

Figure 2 shows the instance lifecycle when a lifecycle hook is added to the AS group.

Figure 2 Instance lifecycle statuses when a lifecycle hook is added to the AS group

When the AS group performs a scaling action and triggers the lifecycle hook, the scaling action is suspended and the instance that is being added to or removed from the AS group is set to waiting state, as shown in 2 and 6 in Figure 2. During this period, you can perform some custom operations on the instance. For example, you can install or configure software on an instance to be added to the AS group. The suspension of a scaling action will be ended in either of the following scenarios:

  • The time when the instance stays in waiting state is longer than the timeout duration.
  • A callback operation is performed to stop the instance waiting state.

Application Scenarios

  • Instances newly added to an AS group can be bound to a load balancer only after initialization (software installation or configuration) has been performed on the instances and services start running properly.
  • Instances can be removed from an AS group only after they are unbound from the load balancer and have finished processing ongoing requests.
  • Before instances are removed from an AS group, data needs to be backed up and logs need to be downloaded.
  • Other scenarios where custom operations need to be performed

Working Rules

After added to an AS group, a lifecycle hook works as follows:

  • Adding an ECS instance to an AS group

    After an instance is added to an AS group and initialized, a lifecycle hook of the Instance adding type is automatically triggered. The instance enters the Wait (Adding to AS group) 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. After finishing the custom operations, you can perform a callback operation to end the instance waiting state. Alternatively, you can wait until the timeout duration ends, when the system automatically ends the instance waiting state. After the instance waiting state ends, two types of Default Callback Action are available, Continue and Abandon.

    • Continue: The instance in waiting state will be added to the AS group.
    • Abandon: The instance in waiting state 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 one 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 instance is added to the AS group after suspension by the last lifecycle hook is complete.

  • Removing an instance from an AS group

    When an instance is removed from an AS group, the instance enters the Removing from AS group state. After a lifecycle hook is triggered, the instance enters the Wait (Removing from AS group) 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. After finishing the custom operations, you can perform the default callback operation or wait for the system to end the instance waiting state when the timeout duration expires. After the instance waiting state ends, two types of Default Callback Action are available, Continue and Abandon.

    • Continue: The instance is removed from the AS group.
    • Abandon: The instance is removed from the AS group.

    If there are multiple lifecycle hooks, Continue allows suspension by other lifecycle hooks to time out. The instance will be removed from the AS group only when the status of all lifecycle hooks is Continue. If the Default Callback Action of any lifecycle hook is Abandon, the instance will be directly removed from the AS group.

Use Restrictions

  • 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.

Add a Lifecycle Hook

  1. Log in to the management console.
  2. Under Computing, click Auto Scaling. Then, on the Auto Scaling page, click the AS Group tab.
  3. Click the name of the AS group to which the lifecycle hook is to be added. On the AS group details page, click the Lifecycle Hooks tab and then Add Lifecycle Hook.
  4. In the displayed Add Lifecycle Hook dialog box, set the parameters listed in Table 1.
    Table 1 Parameters to be configured

    Parameter

    Description

    Example Value

    Hook Name

    Specifies the lifecycle hook name. The name is a string of 1 and 32 characters and can contain 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 sets an instance that is being added to an AS group to Wait (Adding to AS group) state. Instance removal sets an instance that is being removed from an AS group Wait (Removing from AS group) state.

    Instance adding

    Default Callback Action

    Specifies the default system action when the waiting duration of an instance expires.

    When an instance is being added to an AS group, the default callback action can be Continue or Abandon:
    • Continue: When only one lifecycle hook is available, the instance will continue to be added to the AS group. When multiple lifecycle hooks are available, the instance will continue to be added to the AS group only when all lifecycle hooks change to Continue state.
    • Abandon: The system deletes the instance and creates a new one only when one lifecycle hook is in Abandon state, regardless of whether one or multiple lifecycle hooks are available.
    When an instance is being removed from an AS group, the default callback action can be Continue or Abandon:
    • Continue: When only one lifecycle hook is available, the instance will be removed from the AS group. When multiple lifecycle hooks are available, the instance will be removed from the AS group only when all lifecycle hooks change to Continue state.
    • Abandon: The system removes the instance from the AS group when one lifecycle hook is in Abandon state, regardless of whether one or multiple lifecycle hooks are available.

    Continue

    Timeout (s)

    Specifies the instance waiting duration by default. Value range: 300s to 86400s

    You can prolong the timeout duration or perform the Continue or Abandon operation before the timeout duration expires. For more information about callback actions, see Perform a Callback Action.

    3600

    Notification Topic

    Specifies a notification object for a lifecycle hook. For details, see section "Configuring a Topic Policy" in Simple Message Notification User Guide. After the configuration, 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 customized notification content, and the token for controlling lifecycle operations. 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"
     }
    }

    N/A

    Notification Message

    After a notification object is configured, the system sends your customized notification to the object.

    N/A

  5. Click OK.

    The added lifecycle hook is displayed on the Lifecycle Hooks page.

Perform a Callback Action

  1. On the AS Groups page, click the name of the target AS group.
  2. On the displayed page, click the Instances tab.
  3. Locate the instance that has been suspended by the lifecycle hook and click Wait (Adding to AS group) or Wait (Removing from AS group) in the Lifecycle Status column.
    NOTE:

    Callback operations can only be performed on instances that have been suspended by a lifecycle hook.

  4. In the displayed Added Hook dialog box, view the suspended instance and all the lifecycle hooks, and perform callback actions on lifecycle hooks.

    Callback actions include:

    • Continue
    • Abandon
    • Extend timeout

    If you have performed customized operations before the timeout duration expires, select Continue or Abandon to complete the lifecycle operations. For details about Continue and Abandon, see Table 1. If you require more time for customizing operations, select Extend timeout to prolong the timeout duration. Then, the instance waiting duration will be prolonged by 3600 sections each time.

Modify a Lifecycle Hook

On the Lifecycle Hooks page, locate the target lifecycle hook and click Modify in the Operation column. You can modify the parameter except Hook Name, such as Hook Type, Default Callback Action, and Timeout Duration.

Delete a Lifecycle Hook

On the Lifecycle Hooks page, locate the target lifecycle hook and click Delete in the Operation column.