使用 for_each 填写列表参数
Use for_each to fill in list argument
我正在尝试向 google_monitoring_metric_descriptor
resource 的 labels
和 metadata
子块中添加条目。我很难为每个度量描述符以及 labels
和 metadata
的内部集合制作 for_each
循环我的结构图。它似乎不接受块内的内部 for_each
,并且尝试用 =
分配给它似乎也不起作用。
locals {
metrics = {
gsuite_user_count = {
name = "projects/my-gcp-project-123/metricDescriptors/custom.googleapis.com/gsuite_user_count",
labels = [
{
key = "gsuite_domain"
},
{
key = "opencensus_task",
description = "Opencensus task identifier"
}
],
metricKind = "GAUGE",
valueType = "INT64",
unit = "1",
description = "Number of users in GSuite Directory.",
displayName = "custom.googleapis.com/gsuite_user_count",
type = "custom.googleapis.com/gsuite_user_count"
}
}
}
resource "google_monitoring_metric_descriptor" "basic" {
provider = google-beta
for_each = local.metrics
description = lookup(each.value, "description")
display_name = each.value.displayName
# How do I do this?
labels = [for label in each.value.labels : {
key = label.key
value = label.value
}]
// launch_stage = each.value.launchStage
// metadata = each.value.metadata
metric_kind = each.value.metricKind
type = each.value.type
unit = each.value.unit
value_type = each.value.valueType
project = var.project_id
}
这在 terraform apply
上给出了这个错误:
Error: Unsupported argument
on /Users/jaycarlton/repos/workbench/ops/terraform/modules/workbench/modules/monitoring/modules/metrics/main.tf line 32, in resource "google_monitoring_metric_descriptor" "basic":
32: labels = [for label in each.value.labels : {
An argument named "labels" is not expected here. Did you mean to define a
block of type "labels"?
可以使用 dynamic blocks 迭代设置标签。可能是这样的:
dynamic "labels" {
for_each = each.value.labels
content {
key = labels.key
value = labels.value
}
}
我正在尝试向 google_monitoring_metric_descriptor
resource 的 labels
和 metadata
子块中添加条目。我很难为每个度量描述符以及 labels
和 metadata
的内部集合制作 for_each
循环我的结构图。它似乎不接受块内的内部 for_each
,并且尝试用 =
分配给它似乎也不起作用。
locals {
metrics = {
gsuite_user_count = {
name = "projects/my-gcp-project-123/metricDescriptors/custom.googleapis.com/gsuite_user_count",
labels = [
{
key = "gsuite_domain"
},
{
key = "opencensus_task",
description = "Opencensus task identifier"
}
],
metricKind = "GAUGE",
valueType = "INT64",
unit = "1",
description = "Number of users in GSuite Directory.",
displayName = "custom.googleapis.com/gsuite_user_count",
type = "custom.googleapis.com/gsuite_user_count"
}
}
}
resource "google_monitoring_metric_descriptor" "basic" {
provider = google-beta
for_each = local.metrics
description = lookup(each.value, "description")
display_name = each.value.displayName
# How do I do this?
labels = [for label in each.value.labels : {
key = label.key
value = label.value
}]
// launch_stage = each.value.launchStage
// metadata = each.value.metadata
metric_kind = each.value.metricKind
type = each.value.type
unit = each.value.unit
value_type = each.value.valueType
project = var.project_id
}
这在 terraform apply
上给出了这个错误:
Error: Unsupported argument
on /Users/jaycarlton/repos/workbench/ops/terraform/modules/workbench/modules/monitoring/modules/metrics/main.tf line 32, in resource "google_monitoring_metric_descriptor" "basic":
32: labels = [for label in each.value.labels : {
An argument named "labels" is not expected here. Did you mean to define a
block of type "labels"?
可以使用 dynamic blocks 迭代设置标签。可能是这样的:
dynamic "labels" {
for_each = each.value.labels
content {
key = labels.key
value = labels.value
}
}