To ensure service accessibility, you can automatically attach an elastic ip address (EIP) to each new ECS instance during a scale-out event. Conversely, during a scale-in event, you can automatically release the EIPs from instances that are being removed. This is useful during off-peak periods or when public access is no longer required. This practice optimizes resource use and reduces costs by eliminating charges for idle resources.
An EIP is a public IP address that you can purchase and manage independently. You can attach an EIP to or release it from an ECS instance as needed. For more information, see What is an elastic ip address.
Prerequisites
-
Create a scaling group and ensure it is in the Enable state. For more information, see Create a scaling group.
-
Grant CloudOps Orchestration Service (OOS) permissions to access other cloud services. For more information, see Set a RAM role and grant permissions to OOS.
-
Create a RAM role.
Set the trusted service for the RAM role to OOS. This topic uses
OOSServiceRoleas the RAM role name. -
Attach policies to the RAM role.
When OOS runs a task, it requires access to ECS, Auto Scaling, and EIP resources. Therefore, attach the following system policies to the
OOSServiceRolerole:-
AliyunECSFullAccess
-
AliyunESSFullAccess
-
AliyunEIPFullAccess
-
-
Automatically attach EIPs during scale-out
-
Create a lifecycle hook.
The following table describes the key parameters. You can keep the default values for other parameters. For more information, see Create a lifecycle hook.
Parameter
Description
Name
Enter
ESSHookForAttachEip.Scaling Activity
Select Scale-out Event.
Timeout Period
Enter an appropriate timeout period, such as 300 seconds.
ImportantThis parameter specifies the maximum time for the custom action to complete. If the timeout period is too short, the action may fail. Set a timeout period that is long enough for your custom action to complete.
Send Notification When Lifecycle Hook Takes Effect
Select OOS Template and configure the following parameters:
-
For OOS Template Type, select Public Templates.
-
Select the public OOS template
ACS-ESS-LifeCycleAllocateEipAddressAndAttachToInstance. This template automatically attaches an EIP to an ECS instance during a scale-out event. -
Click Parameter Settings next to the
ACS-ESS-LifeCycleAllocateEipAddressAndAttachToInstancetemplate.The following are examples of key parameters. You can keep the default values for other parameters.
-
Billing method for EIP: PayByBandwidth specifies the pay-by-bandwidth billing method. PayByTraffic specifies the pay-by-traffic billing method. This topic uses PayByBandwidth as an example.
-
Peak bandwidth for EIP: This example uses a value of 5, which sets the EIP's peak bandwidth to 5 Mbit/s.
-
EIP tags: Add tags to the EIPs to simplify management.
-
The RAM role assumed by OOS. : Select
OOSServiceRole.This role grants OOS the necessary permissions to manage ECS, Auto Scaling, and EIP resources.
-
-
-
Trigger a scale-out event.
This example triggers a scale-out event by manually executing a scaling rule. You can also use scheduled or event-triggered tasks. For more information, see Execute a scaling rule.
NoteWhen you manually execute a scaling rule to trigger a scale-out event, the lifecycle hook takes effect. However, if you manually add an existing ECS instance, the lifecycle hook is not triggered.
After a scale-out event is successfully triggered, the scaling group automatically creates an ECS instance. Because the
ESSHookForAttachEiplifecycle hook is created in the scaling group, the lifecycle hook places the ECS instance into a pending state and notifies OOS to run a task to create and attach an EIP to the instance.If the scaling activity status becomes Failed and an error like the one below is reported, check the task execution details in the OOS console. For more information, see (Optional) View OOS execution details.
The scaling activity details show the following error:
Fail to create Instances into scaling group(code:"LifecycleActionResult.Abandon", msg:"Abandon lifecycleActionResult parameter caused the instance to rollback.") -
Verify that an EIP is automatically attached to the scaled-out ECS instance.
On the tab of the scaling group, click the instance ID of the automatically created ECS instance to go to the instance details page. For more information, see Query the instance list of a scaling group.
In the basic information section of the ECS instance, check the EIP parameter. An IP address in this field indicates that an EIP was successfully attached.
NoteIf the ECS instance is created but no EIP is attached, check the execution details of the task in the OOS console.
-
(Optional) View OOS execution details.
The task execution details page displays information such as the execution ID and status in the Basic Information area, and provides execution details and logs in the Execution Steps and Results area. For more information, see View task execution details.
ImportantIf the execution fails, a relevant error message is displayed on the execution details page.
Automatically release EIPs during scale-in
-
Create a lifecycle hook.
The following table describes the key parameters. You can keep the default values for other parameters. For more information, see Create a lifecycle hook.
Parameter
Description
Name
Enter
ESSHookForReleaseEip.Scaling Activity
Select Scale-in Event.
Timeout Period
Enter an appropriate timeout period, such as 300 seconds.
ImportantThis parameter specifies the maximum time for the custom action to complete. If the timeout period is too short, the action may fail. Set a timeout period that is long enough for your custom action to complete.
Send Notification When Lifecycle Hook Takes Effect
Select OOS Template and configure the following parameters:
-
For OOS Template Type, select Public Templates.
-
Select the public OOS template
ACS-ESS-LifeCycleReleaseEipAddressFromInstance. This template automatically releases the EIP from an ECS instance during a scale-in event. -
Click Parameter Settings next to the
ACS-ESS-LifeCycleReleaseEipAddressFromInstancetemplate.Set The RAM role assumed by OOS. to
OOSServiceRoleand keep the default values for other parameters. This role grants OOS the necessary permissions to manage ECS, Auto Scaling, and EIP resources.
-
-
Trigger a scale-in event.
This example triggers a scale-in event by manually executing a scaling rule. You can also use scheduled or event-triggered tasks. For more information, see Execute a scaling rule.
NoteWhen you manually execute a scaling rule to trigger a scale-in event, the lifecycle hook takes effect. However, if you manually remove an existing ECS instance, the lifecycle hook is not triggered.
After a scale-in event is successfully triggered, the scaling group automatically removes an ECS instance. Because the
ESSHookForReleaseEiplifecycle hook is created in the scaling group, the lifecycle hook places the ECS instance into a pending state and notifies OOS to run a task to release the EIP from the instance. -
Verify that the EIP is automatically released from the scaled-in ECS instance.
Check the elastic ip addresses list for the EIP. The EIP has been successfully released if it no longer appears in the list.
NoteIf the ECS instance is deleted but the EIP is not released, check the execution details of the task in the OOS console.
-
(Optional) View OOS execution details.
The task execution details page displays information such as the execution ID and status in the Basic Information area, and provides execution details and logs in the Execution Steps and Results area. For more information, see View task execution details.
ImportantIf the execution fails, a relevant 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. For more information, see FAQ.
The following table describes the common error message.
Error message | Cause | Solution |
Forbidden.Unauthorized message: A required authorization for the specified action is not supplied. | You have not authorized Auto Scaling to perform the current action. | Check whether the OOSServiceRole RAM role has the required permissions. |
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 the corresponding resources. | Check whether the OOSServiceRole RAM role has the required permissions. For example, you can grant the OOS permissions to the 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. |
LifecycleHookIdAndLifecycleActionToken.Invalid message: The specified lifecycleActionToken and lifecycleActionId you provided does not match any in process lifecycle action. | The ongoing lifecycle hook action has ended or been stopped. | Assess the timeout period of the lifecycle hook to make sure that the O&M tasks specified in the OOS template can be complete within the allotted time limit. |
Related documents
-
A lifecycle hook is a tool used to manage the lifecycle of instances in a scaling group. You can use lifecycle hooks to perform custom actions on an instance before it is put into service or terminated. For more information, see Lifecycle hook overview.
-
CloudOps Orchestration Service (OOS) is an automated O&M service provided by Alibaba Cloud that you can use to automate task management and execution. For more information, see What is CloudOps Orchestration Service.
-
For advanced networking scenarios, such as creating a low-cost failover solution that requires public internet access, you can automatically attach a secondary elastic network interface (ENI) with an EIP to an ECS instance during a scale-out event. Conversely, during a scale-in event, you can automatically release the secondary ENI and its associated EIP when the instance is terminated or if public access is no longer required. For more information, see Automatically attach and release secondary ENIs with EIPs.