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

创建云服务器实例

最近更新时间2023.05.24 14:24:43

首次发布时间2023.05.24 14:24:43

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

示例代码

package com.volcengine.ecs.examples;

import com.volcengine.ApiClient;
import com.volcengine.ApiException;
import com.volcengine.sign.Credentials;

import com.volcengine.ecs.EcsApi;
import com.volcengine.ecs.model.*;
import java.util.*;


public class TestRunInstances {
    public static void main(String[] args) throws Exception {
		    //设置您的ak
        String ak = "Your AK";
        //设置您的sk
        String sk = "Your SK";
        //设置您要访问的地域
        String region = "Your Region";

				//初始化apiclient
        ApiClient apiClient = new ApiClient()
                .setCredentials(Credentials.getCredentials(ak, sk))
                .setRegion(region);

        EcsApi api = new EcsApi(apiClient);

        //为云服务器实例创建网卡
        NetworkInterfaceForRunInstancesInput reqNetworkInterfaces0 = new NetworkInterfaceForRunInstancesInput();
        reqNetworkInterfaces0.setSecurityGroupIds(Arrays.asList("sg-3ti78x9h8t4bw*****"));//网卡关联的安全组
        reqNetworkInterfaces0.setSubnetId("subnet-3tispp1nai4e8i****");//网卡关联的子网ID

        //为云服务器实例创建云盘
        VolumeForRunInstancesInput reqVolumes0 = new VolumeForRunInstancesInput();
        reqVolumes0.setSize(40);//云盘容量大小
        reqVolumes0.setVolumeType("ESSD_PL0");//云盘类型

        //创建云服务器实例
        RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
        runInstancesRequest.setCount(1);//实例的数量
        runInstancesRequest.setImageId("image-3tefr6wgx63vj0******");//镜像ID
        runInstancesRequest.setInstanceName("instance-test");//实例的名称
        runInstancesRequest.setInstanceTypeId("ecs.g1ie.xlarge");//实例的规格
        runInstancesRequest.setNetworkInterfaces(Arrays.asList(reqNetworkInterfaces0));//实例的网卡,即上文创建的网卡
        runInstancesRequest.setPassword("password@123");//实例的登录密码
        runInstancesRequest.setVolumes(Arrays.asList(reqVolumes0));//实例的云盘,即上文创建的云盘
        runInstancesRequest.setZoneId("cn-beijing-a");//实例所属的可用区

        // 发起请求并处理返回或异常
        try {
            RunInstancesResponse response = api.runInstances(runInstancesRequest);
            System.out.println(response);
        } catch (ApiException e) {
            System.out.println(e.getResponseBody());
        }
    }
}

相关文档