All Products
Search
Document Center

Auto Scaling:Manage lifecycle hooks

Last Updated:Dec 28, 2023

You can use lifecycle hooks to suspend scaling activities that are triggered by Auto Scaling. During the effective period of a lifecycle hook, Elastic Compute Service (ECS) instances or elastic container instances wait to be added to or removed from the scaling group to which you apply the lifecycle hook. You can perform custom operations on the instances, such as preinstall software, before the lifecycle hook times out. This topic describes how to manage lifecycle hooks.

Prerequisites

  • A scaling group is created. For more information, see Manage scaling groups.

  • (Optional) If you want to receive Message Service (MNS) notifications, make sure that an MNS topic or MNS queue is created in advance. For more information, see Create a topic or Create a queue.

Background information

Lifecycle hooks can be used to manage the lifecycles of ECS instances or elastic container instances in a scaling group. For more information, see Overview.

Create lifecycle hooks

You can create up to 10 lifecycle hooks in a scaling group. In this example, only one lifecycle hook is created.

  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 the region where Auto Scaling is activated.

  4. Find a scaling group and use one of the following methods to go to 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.

  5. In the upper part of the scaling group details page, click the Lifecycle Hook tab.

  6. In the upper-left corner of the page that appears, click Create Lifecycle Hook.

  7. In the Create Lifecycle Hook dialog box, configure parameters as prompted.

    The following table describes the parameters.

    Parameter

    Description

    Name

    The name of the lifecycle hook. You cannot change the name of a lifecycle hook after you create the lifecycle hook. 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

    The type of scaling activity to which you want to apply the lifecycle hook. When a scaling activity of the specified type is triggered, the lifecycle hook immediately takes effect and the ECS instances or elastic container instances that you want to add to or remove from your scaling group enter the Pending Add or Pending Remove state. Valid values:

    • Scale-in Event

    • Scale-out Event

    Timeout Period

    The timeout period of the lifecycle hook. The value of this parameter must be an integer that ranges from 30 to 21600. Unit: seconds. Default value: 1800.

    Note

    We recommend that you specify a timeout period based on your business requirements. If the timeout period is shorter than the required period of time, Auto Scaling executes the default execution policy.

    Default Execution Policy

    Scenario 1: Only one lifecycle hook is created

    The action that you want Auto Scaling to perform after the lifecycle hook times out. Valid values:

    • Continue: Auto Scaling continues to add ECS instances or elastic container instances to your scaling group during scale-outs or continues to remove ECS instances or elastic container instances from your scaling group and then releases the ECS instances or elastic container instances during scale-ins.

    • Reject: Auto Scaling releases ECS instances or elastic container instances that are in the Pending Add state during scale-outs or continues to remove ECS instances or elastic container instances from your scaling group and then releases the ECS instances or elastic container instances during scale-ins.

    • Roll Back: Auto Scaling releases ECS instances or elastic container instances that are in the Pending Add state during scale-outs or re-adds ECS instance or elastic container instances that are in the Pending Remove state to your scaling group during scale-ins.

    Scenario 2: Multiple lifecycle hooks are created

    If you have multiple lifecycle hooks, the subsequent action that Auto Scaling performs varies based on the value of the Scaling Activity parameter that you specify for each lifecycle hook.

    • If you create multiple lifecycle hooks whose Default Execution Policy parameter is set to Reject for scale-ins, Auto Scaling removes ECS instances or elastic container instances from your scaling group and then releases the ECS instances or elastic container instances during scale-ins when one of the lifecycle hook times out. In this case, other lifecycle hooks time out ahead of schedule.

      For example, you set the Timeout Period parameter of Lifecycle Hook A whose Default Execution Policy parameter is set to Reject to 600 seconds and set the Timeout Period parameter of Lifecycle Hook B to 1,200 seconds. When Lifecycle Hook A times out during a scale-in, Lifecycle Hook B times out 600 seconds earlier.

    • If you create multiple lifecycle hooks whose Default Execution Policy parameter is set to Continue for scale-ins, Auto Scaling must wait until all lifecycle hooks time out before removing ECS instances or elastic container instances from your scaling group and then releasing the ECS instances or elastic container instances.

    • If you create multiple lifecycle hooks for scale-outs, Auto Scaling must wait until all lifecycle hooks time out to perform the subsequent action, regardless of the value of the Default Execution Policy parameter that you specify for each lifecycle hook.

    (Optional) Scenario 3: An OOS template is used

    If you set the Send Notification When Lifecycle Hook Takes Effect parameter to OOS Template for a lifecycle hook, the subsequent action that Auto Scaling performs after the lifecycle hook times out varies based on the execution result of the CloudOps Orchestration Service (OOS) template.

    • If the execution of the OOS template is successful, Auto Scaling continues to add ECS instances or elastic container instances to your scaling group during scale-outs or stops ECS instances or elastic container instances from providing computing power during scale-ins.

    • If the execution of the OOS template fails, Auto Scaling releases ECS instances or elastic container instances that are in the Pending Add state during scale-outs or continues to stop ECS instances or elastic container instances from providing computing power during scale-ins.

    When Auto Scaling stops ECS instances or elastic container instances from providing computing power in a scaling group, the following rules apply:

    • If the ECS instances or elastic container instances are automatically created or the ECS instances or elastic container instances are manually created and managed by the scaling group whose Instance Reclaim Mode parameter is set to Release, Auto Scaling removes the ECS instances or elastic container instances from the scaling group.

    • If the ECS instances or elastic container instances are automatically created or the ECS instances or elastic container instances are manually created and managed by the scaling group whose Instance Reclaim Mode parameter is set to Economical Mode, Auto Scaling stops the ECS instances or elastic container instances but retains specific resources of the instances in the scaling group. For more information, see Manage scaling groups.

    • If the ECS instances or elastic container instances are manually created but not managed by the scaling group, Auto Scaling removes the ECS instances or elastic container instances from the scaling group. The removed ECS instances or elastic container instances can independently run.

    Send Notification When Lifecycle Hook Takes Effect

    Specify whether to send notifications when the lifecycle hook takes effect. If you want to receive notifications when the lifecycle hook takes effect, specify a notification method. 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 the notification to the MNS topic. You can manage 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 the notification to the MNS queue. You can manage notifications by ID.

    • OOS Template: If you select this option, you must specify the template type and configure the template parameters.

      • Auto Scaling supports the following types of OOS templates: Public Templates and My Templates. Auto Scaling uses the template that you selected to perform actions on ECS instances or elastic container instances.

        Note
        • Before you can select My Templates, you must create a template. 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 Resource Access Management (RAM) role for OOS, the following requirements must be met:

        • The trusted entity type of the RAM role is Alibaba Cloud Service. The trusted service is CloudOps Orchestration Service. For more information, see Grant RAM permissions to OOS.

        • The RAM role must have the required permissions to use resources such as ECS, Elastic Container Instance, and Auto Scaling. You can view the authorized 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 information about how to configure template parameters, see Overview of best practices for lifecycle hooks and OOS templates.

    • Event Bus: If you select this option, Auto Scaling sends different types of lifecycle hook notifications. You can identify and handle events in a flexible manner based on the notifications.

  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 a scaling activity of the specified type is triggered by Auto Scaling, the lifecycle hook takes effect and ECS instances or elastic container instances that are involved in the scaling activity enter the Pending Add or Pending Remove state. The following figure shows the status of ECS instances when a lifecycle hook takes effect.加入挂起中

Modify or delete lifecycle hooks

You can modify a lifecycle hook based on your business requirements. You can also delete a lifecycle hook that you no longer require. For example, when a lifecycle hook takes effect during a scaling activity, you can perform the following steps to modify or delete the lifecycle hook. You can also call the CompleteLifecycleAction API operation to end the timeout period of the lifecycle hook ahead of schedule, or 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 and click Details in the Actions column.

  2. On the scaling group details page, click the Lifecycle Hook tab.

  3. Modify or delete a lifecycle hook based on your business requirements.

    Action

    Scenario

    Procedure

    Modify a lifecycle hook

    If your lifecycle hook no longer meets your business requirements, you can modify the parameters of the lifecycle hook, without the 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 parameters of the lifecycle hook and click OK.

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

    Delete a lifecycle hook

    If you no longer require a lifecycle hook, you can delete the lifecycle hook to free up the lifecycle hook quota.

    Note

    If you delete a lifecycle hook that is in effect, the ECS instances or elastic container instances to which the lifecycle hook is applied exit the Pending Add or Pending Remove state ahead of schedule.

    1. Find the lifecycle hook that you want to delete and click Delete in the Actions column.

    2. (Optional)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 Delete Lifecycle Hook message, click OK.

Disable or enable lifecycle hooks

By default, a lifecycle hook immediately enters the Effective state after you create it. You can enable or disable lifecycle hooks based on your business requirements.

  1. On the Scaling Groups page, find the scaling group and click Details in the Actions column.

  2. On the scaling group details page, click the Lifecycle Hook tab.

  3. Disable or enable a lifecycle hook based on your business requirements.

    Action

    Scenario

    Procedure

    Disable a lifecycle hook

    If you want to remove a lifecycle hook from the Effective state, you can disable the lifecycle hook.

    1. Find the lifecycle hook that you want to disable and click Disable in the Actions column.

    2. In the Delete Lifecycle Hook message, click OK.

    Enable a lifecycle hook

    If you want to use a lifecycle hook, you can enable the lifecycle hook.

    1. Find the lifecycle hook that you want to enable and click Enable in the Actions column.

    2. In the Delete Lifecycle Hook message, click OK.

References

User guide

API references

Manage scaling groups

CreateScalingGroup

Create lifecycle hooks

CreateLifecycleHook

Modify or delete lifecycle hooks

Create a topic

CreateTopic

Create a queue

CreateQueue

N/A

CompleteLifecycleAction