如何在另一个地图(对象)资源中调用一个地图(对象)资源的值

问题描述 投票:0回答:1
resource "google_monitoring_custom_service" "customsrv" {
  for_each = var.custom_service_level
  display_name = each.value.service_display_name
  service_id = each.value.service_id
  project = var.project_id
}

// Google Monitoring SLO objects can support many different metric types, for more info see our documenation. 
resource "google_monitoring_slo" "custom_request_based_slo" {
   for_each = var.custom_sli
   service = google_monitoring_custom_service.customsrv[each.key].service_id
  display_name = each.value.metric_display_name
  goal = each.value.goal
  rolling_period_days = each.value.rolling_period_days // Replacable with calendar_period = "DAY", "WEEK", "FORTNIGHT", or "MONTH"
  request_based_sli {
    // Alternate implementation could use distribution_cut instead of good_total_ratio. 
    good_total_ratio {
      // Any combination of two elements: good_service_filter, bad_service_filter, total_service_filter. 
      good_service_filter = join(" AND ", each.value.good_service_filter)
      total_service_filter = join(" AND ", each.value.total_service_filter)
    }
  }
  depends_on = [ google_monitoring_custom_service.customsrv ]
}

使用 .tfvars 值:

custom_service_level = {
  "composer-service" = {
    service_id = "custom-srv-slos"
    service_display_name = "My Custom SLO"    
  }
}

custom_sli = {
  "composer-health" = { 
    metric_display_name = "test slo with service based SLI"
    goal = 0.9
    rolling_period_days = 28
    window_period = "300s"
    good_service_filter = [
          "metric.type=\"composer.googleapis.com/workflow/run_count\"",
          "resource.type=\"cloud_composer_workflow\"",
          "metric.labels.state=\"success\"",
      ],
    total_service_filter = [
          "metric.type=\"composer.googleapis.com/workflow/run_count\"",
          "resource.type=\"cloud_composer_workflow\"",
      ]
  },
}

使用的变量:

variable "custom_service_level" {
  type = map(object({
    service_id = string,
    service_display_name = string,
  })) 
} 

variable "custom_sli" {
  type = map(object({
     metric_display_name = string,
     goal = number,
     rolling_period_days = number,
     good_service_filter = list(string),
     total_service_filter = list(string)
    # service = string   
  }))
}

计划中出现此错误:

│ Error: Invalid index
│
│   on main.tf line 600, in resource "google_monitoring_slo" "custom_request_based_slo":
│  600:    service = google_monitoring_custom_service.customsrv[each.key].service_id
│     ├────────────────
│     │ each.key is "composer-health"
│     │ google_monitoring_custom_service.customsrv is object with 1 attribute "composer-service"
│
│ The given key does not identify an element in this collection value.
╵
google-cloud-platform terraform terraform-provider-gcp google-cloud-monitoring
1个回答
0
投票

问题

您正在定义 2 个地图,每个地图都有不同的键 -

composer-service
composer-health
:

custom_service_level = {
  "composer-service" = { # <--------------------- map key
    # ...
  }
}

custom_sli = {
  "composer-health" = { # <--------------------- map key
    # ...
  }
}

以下行将引发错误,因为没有带有键

google_monitoring_custom_service.customsrv
composer-health
资源:

service = google_monitoring_custom_service.customsrv[each.key].service_id

解决方法1

如果

custom_service_level
custom_sli
之间存在 1:1 关系,您可以在两个地图中使用相同的键。

使用

null_resource
的工作示例:

variable "custom_service_level" {
  type = map(object({
    service_id           = string,
    service_display_name = string,
  }))
  default = {
    "composer-001" = { // <-------------------- map key
      service_id           = "custom-srv-slos"
      service_display_name = "My Custom SLO"
    }
  }
}

variable "custom_sli" {
  type = map(object({
    metric_display_name  = string,
    goal                 = number,
    rolling_period_days  = number,
    good_service_filter  = list(string),
    total_service_filter = list(string)
  }))
  default = {
    "composer-001" = { // <-------------------- same map key used in var.custom_service_level
      metric_display_name = "test slo with service based SLI"
      goal                = 0.9
      rolling_period_days = 28
      window_period       = "300s"
      good_service_filter = [
        "metric.type=\"composer.googleapis.com/workflow/run_count\"",
        "resource.type=\"cloud_composer_workflow\"",
        "metric.labels.state=\"success\"",
      ],
      total_service_filter = [
        "metric.type=\"composer.googleapis.com/workflow/run_count\"",
        "resource.type=\"cloud_composer_workflow\"",
      ]
    },
  }
}

resource "null_resource" "customsrv" {
  for_each = var.custom_service_level

  triggers = {
    display_name = each.value.service_display_name
    service_id   = each.value.service_id
  }
}

resource "null_resource" "custom_request_based_slo" {
  for_each = var.custom_sli

  triggers = {
    service             = null_resource.customsrv[each.key].triggers.service_id
    display_name        = each.value.metric_display_name
    goal                = each.value.goal
    rolling_period_days = each.value.rolling_period_days
  }
}

跑步

terraform plan
:

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.custom_request_based_slo["composer-001"] will be created
  + resource "null_resource" "custom_request_based_slo" {
      + id       = (known after apply)
      + triggers = {
          + "display_name"        = "test slo with service based SLI"
          + "goal"                = "0.9"
          + "rolling_period_days" = "28"
          + "service"             = "custom-srv-slos"
        }
    }

  # null_resource.customsrv["composer-001"] will be created
  + resource "null_resource" "customsrv" {
      + id       = (known after apply)
      + triggers = {
          + "display_name" = "My Custom SLO"
          + "service_id"   = "custom-srv-slos"
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

解决方法2

custom_service_level
属性添加到
var.custom_sli
。该属性必须包含
var.custom_service_level
的映射键之一。

使用

null_resource
的工作示例:

variable "custom_service_level" {
  type = map(object({
    service_id           = string,
    service_display_name = string,
  }))
  default = {
    "composer-service" = { // <-------------------- map key
      service_id           = "custom-srv-slos"
      service_display_name = "My Custom SLO"
    }
  }
}

variable "custom_sli" {
  type = map(object({
    custom_service_level = string, // <------------------ new property
    metric_display_name  = string,
    goal                 = number,
    rolling_period_days  = number,
    good_service_filter  = list(string),
    total_service_filter = list(string)
  }))
  default = {
    "composer-health" = {
      custom_service_level = "composer-service" // <-------------------- map key from var.custom_service_level
      metric_display_name  = "test slo with service based SLI"
      goal                 = 0.9
      rolling_period_days  = 28
      window_period        = "300s"
      good_service_filter = [
        "metric.type=\"composer.googleapis.com/workflow/run_count\"",
        "resource.type=\"cloud_composer_workflow\"",
        "metric.labels.state=\"success\"",
      ],
      total_service_filter = [
        "metric.type=\"composer.googleapis.com/workflow/run_count\"",
        "resource.type=\"cloud_composer_workflow\"",
      ]
    },
  }
}

resource "null_resource" "customsrv" {
  for_each = var.custom_service_level

  triggers = {
    display_name = each.value.service_display_name
    service_id   = each.value.service_id
  }
}

resource "null_resource" "custom_request_based_slo" {
  for_each = var.custom_sli

  triggers = {
    service             = null_resource.customsrv[each.value.custom_service_level].triggers.service_id
    display_name        = each.value.metric_display_name
    goal                = each.value.goal
    rolling_period_days = each.value.rolling_period_days
  }
}

跑步

terraform plan
:

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.custom_request_based_slo["composer-health"] will be created
  + resource "null_resource" "custom_request_based_slo" {
      + id       = (known after apply)
      + triggers = {
          + "display_name"        = "test slo with service based SLI"
          + "goal"                = "0.9"
          + "rolling_period_days" = "28"
          + "service"             = "custom-srv-slos"
        }
    }

  # null_resource.customsrv["composer-service"] will be created
  + resource "null_resource" "customsrv" {
      + id       = (known after apply)
      + triggers = {
          + "display_name" = "My Custom SLO"
          + "service_id"   = "custom-srv-slos"
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.
© www.soinside.com 2019 - 2024. All rights reserved.