1

我正在尝试将条目添加到资源labels的和metadata子块中。我很难循环遍历每个度量描述符的结构图,以及 和 的内部集合。它似乎不接受块内的内部,并且尝试分配给它似乎也不起作用。google_monitoring_metric_descriptor for_eachlabelsmetadatafor_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"?
4

1 回答 1

2

可以使用动态块迭代地设置标签。可能是这样的:

  dynamic "labels" {
    for_each = each.value.labels
    content {
      key   = labels.key
      value = labels.value
    }
  }
于 2020-10-26T21:42:42.167 回答