You can use lifecycle hooks to put ECS instances that are being added or removed in a scaling activity 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 instance 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 the instances 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.
- The scaling group will not execute other scaling activities.
- You can delete the corresponding lifecycle hooks to resume the scaling activity.
Assume that you have created a scaling group named sg-yk****. The minimum number of instances in the scaling group 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 modification, a scale-out event is triggered because the number of instances in the scaling group does not meet the minimum requirement. Auto Scaling then automatically adds an ECS instance to the scaling group. However, the status of the ECS instance will not become InService immediately 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 target 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 Manage in the Actions column.
- In the left-side navigation pane, click Lifecycle Hooks.
- In the upper-left corner of the Lifecycle Hooks page, click Create Lifecycle Hook.
- Set parameters for the lifecycle hook.
- Set the Name parameter.
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.
- Set the Applicable Scaling Activity Type parameter.
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
- Set the Timeout Period parameter.
During the timeout period, corresponding ECS instances are in the wait state. The value must be an integer from 30 to 21600, in seconds.
- Set the Execution Policy parameter.
This parameter specifies the action to be taken after the lifecycle hook time outs. Valid values:
- Continue: The scaling group continues to execute a scale-in or scale-out event.
- Reject: The scaling group releases the created ECS instances for a scale-out event or retains the ECS instances to be removed for a scale-in event.
- Optional. Set the Notification Method parameter.
When the lifecycle hook is triggered, Auto Scaling sends a notification or performs an operation based on the specified notification method. Valid values:
- 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.
- Set the Name parameter.
- Click Create.