考慮以下場景:在購買 ECS 執行個體的時候,您可能沒有确定執行個體需要使用多長時間,您可以在購買執行個體的時候付費類型選擇按量計費,執行個體使用一段時間以後,您确定了執行個體的使用周期以後再将其轉換為包年包月執行個體。您可以登入 ECS控制台 或者使用 ModifyInstanceChargeType 接口将按量執行個體轉為包年包月執行個體。 和 接口每次最多支援 20 個按量執行個體同時轉換,考慮到您需要轉換的按量執行個體可能較多,本文在介紹通過 接口将按量執行個體轉換為包年包月執行個體的同時,還将介紹如何快速地将某個 Region 下的按量執行個體全部轉換為包年包月執行個體。
按量執行個體轉包年包月執行個體( )
登入
,進入執行個體清單頁,選擇地域,并選擇執行個體的付費類型為按量付費,如下圖所示:

選中需要轉換的按量執行個體(一次最多支援轉換 20 台按量執行個體),點選按量付費轉包年包月,如下圖所示:
點選按量付費轉包年包月以後,彈出按量付費轉包年包月操作界面,如下圖所示:
然後點選批量更改,彈出修改執行個體生命周期對話框,設定執行個體将要轉換成的包年包月執行個體時長,如果執行個體挂載着資料盤,可同時将資料盤轉為包年包月資料盤,如下圖所示:
設定好時長和是否轉換資料盤以後,點選确定,然後點選去下單,并支付訂單,此時按量執行個體就被轉換為包年包月執行個體。
阿裡雲雲伺服器ECS提供了
接口,供您将按量執行個體轉換為包年包月執行個體。本文将以 Python 代碼為例,向您介紹如何将按量執行個體轉換為包年包月執行個體,代碼如下:
# coding=utf-8 # if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs' # if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs' # make sure the sdk version is 4.1, you can use command 'pip show aliyun-python-sdk-ecs' to check import logging from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.ModifyInstanceChargeTypeRequest import ModifyInstanceChargeTypeRequest # configuration the log output formatter, if you want to save the output to file, # append ",filename='ecs_invoke.log'" after datefmt. # https://yq.aliyun.com/articles/69135 logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') import json clt = client.AcsClient('AK', 'AK-PWD', 'cn-hangzhou') instance_ids = ['instance-1', 'instance-2', 'instance-3']
def modify_instance_charge_type(): logging.info("instance ids : " + str(instance_ids)) request = ModifyInstanceChargeTypeRequest() request.set_InstanceIds(json.dumps(instance_ids)) # only generate data disk request.set_AutoPay(True) # include data disk request.set_IncludeDataDisks(True) request.set_Period(1) request.set_PeriodUnit('Month') response = _send_request(request) print response
# send open api request def _send_request(request): request.set_accept_format('json') try: response_str = clt.do_action(request) logging.info(response_str) response_detail = json.loads(response_str) return response_detail except Exception as e: logging.error(e)
if __name__ == '__main__': logging.info("Modify instance spec by OpenApi!") modify_instance_charge_type()
在使用以上代碼前,您可以參考 [使用OpenApi彈性管理雲伺服器ECS
](
https://yq.aliyun.com/articles/69135)來安裝 ECS Python SDK,然後使用上面的代碼将按量執行個體轉換為包年包月執行個體。在使用上面的代碼前,您需要補充自己的 AK資訊、執行個體ID 資訊,以及您期望轉換的包年包月執行個體時長資訊。如果需要了解更多參數的意義,您可以參考
接口說明文檔。
批量操作按量執行個體轉包年包月執行個體
如果您希望将某個地域下的所有按量執行個體快速地轉換為包年包月執行個體,此時通過
或者直接調用接口的時間成本可能會較大,針對這種場景,您可以參考以下代碼快速地将某個地域下的所有按量執行個體轉換為包年包月執行個體:
# coding=utf-8 # if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs' # if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs' # make sure the sdk version is 4.1, you can use command 'pip show aliyun-python-sdk-ecs' to check import logging from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest from aliyunsdkecs.request.v20140526.ModifyInstanceChargeTypeRequest import ModifyInstanceChargeTypeRequest # configuration the log output formatter, if you want to save the output to file, # append ",filename='ecs_invoke.log'" after datefmt. # https://yq.aliyun.com/articles/69135 logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') import json AK = 'ak' AK_SECRET = 'ak-secret' REGION_ID = 'region-id' clt = client.AcsClient(AK, AK_SECRET, REGION_ID) # every order support max 20 instances to convert. ORDER_PER_PAGE = 20 PAGE_SIZE = 100 INSTANCE_CHARGE_TYPE = "PostPaid" PERIOD = 1 PERIOD_UNIT = 'Month'
# 每頁查詢100個,每20個一個訂單。 def modify_instance_batch(): request = DescribeInstancesRequest() request.set_PageSize(PAGE_SIZE) request.set_InstanceChargeType(INSTANCE_CHARGE_TYPE) response = _send_request(request) instances_list = response.get('Instances').get('Instance') instance_ids = [] index = 0 for item in instances_list: index += 1 instance_id = item.get('InstanceId') instance_ids.append(instance_id) if len(instance_ids) == ORDER_PER_PAGE or len(instances_list) == index: modify_instance_charge_type(instance_ids) instance_ids = []
def modify_instance_charge_type(instance_ids): logging.info("instance ids : " + str(instance_ids)) request = ModifyInstanceChargeTypeRequest() request.set_InstanceIds(json.dumps(instance_ids)) # only generate data disk request.set_AutoPay(True) # include data disk request.set_IncludeDataDisks(True) request.set_Period(PERIOD) request.set_PeriodUnit(PERIOD_UNIT) response = _send_request(request) print response
# send open api request def _send_request(request): request.set_accept_format('json') try: response_str = clt.do_action(request) logging.info(response_str) response_detail = json.loads(response_str) return response_detail except Exception as e: logging.error(e)
def check_instance_count(): request = DescribeInstancesRequest() request.set_PageSize(1) request.set_InstanceChargeType(INSTANCE_CHARGE_TYPE) request.set_PageNumber(1) response = _send_request(request) total_count = response.get('TotalCount') return total_count
if __name__ == '__main__': logging.info("Modify instance spec by OpenApi!") count = check_instance_count() total_number = 0 if count != 0: if count % PAGE_SIZE == 0: total_number = count / PAGE_SIZE else: total_number = count / PAGE_SIZE + 1 for item in xrange(0, total_number): logging.info("Current Page is " + str(item)) modify_instance_batch()
在使用上述 Python 代碼進行按量執行個體轉換為包年包月執行個體前,您需要配置自己的 AK 資訊、Region 資訊以及您期望轉換的包年包月執行個體時長資訊。更多參數的詳細資訊,您可以參考
寫在最後
當您确定 ECS 執行個體的使用生命周期以後,建議您将按量執行個體轉換為包年包月執行個體,這樣可以節約您的執行個體使用成本。當您的包年包月執行個體到期後,如果您希望繼續使用此執行個體,您還可以通過
或者
RenewInstance續費接口對您的執行個體進行續費操作。注意,當您的按量執行個體轉換為包年包月執行個體以後,包年包月執行個體的生命周期起始時間從目前時間開始計算。