You can use lifecycle hooks to suspend scaling activities that are triggered by Auto Scaling and to put Elastic Compute Service (ECS) instances or elastic container instances into the pending state. You can perform custom operations on ECS instances or elastic container instances until the timeout periods of your lifecycle hooks end. This topic describes how to configure a lifecycle hook.

Prerequisites

  • A scaling group is created. For more information, see Create a scaling group.
  • Optional:A Message Service (MNS) topic or queue is created. In this case, you can receive notifications when lifecycle hooks are triggered. For more information, see Create a topic or Create a queue.

Background information

A lifecycle hook is a tool that is used to manage the lifecycles of Elastic Compute Service (ECS) instances or elastic container instances in a scaling group. For more information, see Overview.

Create a lifecycle hook

You can create up to 10 lifecycle hooks for a scaling group.

  1. Log on to the Auto Scaling console.
  2. In the left-side navigation pane, click Scaling Groups.
  3. In the top navigation bar, select a region.
  4. Find a scaling group and go to the details page of the scaling group. You can use one of the following methods to go to the details page:
    • Click the ID of the scaling group in the Scaling Group Name/ID column.
    • Click Details in the Actions column of the scaling group.
  5. In the upper part of the page, click the Lifecycle Hook tab.
  6. In the upper-left corner of the Lifecycle Hook tab, click Create Lifecycle Hook.
  7. In the dialog box that appears, configure the parameters.
    The following table describes the parameters.
    Parameter Description
    Name The name of the lifecycle hook that you want to create. You cannot change the name of a lifecycle hook after the lifecycle hook is created. The name must be 2 to 64 characters in length and can contain letters, digits, periods (.), underscores (_), and hyphens (-). The name must start with a letter or a digit.
    Scaling Activity When scaling activities of the specified type occur, the lifecycle hook is triggered and the ECS instances or elastic container instances that you want to add or remove are put into the pending state. Valid values:
    • Scale-in Event
    • Scale-out Event
    Timeout Period The period of time during which ECS instances or elastic container instances are put into the pending state. The value of this parameter must be an integer that ranges from 30 to 21600. Unit: seconds.
    Note We recommend that you configure the Timeout Period parameter based on your business requirements. If the timeout period is shorter than the required period of time, the system executes the default execution policy.
    Default Execution Policy The action that you want the system to perform after the timeout period ends. Valid values:
    • Continue: For scale-out activities, Auto Scaling continues to add ECS instances or elastic container instances to the scaling group. For scale-in activities, Auto Scaling stops ECS instances or elastic container instances that you no longer need from providing computing power in the scaling group.
    • Reject: For scale-out activities, Auto Scaling releases ECS instances or elastic container instances that you want to add. For scale-in activities, Auto Scaling stops ECS instances or elastic container instances that you no longer need from providing computing power in the scaling group.
      Notice

      If you set Notification Method to OOS Template, the action that is performed after a lifecycle hook times out varies based on the execution result of the OOS template.

      • If the execution is successful, Auto Scaling continues to add ECS instances or elastic container instances to the scaling group for scale-out activities. For scale-in activities, Auto Scaling stops ECS instances or elastic container instances from providing computing power in the scaling group.
      • If the execution fails, Auto Scaling releases ECS instances or elastic container instances for scale-out activities. For scale-in activities, Auto Scaling stops ECS instances or elastic container instances from providing computing power in the scaling group.
    The following rules apply when Auto Scaling stops ECS instances or elastic container instances from providing computing power in a scaling group:
    • If you set the Instance Reclaim Mode parameter of the scaling group to Release and ECS instances or elastic container instances are automatically created, or if you set the Instance Reclaim Mode parameter of the scaling group to Release, manually create ECS instances or elastic container instances, and use the scaling group to manage the lifecycles of the ECS instances or elastic container instances, Auto Scaling removes the ECS instances or elastic container instances from the scaling group during scale-in activities.
    • If you set the Instance Reclaim Mode parameter of the scaling group to Economical Mode and ECS instances or elastic container instances are automatically created, or if you set the Instance Reclaim Mode parameter of the scaling group to Economical Mode, manually create ECS instances or elastic container instances, and use the scaling group to manage the lifecycles of the ECS instances or elastic container instances, Auto Scaling stops the ECS instances or elastic container instances during scale-in activities but retains some resources. For more information, see Create a scaling group.
    • If you manually create ECS instances or elastic container instances and do not use the scaling group to manage the lifecycles of the ECS instances or elastic container instances, Auto Scaling only removes the ECS instances or elastic container instances from the scaling group during scale-in activities. The ECS instances or elastic container instances that are removed can continue to run.
    Notification Method When the lifecycle hook is triggered, Auto Scaling sends a notification based on the notification method that you specify or performs actions. Valid values:
    • No Notification: This is the default value.
    • MNS Topic: If you select this option, you must select an MNS topic. If you specify a notification ID, the notification ID is sent together with notifications to the MNS topic. This way, you can manage the notifications by ID.
    • MNS Queue: If you select this option, you must select an MNS queue. If you specify a notification ID, the notification ID is sent together with notifications to the MNS queue. This way, you can manage the notifications by ID.
    • OOS Template: If you select this option, you must specify the template type and configure the template parameters.
      • Template types include Public Templates and My Templates. Auto Scaling uses the template that you select to perform actions on ECS instances.
        Note
        • A template must be created before you select My Templates. For information about how to create a template, see Create a template.
        • For information about public templates, see Public Templates.
      • Configure the template parameters based on your business requirements. If you want to configure a RAM role for OOS, take note of the following points:
        • A RAM role is created for OOS. The trusted entity type of the RAM role is Alibaba Cloud Service. The trusted service is Operation Orchestration Service. For more information, see Grant RAM permissions to OOS.
        • The RAM role must be granted the required permissions to use resources such as ECS, Elastic Container Instance, and Auto Scaling. You can view the authorization policy to obtain the policy document. For information about how to attach a policy to a RAM role, see Grant RAM permissions to OOS.
          Note For more examples on how to configure the template parameters, see Overview of best practices for lifecycle hooks and OOS templates.
  8. Click OK.
    After you create the lifecycle hook, you can view the lifecycle hook on the Lifecycle Hook tab of the scaling group details page.
    When Auto Scaling triggers the specified type of scaling activity, the ECS instances or elastic container instances that you want to scale are put into the pending state.Scale-out activity

Modify or delete a lifecycle hook

You can modify a lifecycle hook based on your business requirements. You can also delete a lifecycle hook that you no longer need. When a scaling activity is triggered and a lifecycle hook is applied, you can perform the following steps to delete the lifecycle hook or call the CompleteLifecycleAction API operation to end the timeout period of the lifecycle hook ahead of schedule. You can also call the DeleteLifecycleHook API operation to delete the lifecycle hook and resume the scaling activity. For more information, see CompleteLifecycleAction and DeleteLifecycleHook.

  1. On the Scaling Groups page, find the scaling group whose lifecycle hook you want to modify or delete and click Details in the Actions column.
  2. On the scaling group details page, click the Lifecycle Hook tab.
  3. On the Lifecycle Hook tab, find the lifecycle hook and click Modify or Delete in the Actions column.
    Feature Scenario Procedure
    Modify a lifecycle hook If your lifecycle hook no longer meets your business requirements, you need to only modify the parameter settings of the lifecycle hook. You do not need to create a new lifecycle hook.
    1. Find the lifecycle hook that you want to modify and click Modify in the Actions column.
    2. Modify the parameter settings for the lifecycle hook and click OK.

      You cannot change the name of an existing lifecycle hook. For information about other parameters, see Step 8.

    Delete a lifecycle hook If you no longer need a lifecycle hook, you can delete the lifecycle hook to free up quota.
    Note If an ECS instance or elastic container instance is put into the pending state due to a lifecycle hook, the ECS instance or elastic container instance exits the pending state ahead of schedule after you delete the lifecycle hook.
    1. Find the lifecycle hook that you want to delete and click Delete in the Actions column.
    2. Optional: If you want to delete multiple lifecycle hooks at the same time, select the lifecycle hooks and click Delete in the lower part of the Lifecycle Hook tab.
    3. In the message that appears, click OK.

References

Documentation API reference
Create a scaling group CreateScalingGroup
Create a lifecycle hook CreateLifecycleHook
Modify or delete a lifecycle hook
Create a topic CreateTopic
Create a queue CreateQueue
N/A CompleteLifecycleAction