通过 Terraform 创建和管理接入中心策略,自动化配置 Prometheus 监控数据采集规则,支持 CS(容器服务)、ECS、Cloud、ES、KAFKA 等多种数据源类型。
前提条件
已安装 Terraform(v1.5.0 及以上版本)。
Cloud Shell 已默认安装并配置 Terraform 和阿里云账号信息,无需额外配置。
本地安装请参见在本地安装和配置Terraform。
已配置阿里云账号信息。支持以下两种方式:
说明建议创建 RAM 用户并授权,避免使用主账号 AccessKey。具体操作请参见创建RAM用户和管理RAM用户的权限。
方式一(推荐):通过环境变量配置身份认证信息。
export ALICLOUD_ACCESS_KEY="<YOUR-ACCESS-KEY>" export ALICLOUD_SECRET_KEY="<YOUR-SECRET-KEY>" export ALICLOUD_REGION="cn-hangzhou"方式二:在 Provider 代码块中指定身份认证信息。
provider "alicloud" { access_key = "<YOUR-ACCESS-KEY>" secret_key = "<YOUR-SECRET-KEY>" region = "cn-hangzhou" endpoints { cms = "cms.cn-hangzhou.aliyuncs.com" } }
重要必须显式指定 CMS endpoint,格式为
endpoints { cms = "cms.{region}.aliyuncs.com" },其中{region}替换为实际的地域 ID。
创建接入中心策略
接入中心策略的配置因 policy_type 而异:CS 类型需要额外配置 entity_group 块,其他类型无需配置。
策略类型 | 监控对象 | 是否需要 entity_group |
| 容器服务 ACK 集群 | 是 |
| 云服务器 ECS 实例 | 否 |
| 云服务指标 | 否 |
| Elasticsearch 实例 | 否 |
| 消息队列 Kafka 实例 | 否 |
创建 CS 类型策略
policy_type 为 CS 时,需要通过 entity_group 块指定关联的容器集群实体组。
创建工作目录,并在目录中创建名为
main.tf的配置文件,内容如下:terraform { required_version = ">= 1.5.0" required_providers { alicloud = { source = "aliyun/alicloud" version = "~> 1.277.0" } } } provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-hangzhou" endpoints { cms = "cms.cn-hangzhou.aliyuncs.com" } } resource "alicloud_cms_integration_policy" "this" { workspace = "your-workspace-name" integration_policy_name = "my-cs-integration-policy" policy_type = "CS" entity_group { cluster_entity_type = "acs.ack.cluster" cluster_id = "your-cluster-id" } } output "integration_policy_id" { value = alicloud_cms_integration_policy.this.id }参数说明如下:
参数
是否必填
说明
workspace是
工作空间名称。
integration_policy_name是
接入中心策略名称。
policy_type是
策略类型。修改此参数会强制创建新资源。
entity_group是(CS 类型)
实体组配置块。包含
cluster_entity_type和cluster_id。仅 CS 类型策略需要配置。初始化 Terraform 工作目录。
terraform init预览资源变更计划。
terraform plan执行创建。在交互提示中输入
yes确认。terraform apply预期输出:
alicloud_cms_integration_policy.this: Creation complete after XXs [id=************] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: integration_policy_id = "************"
创建 ECS/Cloud/ES/KAFKA 类型策略
非 CS 类型策略无需配置 entity_group 块,配置更简单。以下以 ECS 类型为例:
创建工作目录,并在目录中创建名为
main.tf的配置文件,内容如下:terraform { required_version = ">= 1.5.0" required_providers { alicloud = { source = "aliyun/alicloud" version = "~> 1.277.0" } } } provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-hangzhou" endpoints { cms = "cms.cn-hangzhou.aliyuncs.com" } } resource "alicloud_cms_integration_policy" "this" { workspace = "your-workspace-name" integration_policy_name = "my-ecs-integration-policy" policy_type = "ECS" } output "integration_policy_id" { value = alicloud_cms_integration_policy.this.id }说明将
policy_type替换为目标类型(ECS、Cloud、ES、KAFKA)即可创建对应策略。初始化 Terraform 工作目录。
terraform init预览资源变更计划。
terraform plan执行创建。在交互提示中输入
yes确认。terraform apply预期输出:
alicloud_cms_integration_policy.this: Creation complete after XXs [id=************] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: integration_policy_id = "************"
导入已有策略
将控制台已创建的接入中心策略纳入 Terraform 管理:
使用 import 块(Terraform v1.5+)
import {
to = alicloud_cms_integration_policy.this
id = "<integration-policy-id>"
}使用 terraform import 命令
terraform import alicloud_cms_integration_policy.this <integration-policy-id>删除接入中心策略
执行以下命令,删除通过 Terraform 管理的接入中心策略:
terraform destroy在交互提示中输入 yes 确认执行。预期输出:
alicloud_cms_integration_policy.this: Destroying... [id=************]
alicloud_cms_integration_policy.this: Destruction complete after XXs
Destroy complete! Resources: 1 destroyed.删除接入中心策略后,关联的数据采集将立即停止。执行前请确认该策略不再需要。
超时时间
alicloud_cms_integration_policy 资源支持以下超时配置:
操作 | 默认超时 |
create | 5 分钟 |
update | 5 分钟 |
delete | 5 分钟 |