考虑以下场景:在购买 ECS 实例的时候,您可能没有确定实例需要使用多长时间,您可以在购买实例的时候付费类型选择按量计费,实例使用一段时间以后,您确定了实例的使用周期以后再将其转换为包年包月实例。您可以登录 ECS控制台 或者使用 ModifyInstanceChargeType 接口将按量实例转为包年包月实例。
ECS控制台 和 ModifyInstanceChargeType 接口每次最多支持 20 个按量实例同时转换,考虑到您需要转换的按量实例可能较多,本文在介绍通过 ECS控制台 和 ModifyInstanceChargeType 接口将按量实例转换为包年包月实例的同时,还将介绍如何快速地将某个 Region 下的按量实例全部转换为包年包月实例。
按量实例转包年包月实例(ECS控制台)
登录 ECS控制台,进入实例列表页,选择地域,并选择实例的付费类型为按量付费,如下图所示:
选中需要转换的按量实例(一次最多支持转换 20 台按量实例),点击按量付费转包年包月,如下图所示:
点击按量付费转包年包月以后,弹出按量付费转包年包月操作界面,如下图所示:
然后点击批量更改,弹出修改实例生命周期对话框,设置实例将要转换成的包年包月实例时长,如果实例挂载着数据盘,可同时将数据盘转为包年包月数据盘,如下图所示:
设置好时长和是否转换数据盘以后,点击确定,然后点击去下单,并支付订单,此时按量实例就被转换为包年包月实例。
按量实例转包年包月实例(ModifyInstanceChargeType)
阿里云云服务器ECS提供了 ModifyInstanceChargeType 接口,供您将按量实例转换为包年包月实例。本文将以 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 信息,以及您期望转换的包年包月实例时长信息。如果需要了解更多参数的意义,您可以参考 ModifyInstanceChargeType 接口说明文档。
批量操作按量实例转包年包月实例
如果您希望将某个地域下的所有按量实例快速地转换为包年包月实例,此时通过 ECS控制台 或者直接调用接口的时间成本可能会较大,针对这种场景,您可以参考以下代码快速地将某个地域下的所有按量实例转换为包年包月实例:
# 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 信息以及您期望转换的包年包月实例时长信息。更多参数的详细信息,您可以参考 ModifyInstanceChargeType 接口说明文档。
写在最后
当您确定 ECS 实例的使用生命周期以后,建议您将按量实例转换为包年包月实例,这样可以节约您的实例使用成本。当您的包年包月实例到期后,如果您希望继续使用此实例,您还可以通过 ECS控制台 或者 RenewInstance续费接口 对您的实例进行续费操作。注意,当您的按量实例转换为包年包月实例以后,包年包月实例的生命周期起始时间从当前时间开始计算。