All Products
Search
Document Center

Auto Scaling:Automatically add ECS instances to or remove them from the whitelist of a MongoDB instance

Last Updated:Apr 16, 2026

Auto Scaling natively integrates with Server Load Balancer (SLB) and ApsaraDB RDS, but not with ApsaraDB for MongoDB. To keep your MongoDB instance's IP address whitelist in sync with scaling events, combine Auto Scaling lifecycle hooks with a CloudOps Orchestration Service (OOS) public template. When a scale-out event occurs, the lifecycle hook puts new ECS instances into a Pending Add state and triggers OOS to append their private IP addresses to the MongoDB whitelist before the instances go live.

This topic walks through the scale-out case using the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist public template. The same approach applies to scale-in: create a separate lifecycle hook for scale-in events and set modifyMode to remove IPs instead of appending them.

Prerequisites

Before you begin, ensure that you have:

  • A scaling group in the Enabled state

  • An ApsaraDB for MongoDB instance

  • A RAM role for OOS with the trusted entity set to Alibaba Cloud Service and the trusted service set to CloudOps Orchestration Service, and the RAM role must have the permissions to perform operations on the OOS template. For more information, see Use RAM to grant permissions to OOS.

This topic uses OOSServiceRole as the example RAM role name. Any RAM role configured with the required OOS trust relationship works.

How the automation works

  1. A scale-out event triggers the lifecycle hook, putting the new ECS instance into Pending Add state.

  2. Auto Scaling sends a notification to OOS with the parameters defined in NotificationMetadata.

  3. OOS runs the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist template, which retrieves the instance's private IP address and appends it to the MongoDB whitelist.

  4. After OOS completes the task, the lifecycle hook releases the instance, which transitions to the In Service state.

Lifecycle hooks take effect when scaling rules are executed, including scheduled and event-triggered tasks. They do not take effect when you manually add or remove instances from a scaling group.
Important

You must use NotificationMetadata (metadata for short) to specify parameters required to create OOS executions:

  • metadata must be in the JSON string format.

  • The keys in metadata must match the parameters in the OOS template.

  • For parameters that have no default settings in the OOS template, you must specify values for these parameters. For parameters that have default settings in the OOS template, you can specify whether to overwrite their default settings.

Step 1: Grant the RAM role permissions on OOS

The ACS-ESS-LifeCycleModifyMongoDBIPWhitelist template requires permissions to describe ECS instances, modify MongoDB security IPs, and complete lifecycle actions. Create a policy that grants these permissions and attach it to OOSServiceRole.

Create a policy

  1. Log on to the RAM console.

  2. In the left-side navigation pane, choose Permissions > Policies.

  3. Click Create Policy.

  4. On the Create Policy page, click the JSON tab, enter the following policy document, and then click OK.

    ParameterValue
    NameESSHookPolicyForMongoDBWhitelist
    Policy documentSee the JSON below
    {
        "Version": "1",
        "Statement": [
            {
                "Action": [
                    "ecs:DescribeInstances"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "dds:ModifySecurityIps"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "ess:CompleteLifecycleAction"
                ],
                "Resource": "*",
                "Effect": "Allow"
            }
        ]
    }

Attach the policy to OOSServiceRole

  1. In the left-side navigation pane, choose Identities > Roles.

  2. Find OOSServiceRole and click Grant Permission in the Actions column.

  3. In the Grant Permission panel, configure the following settings and click Grant permissions.

    ParameterValue
    Resource ScopeAccount
    PolicyESSHookPolicyForMongoDBWhitelist

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

Create the lifecycle hook

  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 the region where Auto Scaling is activated.

  4. Find your scaling group and click its ID in the Scaling Group Name/ID column, or click Details in the Actions column.

  5. On the scaling group details page, click the Lifecycle Hook tab.

  6. Click Create Lifecycle Hook.

  7. Configure the following parameters and click OK.

    ParameterValue
    NameESSHookForAddMongoDBWhitelist
    Scaling ActivityScale-out Event
    Timeout PeriodSet this to a value long enough for OOS to complete the whitelist update. This example uses 300 seconds. If the timeout expires before OOS finishes, the lifecycle action defaults to Continue and the instance goes live without the IP being added.
    Default Execution PolicyContinue
    Send Notification When Lifecycle Hook Takes EffectSelect OOS Template > Public Templates > LifeCycleModifyMongoDBIPWhitelist
    dbInstanceIdThe ID of your MongoDB instance (this example uses a replica set instance)
    modifyModeAppend
    OOSAssumeRoleOOSServiceRole
If your scaling group already contains ECS instances, manually add their private IP addresses to the MongoDB whitelist before enabling this automation. If those IPs are absent from the whitelist when a scale-in event runs, the removal step will fail.

Trigger a scale-out event

In this example, trigger the scale-out by manually executing a scaling rule. Production environments typically use scheduled or event-triggered tasks.

  1. On the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab.

  2. On the Scaling Rules tab, click Create Scaling Rule.

  3. Configure the following parameters and click OK.

    ParameterValue
    Rule NameAdd1
    Rule TypeSimple Scaling Rule
    OperationAdd 1 Instances
  4. Find the Add1 rule and click Execute in the Actions column.

  5. In the Execute Scaling Rule message, click OK.

Auto Scaling adds one ECS instance to the scaling group. The instance enters Pending Add state because the ESSHookForAddMongoDBWhitelist lifecycle hook is in effect. During the timeout period, OOS runs the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist template to update the MongoDB whitelist.

Step 3: Verify the MongoDB whitelist

  1. Log on to the MongoDB console.

  2. In the left-side navigation pane, click Replica Set Instances.

  3. Find your MongoDB instance and click its ID in the Instance ID/Name column.

  4. In the left-side navigation pane, choose Data Security > Whitelist Settings.

If the private IP address of the new ECS instance appears in the whitelist, the automation is working correctly. If the IP is missing, check the OOS execution log as described in the next step.

(Optional) Step 4: View the OOS execution log

  1. Log on to the OOS console.

  2. In the left-side navigation pane, choose Automated Tasks > Task Execution Management.

  3. Find the execution by timestamp and click Details in the Actions column.

  4. On the execution details page, check the Basic Information section for the execution ID and status. In the Execution Steps and Results section, click individual task nodes to see step-level output.

For more information, see View the details of an execution.

FAQ

If an Operation and Maintenance (O&M) task fails, find the cause based on the error message in the execution result. For more information, see FAQ.

Common error messages are as follows:

Error message

Cause

Solution

Forbidden.Unauthorized message: A required authorization for the specified action is not supplied.

Auto Scaling is not authorized to perform the specified action.

Verify that the required permissions are granted to the OOSServiceRole RAM role.

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

The RAM user or RAM role does not have the permissions to operate on the corresponding resource.

Verify that the OOSServiceRole RAM role has the required permissions. For example, you can grant the sample permissions for the OOS service to the RAM role. You must add operation permissions to the RAM role to ensure that the OOS service can manage the resources specified in the OOS template.

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

The ongoing lifecycle action has expired or has been aborted.

Evaluate the timeout period of the lifecycle hook to ensure that the O&M tasks defined in the OOS template can be completed within the timeout period.

Troubleshooting

IP address not added to the whitelist

Symptom: The scale-out event completes, but the ECS instance's private IP address does not appear in the MongoDB whitelist.

Steps:

  1. Open the OOS execution log (see Step 4) and check whether the execution status is Failed.

  2. If the execution failed, look for one of the following error messages and apply the corresponding fix:

    Error messageCauseFix
    Forbidden.Unauthorized: A required authorization for the specified action is not suppliedOOSServiceRole is missing the required permissionsAttach the ESSHookPolicyForMongoDBWhitelist policy to OOSServiceRole as described in Step 1
    Forbidden.RAM: User not authorized to operate on the specified resource, or this API doesn't support RAMOOSServiceRole is missing the required permissionsAttach the ESSHookPolicyForMongoDBWhitelist policy to OOSServiceRole as described in Step 1
  3. If the execution log shows no record of the OOS task being triggered, the lifecycle hook may not be configured correctly. Confirm that the hook targets Scale-out Event and that the OOS template is set to ACS-ESS-LifeCycleModifyMongoDBIPWhitelist.

Lifecycle action times out before OOS finishes

Symptom: The instance transitions out of Pending Add state before the IP is added to the whitelist, or the OOS execution log shows a task that was interrupted.

Steps:

  1. Open the OOS execution log and check whether the execution ended with a LifecycleHookIdAndLifecycleActionToken.Invalid error:

    LifecycleHookIdAndLifecycleActionToken.Invalid: The specified lifecycleActionToken
    and lifecycleActionId you provided does not match any in process lifecycle action.
  2. If this error appears, the lifecycle hook's Timeout Period is too short. Increase Timeout Period on the lifecycle hook to give OOS enough time to complete the whitelist update, then re-execute the scaling rule to test again.