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.

Background information

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 create lifecycle hooks for a scaling group. When a scaling group with lifecycle hooks executes scaling activities, the instances that are being added to or removed from the scaling group will be put into the wait state. You can create only a limited number of lifecycle hooks for a scaling group. For more information, see Limits.
Note Lifecycle hooks take effect only when a scaling rule is executed to add or remove ECS instances to or from a scaling group. Lifecycle hooks do not take effect when ECS instances are manually added to or removed from a scaling group.
When ECS instances are put into the wait state in a scaling group:
  • 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.
You can configure a notification method for a lifecycle hook. When the lifecycle hook is triggered, a notification can be sent to the specified Message Service (MNS) topic or queue, or an operation can be performed based on the specified Operation Orchestration Service (OOS) template. To configure a notification method, you must create an MNS topic, MNS queue, or OOS custom template in advance.
Note If you use an OOS public template, you do not need to create it in advance. For more information, see Public templates.

Example:

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.

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 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.
  5. In the left-side navigation pane, click Lifecycle Hooks.
  6. In the upper-left corner of the Lifecycle Hooks page, click Create Lifecycle Hook.
  7. Set parameters for the lifecycle hook.
    1. 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.
    2. 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
    3. 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.
    4. 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.
    5. 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.

  8. Click Create.