You can use lifecycle hooks to put ECS instances that are being added or removed into the wait state. After the ECS instances are put into the wait state, you can perform custom operations on them.
You can configure scaling configurations or launch templates to create ECS instances that have specified settings. However, to meet complex business requirements, you may want to perform custom operations on these instances before they are started. In this case, you can use lifecycle hooks.
- You can perform custom operations on these instances during the timeout period of a lifecycle hook. For example, you can initialize or obtain the configurations of the ECS instances.
- Whether other scaling activities in the scaling group can be executed depends on the
settings of the expected number of instances.
Note You can delete a lifecycle hook or call the CompleteLifecycleAction operation to terminate the wait state of a scaling activity in advance.
- 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 when the ongoing scaling activity is a parallel scaling activity. For information about how to determine a parallel scaling activity, see Glossary.
Assume that you have created a scaling group named asg-bp1iir8uwwpdsjfj****. The minimum number of instances that the scaling group must contain is zero. The scaling group has one lifecycle hook for scale-out events and does not have ECS instances.
Change the minimum number of instances to one for the scaling group. After the minimum number is modified, a scale-out event is triggered because the number of instances in the scaling group does not meet the minimum requirement. Then, Auto Scaling automatically adds an ECS instance to the scaling group. However, the status of the ECS instance does not immediately become In Service because the scaling group has a lifecycle hook. Instead, its status becomes Pending.
During the timeout period of the lifecycle hook, you can log on to the ECS instance and install applications or perform custom operations.
- 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 scaling group and use one of the following methods to open the details page
of the scaling group:
- 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 page, click the Lifecycle Hook tab.
- In the upper-left corner of the Lifecycle Hook page, click Create Lifecycle Hook.
- Configure the parameters for the lifecycle hook.The following table describes the parameters of a lifecycle hook.
Parameter Description Name The name cannot be modified 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 (-). It must start with a letter or a digit. Applicable Scaling Activity Type When scaling activities of the specified type occur, the lifecycle hook is triggered to put corresponding ECS instances into the wait state. Valid values:
- Scale-in Event
- Scale-out Event
Timeout Period During the timeout period, corresponding ECS instances remains in the wait state. The value must be an integer from 30 to 21600. Unit: seconds. Execution Policy This parameter specifies the action to be taken after the lifecycle hook times out. Valid values:
- Continue: Auto Scaling continues to execute a scale-in or scale-out event.
- Reject: Auto Scaling releases the created ECS instances for a scale-out event or removes the ECS instances for a scale-in event.
Notification Method When the lifecycle hook is triggered, Auto Scaling sends a notification or performs an operation based on the specified notification method. Valid values:
- No Notification: This is the default value.
- MNS Topic: If you set the parameter to this value, you must select an MNS topic.
- MNS Queue: If you set the parameter to this value, you must select an MNS queue.
- OOS Template: If you set the parameter to this value, you must select an OOS template.
You can set a notification ID if you set the notification method to MNS Topic or MNS Queue. The notification ID is sent along with notifications to the MNS topic or MNS queue so that you can manage the notifications by ID.
- Click OK.