This topic describes the concepts, workflows, scenarios, and limits of lifecycle hooks.

What is a lifecycle hook?

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. When Auto Scaling executes a scaling activity, a lifecycle hook can be triggered to put ECS instances or elastic container instances in the scaling activity into the pending state. You can perform custom operations on the ECS instances or elastic container instances until the lifecycle hook times out.

The following table describes the parameters required to create a lifecycle hook.

Note For information about how to create a lifecycle hook, see Procedure.
Parameter Description
Scaling Activity When scaling activities of the specified type occur, the lifecycle hook is triggered and the ECS instances or elastic container instances enter the pending state.

Valid values: Scale-in Event and Scale-out Event. If you select Scale-out Event, the ECS instances or elastic container instances enter the Pending Add state when the lifecycle hook is triggered. If you select Scale-in Event, the ECS instances or elastic container instances enter the Pending Remove state when the lifecycle hook is triggered.

Timeout Period The period of time during which you can perform custom operations on ECS instances or elastic container instances when the instances are in the pending state. We recommend that you configure a timeout period based on your business requirements. After the timeout period expires, the system executes the default execution policy. You can delete the lifecycle hook or call the CompleteLifecycleAction API operation to manually end the timeout period ahead of schedule.
Default Execution Policy The action that you want Auto Scaling to perform after the timeout period ends. Valid values: Continue and Reject.
  • If you set Default Execution Policy to Continue, Auto Scaling adds ECS instances or elastic container instances to the scaling group during a scale-out activity and removes ECS instances or elastic container instances from the scaling group during a scale-in activity after the timeout period ends.
  • If you set Default Execution Policy to Reject, Auto Scaling releases ECS instances or elastic container instances during a scale-out activity and stops ECS instances or elastic container instances from providing computing power in the scaling group during a scale-in activity after the timeout period ends.
Note If you set Notification Method to OOS Template for a lifecycle hook, the action that is performed after the timeout period ends is determined by the execution result of the specified OOS template. If the execution is successful, the scaling activity continues. If the execution fails, the scale-out activity is rolled back and the scale-in activity continues.
Notification Method When the lifecycle hook is triggered, Auto Scaling sends a notification or performs actions based on the notification method that you specify.
For example, you can set Notification Method to MNS Topic or MNS Queue. This way, you can immediately receive notifications after the lifecycle hook is triggered and perform operations on the instances. You can also set Notification Method to OOS Template to automatically execute tasks.
Note You are charged for the use of Message Service (MNS). For more information, see MNS pricing.

Workflows

The following figures use ECS instances to describe the workflows of lifecycle hooks. Lifecycle hooks that are applied to elastic container instances work in a similar manner as lifecycle hooks that are applied to ECS instances.

Figure 1. Scale-out
Flowchart
Figure 2. Scale-in
Scale-in process - en
The following table describes the concepts that are displayed in the preceding figures.
Concept Description
Lifecycle hook A tool that is used to manage the lifecycles of ECS instances or elastic container instances in a scaling group. For more information, see What is a lifecycle hook?.
You use the scaling group to manage the lifecycles of the instances You use the scaling group to manage the lifecycles of ECS instances or elastic container instances. For information about the lifecycles of ECS instances or elastic container instances, see Instance lifecycle. The status of ECS instances or elastic container instances varies based on how the instances are created.
  • If you use the scaling group to manage the lifecycles of ECS instances or elastic container instances that are automatically created, Auto Scaling creates ECS instances or elastic container instances during scale-out activities and stops and releases the ECS instances or elastic container instances during scale-in activities.
  • If you use the scaling group to manage the lifecycles of the ECS instances or elastic container instances that are manually added, Auto Scaling stops and releases the ECS instances or elastic container instances during scale-in activities.
You manually manage the lifecycles of the instances You do not use the scaling group to manage the lifecycles of the ECS instances or elastic container instances that are manually added to the scaling group. In this case, Auto Scaling only removes ECS instances or elastic container instances from the scaling group during scale-in activities. The ECS instances or elastic container instances are not released.
Note You cannot use the scaling group to manage the lifecycles of subscription ECS instances that are added to the scaling group.

Scenarios

In the following scenarios, you can create lifecycle hooks to put ECS instances or elastic container instances into the pending state and perform operations on the instances before the timeout periods end:

  • The ECS instances or elastic container instances that are used for scale-out activities cannot immediately provide services.

    For example, you can create lifecycle hooks if the ECS instances need to be associated with ApsaraDB RDS instances or need to be bound to secondary elastic network interfaces (ENIs). You can also create lifecycle hooks if the applications that are deployed on the ECS instances require some time to start before they can provide services.

  • The ECS instances or elastic container instances that are used for scale-in activities cannot be immediately removed from scaling groups.

    For example, you must back up data on the ECS instances or elastic container instances, copy instance logs, or wait until the ECS instances or elastic container instances finish processing all requests before you remove the instances from the scaling groups.

For more information, see Overview of best practices for lifecycle hooks and OOS templates and Use lifecycle hooks to ensure service availability.

Limits

  • You can create up to 10 lifecycle hooks for each scaling group.
  • When a scaling activity is triggered in a scaling group, whether the expected number of instances is specified determines whether other scaling activities in the scaling group can be executed.
    • If the expected number of instances is not specified for the scaling group, Auto Scaling rejects other scaling activities.
    • If the expected number of instances is specified for the scaling group, Auto Scaling can execute other scaling activities only if the ongoing scaling activity is a parallel scaling activity. For information about how to determine a parallel scaling activity, see Terms.

How to configure a lifecycle hook

The following table describes the operations that you can perform on lifecycle hooks and the related links.

Documentation Operation API reference
Create a lifecycle hook Create a lifecycle hook CreateLifecycleHook
Modify a lifecycle hook ModifyLifecycleHook
Delete a lifecycle hook DeleteLifecycleHook