Use OpenApi to complete cloud server ECS renewal management

Introduction: Alibaba Cloud is very important to the life cycle of the annual and monthly cloud servers, in addition to the cloud server renewals that can be performed on the ECS console or the sales page. If the cloud server resources cannot be renewed on time, the server may be locked. . So we need the API to easily know and check the expiration time of the resource.
In addition to cloud server renewals on the ECS console or sales page , Alibaba Cloud can also directly query and manage renewal fees through the API.
This article will cover several key functions
•Query cloud servers by expiration time
•Renewal example
•Query the automatic renewal time of the cloud server
•Set cloud server automatic renewal time
It is very important for the life cycle of the annual and monthly cloud server. If the cloud server resources cannot be renewed on time, the server may be locked or even released, which will affect the continuity of your business. So we need the API to easily know and check the expiration time of the resource. At the same time, you can easily complete the renewal and recharge.
In this article you need to pay attention to several APIs
•Query instance list
•Renewal example

【Use OpenApi to complete cloud server ECS renewal management】Query expired cloud servers within a specified range

You can query the instance information that expires within a certain time range through the following filter parameters through the API for querying the instance list. By setting the ExpiredStartTime and ExpiredEndTime of the filtering parameters , the time parameter is expressed according to the ISO8601 standard and needs to use UTC time. The format is: yyyy-MM-ddTHH:mmZ.
It is convenient to query the list of instances that expired within this time. If you need to filter by security group, you only need to add the security group id.

def describe_need_renew_instance( page_size =100, page_number =1, instance_id =None,
check_need_renew = True , security_group_id =None):
request = DescribeInstancesRequest()
if check_need_renew is True :
request.set_Filter3Key( "ExpiredStartTime" )
request.set_Filter4Key( "ExpiredEndTime" )
if instance_id is not None:
if security_group_id:
return _send_request(request)

【Use OpenApi to complete cloud server ECS renewal management】Renewal cloud server

Renewal instances only support annual and monthly server types, but do not support pay-as-you-go servers. At the same time, users are required to support account balance payment or credit payment. Synchronous deduction and order generation will be performed when the API is executed. Therefore, when executing the API, you must ensure that your account has sufficient funds to support automatic deduction.

def _renew_instance_action( instance_id , period ='1') :
request = RenewInstanceRequest()
request.set _Period( period )
request.set _InstanceId( instance_id )
response = _send_request( request )'renew %s ready, output is %s ', instance_id, response)
Renewed instances will be automatically deducted. After the renewal is executed, you can query the resource expiration time of the instance based on the InstanceId . Since the API is an asynchronous task, the query resource expiration time may need to be delayed for 10 seconds before it changes.
Enable automatic renewal of cloud server
In order to reduce the maintenance cost of your resources upon expiration, Alibaba Cloud has also launched the automatic renewal function for ECS instances with annual subscription and monthly subscription. The automatic renewal deduction date is 08:00:00 on the seventh day before the server If the daily automatic deduction fails, it will continue to be executed on the next day until the deduction is completed or the resources are locked after 7 days. You only need to ensure that your account balance or credit line is sufficient.
Query auto-renewal settings
We also provide OpenApi to query and set automatic renewal. This API also only supports monthly and annual instances, and the execution of pay-as-you-go instances will report an error.
Querying the automatic renewal status of an instance supports querying a maximum of 100 instances with an annual subscription and a monthly subscription. Multiple instance IDs are connected with commas.
The input parameter of DescribeInstanceAutoRenewAttribut is the instance ID.
•InstanceId: Supports querying up to 100 instances with annual and monthly subscriptions. Multiple instance IDs are connected by commas.
# check the instances is renew or not
def describe_auto_renew(instance_ids, expected_auto_renew = True ):
describe_request = DescribeInstanceAutoRenewAttributeRequest()
response_detail = _send_request( request =describe_request )
failed_instance_ids = ''
if response_detail is not None:
attributes = response_detail.get ( 'InstanceRenewAttributes' ). get ( ' InstanceRenewAttribute ' )
if attributes:
for item in attributes:
auto_renew_status = item.get ( ' AutoRenewEnabled' )
if auto_renew_status != expected_auto_renew:
failed_instance_ids += item.get ( ' InstanceId' ) + ','

describe_auto_renew( 'i-1111,i-2222' )
The content returned is as follows
{ "InstanceRenewAttributes" :{ "InstanceRenewAttribute" :[{ "Duration" : 0 , "InstanceId" : "i-1111" , "AutoRenewEnabled" : false },{ "Duration" : 0 , "InstanceId" : "i-2222 " , "AutoRenewEnabled" : false }]}, "RequestId" : "71FBB7A5-C793-4A0D-B17E-D6B426EA746A" }
If auto-renewal is set, the returned property AutoRenewEnabled is true, otherwise it returns false.
Set up and cancel automatic renewal of cloud server
There are three parameters for setting automatic renewal:
•InstanceId: Supports querying up to 100 instances with annual and monthly subscriptions. Multiple instance IDs are connected by commas.
•Duration: support 1, 2, 3, 6, 12, the unit is month
•AutoRenew: true/false, true to enable automatic renewal, false to cancel automatic renewal
def setting _instance_auto_renew( instance_ids , auto_renew = True) :'execute enable auto renew ' + instance_ids)
request = ModifyInstanceAutoRenewAttributeRequest() ;
request.set _Duration(1) ;
request.set _AutoRenew( auto_renew ) ;
request.set _InstanceId( instance_ids )
_send_request( request )
If the execution is successful, the Response is returned as follows:
{ "RequestId" : "7DAC9984-AAB4-43EF-8FC7-7D74C57BE46D" }
After the renewal is successful, you can perform another query. If the renewal is successful, it will return the duration of the renewal and whether to enable automatic renewal.
{ "InstanceRenewAttributes" :{ "InstanceRenewAttribute" :[{ "Duration" : 1 , "InstanceId" : "i-1111" , "AutoRenewEnabled" : true },{ "Duration" : 1 , "InstanceId" : "i-2222 " , "AutoRenewEnabled" : true }]}, "RequestId" : "7F4D14B0-D0D2-48C7-B310-B1DF713D4331" }

Copyright statement: The content of this article is contributed by Alibaba Cloud real-name registered users. The copyright belongs to the original author. The Alibaba Cloud developer community does not own the copyright and does not assume the corresponding legal responsibility. For specific rules, please refer to the " Alibaba Cloud Developer Community User Service Agreement " and " Alibaba Cloud Developer Community Intellectual Property Protection Guidelines ". If you find any content suspected of plagiarism in this community, fill out the infringement complaint form to report it. Once verified, this community will delete the allegedly infringing content immediately.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us