You need to enable JavaScript to run this app.
导航

创建云服务器实例

最近更新时间2023.05.31 16:11:06

首次发布时间2023.05.26 14:18:47

本文介绍如何创建一台或多台云服务器实例。

示例代码

# Example Code generated by Beijing Volcanoengine Technology.
from __future__ import print_function
import volcenginesdkcore
import volcenginesdkecs
from pprint import pprint
from volcenginesdkcore.rest import ApiException

if __name__ == '__main__':
    configuration = volcenginesdkcore.Configuration()
    # 设置您的ak、sk和要访问的地域
    configuration.ak = "AK"
    configuration.sk = "SK"
    configuration.region = "cn-beijing"
    # 设置默认配置
    volcenginesdkcore.Configuration.set_default(configuration)

    # 使用全局默认配置
    api_instance = volcenginesdkecs.ECSApi()

    # 为云服务器实例创建网卡
    req_network_interfaces0 = volcenginesdkecs.NetworkInterfaceForRunInstancesInput(
        security_group_ids=["sg-3ti78x9h8t4bw*****"],# 网卡关联的安全组
        subnet_id="subnet-3tispp1nai4e8i****",# 网卡关联的子网ID
    )
    
    # 为云服务器实例创建云盘
    req_volumes0 = volcenginesdkecs.VolumeForRunInstancesInput(
        size=40,# 云盘容量大小
        volume_type="ESSD_PL0",# 云盘类型
    )

    # 创建云服务器实例
    run_instances_request = volcenginesdkecs.RunInstancesRequest(
        count=1,# 实例的数量
        image_id="image-3tefr6wgx63vj0******",# 镜像ID
        instance_name="instance-test",# 实例的名称
        instance_type_id="ecs.g1ie.xlarge",# 实例的规格
        network_interfaces=[req_network_interfaces0],# 实例的网卡,即上文创建的网卡
        password="password@123",# 实例的登录密码
        volumes=[req_volumes0],# 实例的云盘,即上文创建的云盘
        zone_id="cn-beijing-a",# 实例所属的可用区
    )

		# 发起请求并处理返回或异常
    try:
        resp = api_instance.run_instances(run_instances_request)
        pprint(resp)
    except ApiException as e:
        print("Exception when calling api: %s\n" % e)

相关文档