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

创建VPC

最近更新时间2023.08.29 19:07:47

首次发布时间2023.06.09 11:20:33

本文介绍如何创建一个VPC。

示例代码

// Example Code generated by Beijing Volcanoengine Technology.

package vpcexample

import (
	"fmt"

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

func CreateVpc() {
  // 设置您的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 := vpc.New(sess)

	// 创建一个VPC
	createVpcInput := &vpc.CreateVpcInput{
		CidrBlock:   volcengine.String("172.XX.XX.0/12"),           // VPC的网段
		Description: volcengine.String("test"),                     // VPC的描述
		DnsServers:  volcengine.StringSlice([]string{"1.XX.XX.1"}), // VPC的DNS服务器地址
		VpcName:     volcengine.String("test"),                     // VPC的名称
	}

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

相关文档