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
A scale-out event triggers the lifecycle hook, putting the new ECS instance into Pending Add state.
Auto Scaling sends a notification to OOS with the parameters defined in
NotificationMetadata.OOS runs the ACS-ESS-LifeCycleModifyMongoDBIPWhitelist template, which retrieves the instance's private IP address and appends it to the MongoDB whitelist.
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.
You must use NotificationMetadata (metadata for short) to specify parameters required to create OOS executions:
metadatamust be in the JSON string format.The keys in
metadatamust 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
Log on to the RAM console.
In the left-side navigation pane, choose Permissions > Policies.
Click Create Policy.
On the Create Policy page, click the JSON tab, enter the following policy document, and then click OK.
Parameter Value Name ESSHookPolicyForMongoDBWhitelist Policy document See 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
In the left-side navigation pane, choose Identities > Roles.
Find OOSServiceRole and click Grant Permission in the Actions column.
In the Grant Permission panel, configure the following settings and click Grant permissions.
Parameter Value Resource Scope Account Policy ESSHookPolicyForMongoDBWhitelist
Step 2: Create a lifecycle hook and trigger a scale-out event
Create the lifecycle hook
Log on to the Auto Scaling console.
In the left-side navigation pane, click Scaling Groups.
In the top navigation bar, select the region where Auto Scaling is activated.
Find your scaling group and click its ID in the Scaling Group Name/ID column, or click Details in the Actions column.
On the scaling group details page, click the Lifecycle Hook tab.
Click Create Lifecycle Hook.
Configure the following parameters and click OK.
Parameter Value Name ESSHookForAddMongoDBWhitelist Scaling Activity Scale-out Event Timeout Period Set 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 Policy Continue Send Notification When Lifecycle Hook Takes Effect Select OOS Template > Public Templates > LifeCycleModifyMongoDBIPWhitelist dbInstanceId The ID of your MongoDB instance (this example uses a replica set instance) modifyMode Append OOSAssumeRole OOSServiceRole
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.
On the scaling group details page, click the Scaling Rules and Event-triggered Tasks tab.
On the Scaling Rules tab, click Create Scaling Rule.
Configure the following parameters and click OK.
Parameter Value Rule Name Add1 Rule Type Simple Scaling Rule Operation Add 1 Instances Find the Add1 rule and click Execute in the Actions column.
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
Log on to the MongoDB console.
In the left-side navigation pane, click Replica Set Instances.
Find your MongoDB instance and click its ID in the Instance ID/Name column.
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
Log on to the OOS console.
In the left-side navigation pane, choose Automated Tasks > Task Execution Management.
Find the execution by timestamp and click Details in the Actions column.
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:
Open the OOS execution log (see Step 4) and check whether the execution status is Failed.
If the execution failed, look for one of the following error messages and apply the corresponding fix:
Error message Cause Fix Forbidden.Unauthorized: A required authorization for the specified action is not suppliedOOSServiceRole is missing the required permissions Attach the ESSHookPolicyForMongoDBWhitelistpolicy to OOSServiceRole as described in Step 1Forbidden.RAM: User not authorized to operate on the specified resource, or this API doesn't support RAMOOSServiceRole is missing the required permissions Attach the ESSHookPolicyForMongoDBWhitelistpolicy to OOSServiceRole as described in Step 1If 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:
Open the OOS execution log and check whether the execution ended with a
LifecycleHookIdAndLifecycleActionToken.Invaliderror:LifecycleHookIdAndLifecycleActionToken.Invalid: The specified lifecycleActionToken and lifecycleActionId you provided does not match any in process lifecycle action.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.