This topic describes how to use the lifecycle hook feature in combination with a CloudOps Orchestration Service (OOS) template to put Elastic Compute Service (ECS) instances into the Pending state and apply an automatic snapshot policy to the ECS instances.
Prerequisites
A scaling group is created. The scaling group is in the Enabled state. For information about how to create a scaling group, see Manage scaling groups.
An automatic snapshot policy is created. For information about how to create an automatic snapshot policy, see Create an automatic snapshot policy.
A RAM role is created for OOS. The trusted entity of the RAM role must be Alibaba Cloud Service, the trusted service must be CloudOps Orchestration Service, and the RAM role must have the permissions to perform operations on the OOS template. For more information, see Grant RAM permissions to OOS.
NoteIn this topic, the OOSServiceRole RAM role is used as an example. You can also use other roles.
Background information
Automatic snapshot policies help you create snapshots for ECS instances on a regular basis. This enhances data security and fault tolerance. You cannot specify an automatic snapshot policy in a scaling configuration when you create the scaling configuration. However, you can use the lifecycle hook feature of Auto Scaling in combination with an OOS template to automatically apply an automatic snapshot policy to a group of ECS instances. This is more efficient than manually applying an automatic snapshot policy to ECS instances.
Automatic snapshot policies enable the scheduled creation of snapshots at the preconfigured points in time to safeguard data integrity of system disks and data disks. For more information, see Overview.
Procedure
In this topic, the ACS-ESS-LifeCycleApplyAutoSnapshotPolicy
public OOS template is used as an example to show how to automatically apply an automatic snapshot policy to a group of ECS instances during a scale-out. You must perform the following steps:
Step 1: Grant a RAM role the permissions on OOS
You must have the permissions to execute OOS templates. The ACS-ESS-LifeCycleApplyAutoSnapshotPolicy
public OOS template defines the ECS and Auto Scaling resources required to perform O&M operations.
Log on to the RAM console.
Create a policy.
In the left-side navigation pane, choose .
Click Create Policy.
On the Create Policy page, click the JSON tab, enter the policy content, and then click Next to edit policy information.
Sample policy:
{ "Version": "1", "Statement": [ { "Action": [ "ecs:DescribeDisks", "ecs:ApplyAutoSnapshotPolicy", "ecs:DescribeInstances" ], "Resource": "*", "Effect": "Allow" }, { "Action": [ "ess:CompleteLifecycleAction" ], "Resource": "*", "Effect": "Allow" } ] }
In the Basic Information section, enter a policy name and check whether the policy content is valid.
In this example, set the Name parameter to
ESSHookPolicyForApplyAutoSnapshotPolicy
.NoteIf you want to modify the policy content, click Back to edit policy document.
Click OK.
Attach the policy to the OOSServiceRole RAM role.
In the left-side navigation pane, choose .
Find OOSServiceRole and click Grant Permission in the Actions column.
Add the required permissions for the OOSServiceRole RAM role that is assumed by OOS to complete the authorization.
In the Grant Permission panel, select the policy that you want to assign and click OK.
The following table describes the parameters used in this example. For parameters that are not described in the table, use the default settings.
Parameter
Description
Authorized Scope
Select Alibaba Cloud Account.
Select Policy
Select the
ESSHookPolicyForApplyAutoSnapshotPolicy
policy.After the authorization is complete, click OK.
Step 2: Create a lifecycle hook of the scale-out type and trigger a scale-out
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 a scaling group and use one of the following methods to go to the scaling group details page:
Click the ID of the scaling group in the Scaling Group Name/ID column.
Click Details in the Actions column.
Create a lifecycle hook of the scale-out type.
In the upper part of the scaling group details page, click the Lifecycle Hook tab.
Click Create Lifecycle Hook.
Configure parameters based on your business requirements and click OK.
The following table describes the parameters that are used in this example. For parameters that are not described in the table, use the default settings.
Parameter
Description
Name
Enter ESSHookForApplyAutoSnapshotPolicy.
Scaling Activity
Select Scale-out Event.
Timeout Period
Configure the Timeout Period parameter based on your business requirements. In this example, set the Timeout Period parameter to 300.
NoteThe timeout period is the period of time during which you can perform custom operations on instances. If the timeout period is shorter than the period of time that is required to perform custom operations, the operations may fail. We recommend that you estimate the period of time that is required to perform custom operations on instances and configure the Timeout Period parameter based on your estimations.
Default Execution Policy
Select Continue.
Send Notification When Lifecycle Hook Takes Effect
In this example, perform the following operations:
Select OOS Template.
Select Public Templates.
Select
ACS-ESS-LifeCycleApplyAutoSnapshotPolicy
.
Configure the following parameters in the
ACS-ESS-LifeCycleApplyAutoSnapshotPolicy
public OOS template:AutoSnapshotPolicyId: Enter the ID of the automatic snapshot policy.
OOSAssumeRole: Select OOSServiceRole. In Step 1, permissions on ECS and Auto Scaling resources are granted to the OOSServiceRole RAM role. OOS can obtain these permissions after it assumes the RAM role.
Trigger a scale-out.
In this example, a scale-out is manually triggered by executing a scaling rule. You can also trigger scale-outs by using scheduled or event-triggered tasks.
NoteIf scaling activities are triggered when you manually execute scaling rules, lifecycle hooks take effect. However, lifecycle hooks do not take effect when you manually add or remove ECS instances to or from a scaling group.
In the upper part of the page that appears, click the Scaling Rules and Event-triggered Tasks tab.
On the Scaling Rules tab, click Create Scaling Rule.
In the Create Scaling Rule dialog box, configure parameters based on your business requirements and click OK.
The following table describes the parameters that are used in this example. For parameters that are not described in the table, use the default settings.
Parameter
Description
Rule Name
Enter Add1.
Rule Type
Select Simple Scaling Rule.
Operation
Set the value to Add 1 Instances.
On the Scaling Rules tab, find the Add1 scaling rule and click Execute in the Actions column.
In the Execute Scaling Rule message, click OK.
After Add1 is executed, the system attempts to add one ECS instance to the scaling group. Due to the
ESSHookForApplyAutoSnapshotPolicy
lifecycle hook, the ECS instance enters the Pending Add state before it is added to the scaling group. During this period, Auto Scaling notifies OOS to execute theACS-ESS-LifeCycleApplyAutoSnapshotPolicy
template and complete the defined O&M operations.Check whether the automatically created ECS instance meets your expectations.
In the upper part of the scaling group details page, click the Instances tab.
Find the automatically created ECS instance and click its ID in the ECS Instance ID/Name column.
On the Instance Details page, click the Cloud Disk tab.
Find the desired cloud disk and click Configure Automatic Snapshot Policy in the Actions column.
In the following figure, the Automatic Snapshot Policy switch is turned on. The applied snapshot policy is the policy specified in the
ACS-ESS-LifeCycleApplyAutoSnapshotPolicy
OOS public template when you created the lifecycle hook. This proves that the automatically created ECS instance meets your expectations.If the ECS instance is created but has no automatic snapshot policy, go to the OOS console to check the execution of the O&M operations defined in the public template.
(Optional) Step 3: View the execution status of an OOS template
Log on to the OOS console.
In the left-side navigation pane, choose .
Find the execution task by time and click Details in the Actions column.
On the execution details page that appears, view the related information.
For example, you can view the execution ID and status in the Basic Information section. You can also click a task node in the Execution Steps and Results section to view the execution details. For more information, see View the details of an execution.
NoteIf the execution fails, an error message is displayed on the execution details page.
FAQ
If you fail to execute an O&M task, troubleshoot the issue based on the error message in the execution result. The following section describes the common error messages and solutions:
Error message: Forbidden.Unauthorized message: A required authorization for the specified action is not supplied.
Solution: Check whether the required permissions, such as the sample permissions in Step 1, are granted to the OOSServiceRole RAM role. Before OOS can manage the resources that are declared in the OOS template, you must grant the required permissions to the RAM role.
Error message: Forbidden.RAM message: User not authorized to operate on the specified resource, or this API doesn't support RAM.
Solution: Check whether the required permissions, such as the sample permissions in Step 1, are granted to the OOSServiceRole RAM role. Before OOS can manage the resources that are declared in the OOS template, you must grant the required permissions to the RAM role.
Error message: LifecycleHookIdAndLifecycleActionToken.Invalid message: The specified lifecycleActionToken and lifecycleActionId you provided does not match any in process lifecycle action.
Solution: Check whether the timeout period of the lifecycle hook is sufficient for the O&M task specified in the OOS template to complete.
References
You can also use the lifecycle hook feature of Auto Scaling in combination with an OOS template to enable automatic execution of scripts on ECS instances during a scale-in. For more information, see Automatically execute scripts on ECS instances.