The lifecycle hook feature can pause automatically triggered scaling activities to put Elastic Compute Service (ECS) instances or elastic container instances into a Pending state. This gives you a period of time to perform custom operations on the instances. For example, you can preinstall software on instances that are in the Pending Add state. You can resume scaling activities that are paused by lifecycle hooks only after the lifecycle hooks time out. This topic describes how to create, modify, delete, enable, or disable a lifecycle hook.
Prerequisites
A scaling group is created. For more information, see Manage scaling groups or CreateScalingGroup.
(Optional) A Message Service (MNS) topic or queue is created if you want to send MNS notifications when a lifecycle hook is triggered.
For information about how to create a topic, see Create a topic or CreateTopic.
For information about how to create a queue, see Create a queue or CreateQueue.
Create a lifecycle hook
A lifecycle hook is a tool that is used to manage the lifecycles of ECS instances or elastic container instances in a scaling group. For more information, see Overview. You can create up to 10 lifecycle hooks in a scaling group. In this example, only one lifecycle hook is created for your reference.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
Find the desired scaling group and use one of the following methods to open 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.
In the upper part of the details page, click the Lifecycle Hook tab.
In the upper-left corner of the page that appears, click Create Lifecycle Hook.
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 occurs, the lifecycle hook is triggered to put the ECS instances or elastic container instances that you want to add to or remove from your scaling group into 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.
NoteWe 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-out events, 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-in events.
Reject: Auto Scaling releases ECS instances or elastic container instances that are in the Pending Add state during scale-out events, 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-in events.
Roll Back: Auto Scaling releases ECS instances or elastic container instances that are in the Pending Add state during scale-out events or re-adds ECS instances or elastic container instances that are in the Pending Remove state to your scaling group during scale-in events.
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 set the Scaling Activity parameter to Scale-in Event and set the Default Execution Policy parameter for one of the lifecycle hooks to Continue, Auto Scaling executes the default execution policy only after all the lifecycle hooks time out. If you set the Default Execution Policy parameter for one of the lifecycle hooks to Reject or Roll Back, Auto Scaling ends the timeout periods of all the lifecycle hooks ahead of schedule after that lifecycle hook times out. If you set the Default Execution Policy parameter for that lifecycle hook to Roll Back, Auto Scaling stops the scale-in process and rolls back all the changes. If you set the Default Execution Policy parameter for that lifecycle hook to Reject, Auto Scaling continues to execute the scale-in process.
If you set the Scaling Activity parameter to Scale-out Event and set the Default Execution Policy parameter for all lifecycle hooks to Continue, Auto Scaling continues to execute the scale-out process only after all the lifecycle hooks time out. If you set the Default Execution Policy parameter for one of the lifecycle hooks to Reject or Roll Back, Auto Scaling ends the timeout periods of all the lifecycle hooks ahead of schedule, stops the scale-out process, and then rolls back all the changes. Take note that the Roll Back setting and the Reject setting have the same effect during scale-out events.
(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-out events or continues to stop ECS instances or elastic container instances from providing computing power during scale-in events.
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-out events or continues to stop ECS instances or elastic container instances from providing computing power during scale-in events.
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 a 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 a 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 send 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.
NoteBefore you can select My Templates, you must create a template. For more information about how to create a template, see Create a template.
For more 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 rules apply:
When you create a RAM role for OOS, you must select Alibaba Cloud Service as the trusted entity and CloudOps Orchestration Service as the trusted service. For more information, see Create a RAM role for OOS.
You must grant the RAM role the required permissions on resources such as ECS, Elastic Container Instance, and Auto Scaling. To view the required policy, click View the permission policy that is required to execute the template. For information about how to attach a policy to a RAM role, see Attach the required policy to the OOS-trusted role.
NoteFor 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.
In the message that appears, 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.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
On the Scaling Groups page, find the scaling group and click Details in the Actions column.
On the scaling group details page, click the Lifecycle Hook tab.
Modify or delete a lifecycle hook based on your business requirements.
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 one.
Find the lifecycle hook that you want to modify and click Modify in the Actions column.
Modify the lifecycle hook based on your business requirements and click OK.
You cannot change the name of a lifecycle hook after you create the lifecycle hook. For information about lifecycle hook parameters, see Step 7.
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. For example, when a scaling activity is paused by a lifecycle hook, you can delete the lifecycle hook.
ImportantIf 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.
Delete one or more lifecycle hooks.
Delete one lifecycle hook: Find the lifecycle hook that you want to delete and click Delete in the Actions column.
Delete multiple lifecycle hooks: Select the lifecycle hooks that you want to delete and click Delete in the lower-left corner of the page.
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.
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select a region.
On the Scaling Groups page, find the desired scaling group and click Details in the Actions column.
On the scaling group details page, click the Lifecycle Hook tab.
Disable or enable a lifecycle hook based on your business requirements.
Disable a lifecycle hook
If your business does not require a lifecycle hook that is in effect for now, you can disable the lifecycle hook.
Find the lifecycle hook that you want to disable and click Disable in the Actions column.
In the Disable Lifecycle Hook message, click OK.
Enable a lifecycle hook
If your business requires a lifecycle hook that is disabled, you can enable the lifecycle hook.
Find the lifecycle hook that you want to enable and click Enable in the Actions column.
In the Enable Lifecycle Hook message, click OK.
What to do next
For information about how to create one or more lifecycle hooks by calling an API operation, see CreateLifecycleHook.
For information about how to modify a lifecycle hook by calling an API operation, see ModifyLifecycleHook.
For information about how to delete a lifecycle hook by calling an API operation, see DeleteLifecycleHook.
For information about how to end a lifecycle hook ahead of schedule, see CompleteLifecycleAction.