TIC Best Practices

TIC 是腾讯云推出的 IaC 开放平台,融合多种业内优秀的开源技术,通过 IaC 的方式解决您在云基础设施管理中面临的效率、成本和安全问题。

TIC 提供了资源编排、配置管理和合规检查三大功能模块,支持 HCL(Terraform)格式语法编写,同时提供丰富的基于腾讯云最佳实践的公共模板,有效降低您的学习、使用难度。

操作指南

产品文档非常详细了,以下就不赘述,下面提供一个创建腾讯云对象存储(COS)存储桶的实际用例。

控制台部署腾讯云资源

新建资源栈

创建 cos 资源描述文件

创建 cos.tf 文件

文件内容如下:

resource "tencentcloud_cos_bucket" "demo" {
bucket = "demo-1251762279"
acl = "private"
}

预览结果 (terraform plan)

发布 (terraform apply)

回到控制台,可以看到刚刚部署的资源已经生效

销毁资源,控制台对应的资源也会销毁

API调用

使用 API Explorer 生成代码

调试地址

Golang Example

package main

import (
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
tic "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tic/v20201117"
"time"
)

func describeStacks(client *tic.Client) []*tic.StackInfo {
request := tic.NewDescribeStacksRequest()
response, err := client.DescribeStacks(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil
}
if err != nil {
panic(err)
}
return response.Response.Stacks
}

func createStack(client *tic.Client, name, region, templateUrl string) *string {
request := tic.NewCreateStackRequest()
request.StackName = common.StringPtr(name)
request.StackRegion = common.StringPtr(region)
request.TemplateUrl = common.StringPtr(templateUrl)

response, err := client.CreateStack(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil
}
if err != nil {
panic(err)
}
return response.Response.StackId
}

func deleteStack(client *tic.Client, stackId string) {
request := tic.NewDeleteStackRequest()
request.StackId = common.StringPtr(stackId)

_, err := client.DeleteStack(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
}

func describeStackVersions(client *tic.Client) []*tic.VersionInfo {
request := tic.NewDescribeStackVersionsRequest()

response, err := client.DescribeStackVersions(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil
}
if err != nil {
panic(err)
}
return response.Response.Versions
}

func planStack(client *tic.Client, stackId, versionId string) *string {
request := tic.NewPlanStackRequest()
request.StackId = common.StringPtr(stackId)
request.VersionId = common.StringPtr(versionId)

response, err := client.PlanStack(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return nil
}
if err != nil {
panic(err)
}
fmt.Printf("%s\n", response.ToJsonString())
return response.Response.EventId
}

func applyStack(client *tic.Client, stackId, versionId string) {
request := tic.NewApplyStackRequest()
request.StackId = common.StringPtr(stackId)
request.VersionId = common.StringPtr(versionId)

response, err := client.ApplyStack(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return
}
if err != nil {
panic(err)
}
fmt.Printf("%s", response.ToJsonString())
}

func describeStackEvent(client *tic.Client, eventId string) string {
request := tic.NewDescribeStackEventRequest()
request.EventId = common.StringPtr(eventId)

response, err := client.DescribeStackEvent(request)
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
return ""
}
if err != nil {
panic(err)
}
return *response.Response.Status
}

func main() {
credential := common.NewCredential(
"SecretId",
"SecretKey",
)
cpf := profile.NewClientProfile()
cpf.HttpProfile.Endpoint = "tic.tencentcloudapi.com"
client, _ := tic.NewClient(credential, "", cpf)
region := "ap-shanghai"

//获取资源栈列表
stacks := describeStacks(client)
for _, stack := range stacks {
//删除资源栈
deleteStack(client, *stack.StackId)
}
//创建资源栈
createStack(client, "demo", region, "")
//获取版本列表
versions := describeStackVersions(client)
for _, version := range versions {
fmt.Println(*version.StackId, *version.VersionId, *version.VersionName)
//执行Plan事件
planStack(client, *version.StackId, *version.VersionId)
//等待事件完成
time.Sleep(time.Second * 30)
//执行Apply事件
applyStack(client, *version.StackId, *version.VersionId)
}
}
templateUrl (⽬前仅限 COS URL)

这里需要注意的是 templateUrl 参数 (模板 URL,⽬前仅限 COS URL, ⽂件为zip压缩格式),压缩为 zip 文件上传 cos。

API 调用执行

回到控制台,可以看到刚刚调用生效

参考文档