现在建站好么,泉州免费建站模板,建站宝盒创业经历,网站诊断与优化的作用1 腾讯云
1.1 tf配置
标签#xff08;Tag#xff09;是腾讯云提供的云资源管理工具#xff0c;以键值对 key:values 的形式存在#xff0c;用于关联您的大多数云资源#xff0c;对于资源的分类、搜索和聚合十分有用。 在 Terraform 中#xff0c;通过 Map 来定义一个资…1 腾讯云
1.1 tf配置
标签Tag是腾讯云提供的云资源管理工具以键值对 key:values 的形式存在用于关联您的大多数云资源对于资源的分类、搜索和聚合十分有用。 在 Terraform 中通过 Map 来定义一个资源的 Tag示例如下
resource tencentcloud_instance cvm {tags {key1: val1,key2: val2}
}
1.2 Tag 代码实现
通过云 API 对资源添加 Tag 有两种实现方式。第一种是通过 CreateAPI 参数传入代码如下
rsType : instancerequest : cvm.NewRunInstanceRequest()
request.TagSpecification append(request.TagSpecification, cvm.TagSpecification{ResourceType: rsType,Tags: []*cvm.Tag{{Key: key,Value: value,},},
})
需要注意的是目前仅有部分资源的创建 API 支持传入 Tag且数据结构也有所不同。为了统一管理 Tag 代码我们采用第二种方式即创建后单独调用 Tag API 关联入参为 资源六段式格式为
qcs:project_id此处置空:模块:地域:账号/uin:资源/ID
以 VPC 举例如要修改 VPC 实例关联的 Tag入参如下
qcs::vpc:ap-singapore:uin/:vpc/vpc-xxxxxxxx
代码中则调用封装好的 ModifyTags 即可
package mainfunc ResourceTencentCloudVPCUpdate(d *schema.ResourceData, meta interface{}) {ctx : context.TODO()region : meta.(*TencentCloudClient).apiV3Conn.Regionid : d.Id()resourceName : fmt.Sprintf(qcs::vpc:%s:uin/:vpc/%s, region, id)replaceTags, deleteTags : diffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))if err : tagService.ModifyTags(ctx, resourceName, replaceTags, deleteTags); err ! nil {return err}
}
2 AWS
2.1 tf配置
1直接在resource配置
resource aws_instance example {ami ami-0c55b159cbfafe1f0instance_type t2.microtags {env devapp webrole frontend}
}2单独配置变量
# 设置提供商
provider aws {region us-west-2
}# 定义标签
variable tags {type mapdefault {Name example-resourceEnvironment production}
}# 创建 EC2 实例
resource aws_instance example {ami ami-0c94855ba95c71c99instance_type t2.microtags var.tags
}
3default_tags块中添加动态值的标签
provider aws {region us-west-2default_tags {tags {Name my-instanceEnvironment var.environmentProject var.project}}
}variable environment {description The environmentdefault dev
}variable project {description The project namedefault my-project
}resource aws_instance example {ami ami-0c94855ba95c71c99instance_type t2.micro
}
3 阿里云
3.1 tf配置
...
resource alicloud_db_instance instance {
...tags {testTarraform PG12}
}
4 华为云
4.1 tf配置
resource huaweicloud_compute_instance default {cidr_block 10.${var.cidr_numeral}.0.0/16enable_dns_hostnames truetags {Name vpc-${var.vpc_name}}
}
5 GCP
provider google { project google-project-id region us-central1
}resource google_compute_instance default { name example-instance machine_type f1-micro// 添加标签 labels { environment production tier web }
}
resource google_compute_instance example {name example-instancemachine_type n1-standard-1zone us-central1-acount var.set_labels ? 1 : 0labels {[var.label_name] var.label_value} lifecycle {ignore_changes [labels]}
}