使用函数服务的 SDK 可以更加便捷地执行创建函数、上传代码和管理触发器等操作。本文介绍 Go SDK 的使用案例。
安装 Go 环境,并添加火山 veFaaS 提供的 Go SDK 依赖。具体信息,请参见SDK 概览。
package main import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/vefaas" "github.com/volcengine/volcengine-go-sdk/volcengine" "github.com/volcengine/volcengine-go-sdk/volcengine/credentials" "github.com/volcengine/volcengine-go-sdk/volcengine/session" ) func main() { CreateFunctionMicroServiceCode() } func CreateFunctionMicroServiceCode() { region := "cn-beijing" accessKey := "yourAccessKeyId" secretKey := "yourAccessKeySecret" config := volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(accessKey, secretKey, "")).WithRegion(region) s, err := session.NewSession(config) if err != nil { panic(err) } client := vefaas.New(s) resp, err := client.CreateFunction(&vefaas.CreateFunctionInput{ Name: volcengine.String("micro-service-doctest"), Runtime: volcengine.String("native/v1"), VpcConfig: &vefaas.VpcConfigForCreateFunctionInput{ EnableVpc: volcengine.Bool(true), VpcId: volcengine.String("vpc-id"), SubnetIds: []*string{volcengine.String("subnet-id")}, SecurityGroupIds: []*string{volcengine.String("sg-id")}, }, CpuStrategy: volcengine.String("always"), Command: volcengine.String("./run.sh"), }) if err != nil { panic(err) } fmt.Println(resp) }
请根据业务真实情况配置代码参数,可参考文档CreateFunction,下表只介绍创建微服务应用时的特殊配置。
参数 | 说明 |
---|---|
Runtime | 设置函数的 Runtime 运行时。 |
CpuStrategy | CPU 分配模式。 |
Command | 程序的启动命令。 |
package main import ( "fmt" "github.com/volcengine/volcengine-go-sdk/service/vefaas" "github.com/volcengine/volcengine-go-sdk/volcengine" "github.com/volcengine/volcengine-go-sdk/volcengine/credentials" "github.com/volcengine/volcengine-go-sdk/volcengine/session" ) const ( region = "cn-beijing" accessKey = "yourAccessKeyId" secretKey = "yourAccessKeySecret" ) func main() { CreateFunctionMicroServiceImage() } func CreateFunctionMicroServiceImage() { config := volcengine.NewConfig().WithCredentials(credentials.NewStaticCredentials(accessKey, secretKey, "")).WithRegion(region) s, err := session.NewSession(config) if err != nil { panic(err) } client := vefaas.New(s) resp, err := client.CreateFunction(&vefaas.CreateFunctionInput{ Name: volcengine.String("micro-service-image-doctest"), Runtime: volcengine.String("native/v1"), SourceType: volcengine.String("image"), Source: volcengine.String("vefaas-enterprise-ap-southeast-1.cr.volces.com/vefaastest/nodejs:1.0"), VpcConfig: &vefaas.VpcConfigForCreateFunctionInput{ EnableVpc: volcengine.Bool(true), VpcId: volcengine.String("vpc-id"), SubnetIds: []*string{volcengine.String("subnet-id")}, SecurityGroupIds: []*string{volcengine.String("sg-id")}, }, CpuStrategy: volcengine.String("always"), Command: volcengine.String("/opt/application/run.sh"), }) if err != nil { panic(err) } fmt.Println(resp) }
请根据业务真实情况配置代码参数,可参考文档CreateFunction,下表只介绍创建微服务应用时的特殊配置。
参数 | 说明 |
---|---|
Runtime | 设置函数的 Runtime 运行时。微服务应用目前仅支持 |
CpuStrategy | CPU 分配模式。微服务应用默认为始终分配 CPU,取值为 |
Command | 程序的启动命令。 |
通过 SDK 创建函数后,您可以登录函数服务控制台查看是否成功创建函数,微服务应用的CPU 分配模式固定为始终分配。