按量付费转包年包月之批量操作篇

简介: 阿里云 ECS 实例按实例付费类型分为两种:即 按量(后付费)实例 和 包年包月(预付费)实例,按量实例又被分为 普通按量实例 和 [竞价实例](https://help.aliyun.com/document_detail/52088.html) 两种,本文将介绍如何批量地将 普通按量实例 转换为 包年包月实例。

考虑以下场景:在购买 ECS 实例的时候,您可能没有确定实例需要使用多长时间,您可以在购买实例的时候付费类型选择按量计费,实例使用一段时间以后,您确定了实例的使用周期以后再将其转换为包年包月实例。您可以登录 ECS控制台 或者使用 ModifyInstanceChargeType 接口将按量实例转为包年包月实例。

ECS控制台ModifyInstanceChargeType 接口每次最多支持 20 个按量实例同时转换,考虑到您需要转换的按量实例可能较多,本文在介绍通过 ECS控制台ModifyInstanceChargeType 接口将按量实例转换为包年包月实例的同时,还将介绍如何快速地将某个 Region 下的按量实例全部转换为包年包月实例。

按量实例转包年包月实例(ECS控制台

登录 ECS控制台,进入实例列表页,选择地域,并选择实例的付费类型为按量付费,如下图所示:

粘贴图片.png

选中需要转换的按量实例(一次最多支持转换 20 台按量实例),点击按量付费转包年包月,如下图所示:

粘贴图片.png

点击按量付费转包年包月以后,弹出按量付费转包年包月操作界面,如下图所示:

粘贴图片.png

然后点击批量更改,弹出修改实例生命周期对话框,设置实例将要转换成的包年包月实例时长,如果实例挂载着数据盘,可同时将数据盘转为包年包月数据盘,如下图所示:

粘贴图片.png

设置好时长和是否转换数据盘以后,点击确定,然后点击去下单,并支付订单,此时按量实例就被转换为包年包月实例。

按量实例转包年包月实例(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 来安装 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续费接口 对您的实例进行续费操作。注意,当您的按量实例转换为包年包月实例以后,包年包月实例的生命周期起始时间从当前时间开始计算。

相关实践学习
通义万相文本绘图与人像美化
本解决方案展示了如何利用自研的通义万相AIGC技术在Web服务中实现先进的图像生成。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情: https://www.aliyun.com/product/ecs
相关文章
|
移动开发 前端开发 HTML5
分享136个HTML公司企业模板,总有一款适合您
分享136个HTML公司企业模板,总有一款适合您
577 0
|
存储 编译器 C语言
[Eigen中文文档] 对未对齐数组断言的解释
本文将解释程序因断言失败而终止的问题。
629 0
|
6月前
|
人工智能 专有云 索引
Qwen3-Pro重磅发布!基于阿里云APG服务器的专属优化模型,性能翻倍
近日,阿里云专有云团队联合通义实验室推出针对阿里云APG服务器的专属优化模型Qwen3-Pro:对比Qwen3-VL-235B(开源版)模型效果持平、性能翻倍
785 0
|
7月前
|
弹性计算 搜索推荐 应用服务中间件
最新版:阿里云服务器收费价格表一年、1个月和1小时费用标准清单
2025年阿里云服务器最新优惠价格表出炉!轻量应用服务器低至38元/年起,ECS云服务器2核2G仅99元/年,4核16G 89元/月,8核32G 160元/月,香港轻量服务器25元/月起,带宽高达200M,不限流量,新老用户同享,续费同价,限时秒杀,性价比极高。
|
10月前
|
机器学习/深度学习 存储 人工智能
AI 视频检测:重构食品质检体系,破解大规模生产品质难题
AI视频检测技术助力食品行业质检升级,通过实时感知、精准识别与数据驱动,实现从加工到成品的全流程智能管控,解决传统质检效率低、标准不统一等问题。
1197 0
|
SQL 弹性计算 安全
阿里云服务器租用价格:包年包月收费标准与最新活动价格参考
本文为大家分享阿里云服务器包年包月收费标准,云服务器最新活动价格,以及后续费挂载数据盘、设置密码和安全组等操作教程,以供参考。
|
人工智能 IDE JavaScript
分享一个很好用的代码辅助AI工具CodeGeeX2
分享一个很好用的代码辅助AI工具CodeGeeX2
788 1
|
达摩院 算法 Java
选择优化求解器的关键因素:以MindOpt为例
选择一款适合自己业务需求的求解器我们一般需要考量什么呢?可求解的问题类型?问题规模?本文将介绍一些需要考虑的重要因素,并且介绍阿里达摩院MindOpt优化求解器在这些因素下的表现。
|
存储 供应链 Python
用Python代码打造超市收银系统
用Python代码打造超市收银系统
1181 1
|
移动开发 前端开发 JavaScript
uniapp中IO模块(管理本地文件系统)的常用功能封装
uniapp中IO模块(管理本地文件系统)的常用功能封装
2551 1

热门文章

最新文章