This topic describes how to use a lifecycle hook to put 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 the whitelist of an ApsaraDB for MongoDB (MongoDB) instance.

Prerequisites

  • Before you perform the operations provided in the tutorial, you must have registered an Alibaba Cloud account. To create an Alibaba Cloud account, click Create a new Alibaba Cloud account.
  • A scaling group is created and enabled.
  • A MongoDB instance is created.
  • A RAM role is created for OOS. For more information, see Grant RAM permission 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 (RDS) instances, but cannot be associated with MongoDB instances. If your business data is stored on a MongoDB instance, you must manually add or remove your ECS instances to or from the whitelist of the MongoDB 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 the whitelist of a MongoDB instance.

Procedure

The following example uses the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist public template to demonstrate how to add ECS instances to the whitelist of a MongoDB instance during scale-out events. Perform the following steps:
Note If you want to remove ECS instances from the whitelist of a MongoDB 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 user

You must be granted the permissions required to execute OOS templates. Resources of ECS, Auto Scaling, and MongoDB are involved when the O&M operations specified in the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist 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. In the upper-left corner of 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 ESSHookPolicyForMongoDBWhitelist.
      Configuration Mode Select Script.
      Policy Document Enter the following content:
      {
          "Version": "1",
          "Statement": [
              {
                  "Action": [
                      "ecs:DescribeInstances"
                  ],
                  "Resource": "*",
                  "Effect": "Allow"
              },
              {
                  "Action": [
                      "dds: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
      Authorization Select Alibaba Cloud account all resources.
      Select Policy Select Custom Policy and then the ESSHookPolicyForMongoDBWhitelist policy.

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

  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.
    • Click Details in the Actions column.
  5. Create a lifecycle hook for scale-out events.
    1. In the left-side navigation pane, click Lifecycle Hooks.
    2. In the upper-left corner of the Lifecycle Hooks page, 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 ESSHookForAddMongoDBWhitelist.
      Applicable Scaling Activity Type Select Scale-out Event.
      Timeout Period Enter a proper value, such as 300.
      Note The timeout period is the period of time during which customized operations are performed. If the period is short, the operations may fail to be performed. Evaluate and set a proper 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 LifeCycleModifyMongoDBIPWhitelist from the drop-down list.
      The following section describes the parameters for ACS-ESS-LifeCycleModifyMongoDBIPWhitelist:
      • dbInstanceId: Enter the MongoDB instance ID. A MongoDB replica set instance is used in this example.
      • modifyMode: Select Append. This value applies to scale-out events and allows ECS instances to be added to the whitelist of the MongoDB instance.
      • Permissions: Select OOSServiceRole. In Step 1, the OOSServiceRole RAM role is granted permissions of ECS, Auto Scaling, and MongoDB. 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 left-side navigation pane, click Scaling Rules.
    2. In the upper-left corner of the Scaling Rules page, 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. In the Execute Scaling Rule message, click OK.
    After the scaling rule is executed, an ECS instance is automatically created. The ESSHookForAddMongoDBWhitelist 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-LifeCycleModifyMongoDBIPWhitelist template on the ECS instance.

Step 3: View the whitelist of the MongoDB instance

  1. Log on to the MongoDB console.
  2. In the left-side navigation pane, click Replica Set Instances.
  3. Find the MongoDB 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 MongoDB instance as specified in the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist template.mongodb-whitelist

    If the ECS instance is created, but its private IP address is not added to the whitelist of the MongoDB instance, go to the OOS console to view the execution result of O&M tasks. For more information, see Step 4 (optional): View the OOS execution status.

Step 4 (optional): View the OOS execution status

  1. Log on to the OOS console.
  2. In the left-side navigation pane, click Executions.
  3. Find the execution task 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 that are 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 that are 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 that is specified in the OOS template can be complete within the timeout period.