This topic describes how to use a lifecycle hook to put Elastic Compute Service (ECS) instances into the wait state and then use an Operation Orchestration Service (OOS) template to automatically add or remove the instances to or from a whitelist of an ApsaraDB for Redis instance.

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.
  • A scaling group is created and enabled.
  • An ApsaraDB for Redis instance is created.
  • A RAM role is created for OOS. For more information, see Grant RAM permissions for OOS.
    Note The OOSServiceRole RAM role is used in this example. You can also use other custom RAM roles, but you must make sure that the used RAM role has the permissions required to execute OOS templates.

Background information

A scaling group can be associated with Server Load Balancer (SLB) or ApsaraDB for RDS instances, but cannot be associated with ApsaraDB for Redis instances. If your business data is stored in an ApsaraDB for Redis instance, you must manually add or remove ECS instances to or from a whitelist of the ApsaraDB for Redis instance. This is time-consuming and inefficient. You can use lifecycle hooks and OOS templates to automatically add or remove ECS instances to or from a whitelist of the ApsaraDB for Redis instance.

Procedure

In the following example, the ACS-ESS-LifeCycleModifyRedisIPWhitelist public template of OOS is used to demonstrate how to add ECS instances to a whitelist of an ApsaraDB for Redis instance during scale-out events. Perform the following steps to add ECS instances to a whitelist:
Note If you want to remove ECS instances from a whitelist of an ApsaraDB for Redis instance during scale-in events, you can create lifecycle hooks that are applicable to scale-in events and then trigger the scale-in events.

Step 1: Grant OOS permissions to the RAM role

You must be granted the permissions to execute OOS templates. Resources of ECS, Auto Scaling, and ApsaraDB for Redis are involved when O&M operations specified in the ACS-ESS-LifeCycleModifyRedisIPWhitelist public template are performed.

  1. Log on to the RAM console.
  2. Create a policy.
    1. In the left-side navigation pane, choose Permissions > Policies.
    2. On the Policies page, click Create Policy.
    3. On the Create Custom Policy page, configure parameters for the policy and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Policy Name Enter ESSHookPolicyForRedisWhitelist.
      Configuration Mode Select Script.
      Policy Document Enter the following content:
      {
          "Version": "1",
          "Statement": [
              {
                  "Action": [
                      "ecs:DescribeInstances"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              },
              {
                  "Action": [
                      "kvstore:ModifySecurityIps"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              },
              {
                  "Action": [
                      "ess:CompleteLifecycleAction"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              }
          ]
      }
  3. Attach the policy to the OOSServiceRole RAM role.
    1. In the left-side navigation pane, click RAM Roles.
    2. Find the OOSServiceRole RAM role and click Add Permissions in the Actions column.
      Attach the policy to the OOSServiceRole RAM role assumed by OOS to complete the authorization.
    3. In the Add Permissions panel, configure the parameters and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Authorized Scope Select Alibaba Cloud Account.
      Select Policy Select Custom Policy and then the ESSHookPolicyForRedisWhitelist policy.

Step 2: Create a lifecycle hook for scale-out events and trigger a scale-out event

If you want ECS instances to be automatically added to a whitelist of an ApsaraDB for Redis instance when scale-out events are triggered, you can set the notification method to OOS Template and configure related parameters when you create lifecycle hooks.

  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 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.
    • In the Actions column corresponding to the scaling group, click Details.
  5. Create a lifecycle hook for scale-out events.
    1. In the upper part of the page, click the Lifecycle Hook tab.
    2. Click Create Lifecycle Hook.
    3. Configure parameters for the lifecycle hook and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Name Enter ESSHookForAddRedisWhitelist.
      Applicable Scaling Activity Type Select Scale-out Event.
      Timeout Period Enter an appropriate value, such as 300.
      Note The timeout period is the period of time during which to perform customized operations. If the period is short, the operations may fail to be properly performed. Estimate the time required to perform the operations and set an appropriate timeout period.
      Execution Policy Select Continue.
      Notification Method Configure the following settings:
      • Notification method: Select OOS Template.
      • OOS template type: Select Public Templates.
      • Public template: Select ACS-ESS-LifeCycleModifyRedisIPWhitelist from the drop-down list.
      The parameters for the ACS-ESS-LifeCycleModifyRedisIPWhitelist public template:
      • dbInstanceId: Enter the ID of the ApsaraDB for Redis instance.
      • modifyMode: Select Append. This value applies to scale-out events and allows ECS instances to be added to a whitelist of the ApsaraDB for Redis instance.
      • Permissions: Select OOSServiceRole. In Step 1, the OOSServiceRole RAM role is granted permissions on resources of ECS, Auto Scaling, and ApsaraDB for Redis. OOS owns the preceding permissions after it assumes the RAM role.
  6. Trigger a scale-out event.
    In this example, a scale-out event is manually triggered by executing a scaling rule. You can also trigger scale-out events by using scheduled or event-triggered tasks.
    Note Lifecycle hooks take effect when scaling activities are manually triggered by executing scaling rules. Lifecycle hooks do not take effect when you manually add or remove ECS instances to or from a scaling group.
    1. In the upper part of the page, click the Scaling Rules tab.
    2. Click Create Scaling Rule.
    3. In the Create Scaling Rule dialog box, configure the parameters and click OK.
      The following table describes the parameters used in this example. Use the default values for parameters that are not mentioned in the table.
      Parameter Description
      Rule Name Enter Add1.
      Rule Type Select Simple Scaling Rule.
      Operation Set this parameter to Add 1 Instances.
    4. On the Scaling Rules page, find the created Add1 scaling rule and click Execute in the Actions column.
    5. Click OK.
    After the scaling rule is executed, an ECS instance is automatically created. The ESSHookForAddRedisWhitelist lifecycle hook in the scaling group puts the ECS instance into the wait state. Auto Scaling automatically notifies OOS to perform the O&M operations specified in the ACS-ESS-LifeCycleModifyRedisIPWhitelist public template on the ECS instance.

Step 3: View the whitelist of the ApsaraDB for Redis instance

  1. Log on to the ApsaraDB for Redis console.
  2. In the left-side navigation pane, click Instances.
  3. Find the ApsaraDB for Redis instance and click its ID in the Instance ID/Name column.
  4. In the left-side navigation pane, click Whitelist Settings.
    The following figure shows that the private IP address of the ECS instance is added to the whitelist of the ApsaraDB for Redis instance as specified in the ACS-ESS-LifeCycleModifyRedisIPWhitelist public template. Whitelist of the ApsaraDB for Redis instance

    If the ECS instance is created but its private IP address is not added to the whitelist of the ApsaraDB for Redis instance, log on to the OOS console to view the execution result of O&M tasks. For more information, see Step 4: (Optional) View the execution status of the OOS template.

Step 4: (Optional) View the execution status of the OOS template

  1. Log on to the OOS console.
  2. In the left-side navigation pane, click Executions.
  3. Find the execution by time and click Details in the Actions column.
  4. On the page that appears, click the Advanced View tab.
    The execution status is displayed on the Execution Result tab. Success
    If the execution fails, the error message is also displayed on the Execution Result tab. Failed

FAQ

If an O&M task fails to be executed, find the cause based on the error message in the execution result. The following section describes the common error messages and their solutions:
  • Error message: Forbidden.Unauthorized message: A required authorization for the specified action is not supplied.

    Solution: Check whether you have granted the required permissions to the OOSServiceRole RAM role, such as the sample permissions in Step 1. You must grant required permissions to the OOSServiceRole RAM role to make sure that OOS can manage the resources involved in OOS templates.

  • Error message: Forbidden.RAM message: User not authorized to operate on the specified resource, or this API doesn't support RAM.

    Solution: Check whether you have granted the required permissions to the OOSServiceRole RAM role, such as the sample permissions in Step 1. You must grant required permissions to the OOSServiceRole RAM role to make sure that OOS can manage the resources involved in OOS templates.

  • Error message: LifecycleHookIdAndLifecycleActionToken.Invalid message: The specified lifecycleActionToken and lifecycleActionId you provided does not match any in process lifecycle action.

    Solution: Estimate the timeout period of the lifecycle hook to make sure that the O&M task specified in the OOS template can be complete within the timeout period.