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

创建云服务器实例

最近更新时间2023.05.26 14:18:47

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

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

示例代码

package ecsexample

import (
	"fmt"

	"github.com/volcengine/volcengine-go-sdk/service/ecs"
	"github.com/volcengine/volcengine-go-sdk/volcengine"
	"github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
	"github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func RunInstances() {
	//设置您的ak、sk和要访问的地域
	ak, sk, region := "Your AK", "Your SK", "Region"
	config := volcengine.NewConfig().
		WithRegion(region).
		WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
	sess, err := session.NewSession(config)
	if err != nil {
		panic(err)
	}
	svc := ecs.New(sess)
	
	//为云服务器实例创建网卡
	reqNetworkInterfaces0 := &ecs.NetworkInterfaceForRunInstancesInput{
		SecurityGroupIds: volcengine.StringSlice([]string{"sg-3ti78x9h8t4bw*****"}),//网卡关联的安全组
		SubnetId:         volcengine.String("subnet-3tispp1nai4e8i****"),//网卡关联的子网ID
	}
	
	//为云服务器实例创建云盘
	reqVolumes0 := &ecs.VolumeForRunInstancesInput{
		Size:       volcengine.Int32(40),//云盘容量大小
		VolumeType: volcengine.String("ESSD_PL0"),//云盘类型
	}
	
	//创建云服务器实例
	runInstancesInput := &ecs.RunInstancesInput{
		Count:             volcengine.Int32(1),//实例的数量
		ImageId:           volcengine.String("image-3tefr6wgx63vj0******"),//镜像ID
		InstanceName:      volcengine.String("instance-test"),//实例的名称
		InstanceTypeId:    volcengine.String("ecs.g1ie.xlarge"),//实例的规格
		NetworkInterfaces: []*ecs.NetworkInterfaceForRunInstancesInput{reqNetworkInterfaces0},//实例的网卡,即上文创建的网卡
		Password:          volcengine.String("password@123"),//实例的登录密码
		Volumes:           []*ecs.VolumeForRunInstancesInput{reqVolumes0},//实例的云盘,即上文创建的云盘
		ZoneId:            volcengine.String("cn-beijing-a"),//实例所属的可用区
	}

	// 发起请求并处理返回或异常
	resp, err := svc.RunInstances(runInstancesInput)
	if err != nil {
		panic(err)
	}
	fmt.Println(resp)
}

相关文档