You can use lifecycle hooks to suspend scaling activities that are triggered by Auto Scaling and put Elastic Compute Service (ECS) instances into the pending state. You can perform operations on ECS instances until the timeout period of your lifecycle hooks ends. This topic describes how to create 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 to allow you to receive notifications when lifecycle hooks are triggered. For more information, see Create a topic or Create a queue.

Background information

  • You can create lifecycle hooks for scaling groups that contain ECS instances. You cannot create lifecycle hooks for scaling groups that contain elastic container instances.
  • You can create up to eight lifecycle hooks for a scaling group.
  • You can resume a scaling process that is suspended by calling the CompleteLifecycleAction API operation or deleting the lifecycle hook that is used to suspend the scaling process.

Procedure

  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 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 When scaling activities of the specified type occur, the lifecycle hook is triggered and the ECS instances enter the pending state. Valid values:
    • Scale-in Event
    • Scale-out Event
    Timeout Period During the timeout period, ECS instances remain in 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 to take after the lifecycle hook times out. Valid values:
    • Continue: For scale-out activities, Auto Scaling continues to add ECS instances to the scaling group. For scale-in activities, Auto Scaling stops ECS instances from providing computing power in the scaling group.
    • Reject: For scale-out activities, Auto Scaling releases ECS instances. For scale-in activities, Auto Scaling stops ECS instances 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 to the scaling group for scale-out activities. For scale-in activities, Auto Scaling stops ECS instances from providing computing power in the scaling group.
        • If the execution fails, Auto Scaling releases ECS instances for scale-out activities. For scale-in activities, Auto Scaling stops ECS instances from providing computing power in the scaling group.
      • The following rules apply when Auto Scaling stops ECS 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 are automatically created, or if you set the Instance Reclaim Mode parameter of the scaling group to Release, manually create ECS instances, and use the scaling group to manage the lifecycle of the ECS instances, Auto Scaling removes the ECS 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 are automatically created, or if you set the Instance Reclaim Mode parameter of the scaling group to Economical Mode, manually create ECS instances, and use the scaling group to manage the lifecycle of the ECS instances, Auto Scaling stops the ECS instances during scale-in activities but retains some resources. For more information, see Create a scaling group.
        • If you manually create ECS instances and do not use the scaling group to manage the lifecycle of the ECS instances, Auto Scaling only removes the ECS instances from the scaling group during scale-in activities and the ECS instances that are removed can still run.
    Notification Method When the lifecycle hook is triggered, Auto Scaling sends a notification or performs actions based on the notification method that you specify. Valid values:
    • No Notification: This is the default value.
    • MNS Topic: If you select this option, 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, 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, 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 "Create a RAM role for OOS" in Grant RAM permissions to OOS.
        • The RAM role must be granted the required permissions to use resources such as ECS and Auto Scaling. Click the name of the RAM role that you created. On the page that appears, you can view the policy that is attached to the role. For information about how to attach an authorization policy to a RAM role, see "Attach the required policy to the OOS-trusted role" in 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.

Result

After you create a lifecycle hook, you can view the lifecycle hook on the Lifecycle Hook tab of the scaling group details page.

When Auto Scaling triggers a scaling activity, the ECS instance enters the pending state. You can view the status of the ECS instance on the Instances tab of the scaling group details page, as shown in the following figure. During the timeout period of the lifecycle hook, you can install applications or perform other operations on the ECS instances.

Scale-out activity