我正在使用 Terraform 创建 Grafana 警报,并且创建了一个 YAML 配置,我试图在 Terraform 模块中引用该配置,但在引用嵌套列表时遇到了一些问题。
我的 YAML 配置“rules.yaml”:
dashboard_uid: "my-dashboard"
env:
staging:
folder_uid: "xxxxxxxxxxxxxx"
datasource_uid: "xxxxxxxxxxxx"
load_balancer: "my-loadbalancer"
datasource_config:
cloudwatch_config: &cloudwatch_config
datasource:
type: cloudwatch
uid: "${datasource_uid}"
intervalMs: 50
logGroups: []
matchExact: true
maxDataPoints: 1500
metricEditorMode: 0
metricQueryType: 0
period: ""
queryMode: Metrics
region: default
sqlExpression: ""
common_rule_config: &common_rule_config
no_data_state: "KeepLast"
exec_err_state: "KeepLast"
interval_seconds: 60
labels: {}
is_paused: true
for: "1m"
common_data_config: &common_data_config
relative_time_range:
from: 60
to: 0
rules:
- name: "My Alert"
condition: "B"
<<: *common_rule_config
annotations:
__alertId__: "1"
__dashboardUid__: "${dashboard_uid}"
__panelId__: "3"
message: "${load_balancer}: HTTP Code Counts alert"
data_blocks:
- ref_id: "A"
<<: *common_data_config
datasource_uid: "${datasource_uid}"
model:
<<: *cloudwatch_config
alias: "2XX"
dimensions:
LoadBalancer: "${load_balancer}"
label: "2XX"
metricName: "HTTPCode_Target_2XX_Count"
namespace: "AWS/ApplicationELB"
refId: "A"
statistic: "Sum"
- ref_id: "D"
<<: *common_data_config
datasource_uid: "${datasource_uid}"
model:
datasource_config: *cloudwatch_config
alias: "5XX"
dimensions:
LoadBalancer: "${load_balancer}"
label: "5XX"
metricName: "HTTPCode_Target_5XX_Count"
namespace: "AWS/ApplicationELB"
refId: "D"
statistic: "Sum"
- ref_id: "B"
<<: *common_data_config
datasource_uid: "__expr__"
model:
conditions:
- evaluator:
params: [20]
type: "lt"
operator:
type: "or"
query:
params: ["A"]
reducer:
params: []
type: "avg"
- evaluator:
params: [0]
type: "gt"
operator:
type: "or"
query:
params: ["D"]
reducer:
params: []
type: "avg"
intervalMs: 1000
maxDataPoints: 43200
refId: "B"
type: "classic_conditions"
我的 Terraform 模块:
locals {
alert_rules = yamldecode(file("${path.module}/rules.yaml"))
}
resource "grafana_rule_group" "rule_group_0000" {
name = "My Alerts"
folder_uid = "xxxxxxxxxxxxxxx"
interval_seconds = 60
dynamic "rule" {
for_each = local.alert_rules["rules"]
content {
name = rule.value["name"]
condition = rule.value["condition"]
dynamic "data" {
for_each = rule.value["data_blocks"]
content {
ref_id = data.value["ref_id"]
relative_time_range {
from = 60
to = 0
}
datasource_uid = ""
model = jsonencode({
model = data.value["model"]
})
}
}
}
}
}
现在数据块中的模型结构不同,我想知道如何引用它,是否可以直接引用整个列表而不需要设置单独的键值。另外,我在 YAML 配置中使用锚点和别名;这可以用吗还是我需要删除它们。
我尝试在动态“数据”中使用动态“模型”,但它不允许我这样做并引发错误。
我在这里看到几个问题。首先是
yamldecode
函数无法解析YAML锚点和别名,您需要重写rules.yaml或使用pyyaml
之类的工具来预处理文件。
第二个问题是你有多个
rules
和多个data_blocks
,所以你需要迭代它们。例如,类似:
resource "grafana_rule_group" "rule_group_0000" {
name = "My Alerts"
folder_uid = "xxxxxxxxxxxxxxx"
interval_seconds = 60
dynamic "rule" {
for_each = local.alert_rules["rules"]
content {
name = rule.value["name"]
condition = rule.value["condition"]
dynamic "data" {
for_each = rule.value["data_blocks"]
content {