简体   繁体   中英

Use for_each to fill in list argument

I'm trying to add entries to the labels and metadata sub-blocks on the google_monitoring_metric_descriptor resource . I'm having a tough time making for_each loop over my map of structures for each metric descriptor, as well as the inner collections for labels and metadata . It doesn't appear to accept an inner for_each inside the block, and trying to assign to it with = also seems not to work.


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
}

This gives this error on 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"?

The label could be set iteratively using dynamic blocks . It could be something along these lines:

  dynamic "labels" {
    for_each = each.value.labels
    content {
      key   = labels.key
      value = labels.value
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM