azure 从terraform嵌套map对象调用元素时遇到问题

jyztefdp  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(117)

我正在尝试为Azure数据收集规则创建terraform。下面是我的变量声明:

variable "data_collection_rules" {
  type = map(object({
    location            = string
    name                = string
    resource_group_name = string
    data_flow = map(object({
      destinations = list(string)
      streams = list(string)
      built_in_transform = string
      output_stream = string
      transform_kql = string
    }))
    destinations = object({
      enable_azure_monitor_metrics = bool
      azure_monitor_metrics = object({
        name = string
      })
      enable_log_analytics = bool
      log_analytics = map(object({
        name = string
        log_analytics_workspace_variable_name = string
      }))
      enable_storage_blob = bool
      storage_blob = map(object({
        container_name = string
        name = string
        storage_account_variable_name = string
      }))
    })
  }))
}

variable "log_analytics_workspace_ids" {
  type = map(string)
}

variable "storage_account_ids" {
  type = map(string)
}

字符串
下面是我的模块声明:

resource "azurerm_monitor_data_collection_rule" "data_collection_rule" {
    for_each = var.data_collection_rules
    location = each.value["location"]
    name = each.value["name"]
    resource_group_name = each.value["resource_group_name"]
    dynamic "data_flow" {
      for_each = each.value["data_flow"]
      content {
        destinations = data_flow.value["destinations"]
        streams = data_flow.value["streams"]
        built_in_transform = data_flow.value["built_in_transform"]
        output_stream = data_flow.value["output_stream"]
        transform_kql = data_flow.value["transform_kql"]
      }
    }
    destinations {
      dynamic "azure_monitor_metrics" {
         for_each = each.value.destinations["enable_azure_monitor_metrics"]  == true ? [1] : []
         content {
          name = each.value.destinations.azure_monitor_metrics["name"]
         }       
      }
      dynamic "log_analytics" {
         for_each = each.value.destinations["enable_log_analytics"]  == true ? [1] : []
         content {
          name = each.value.destinations.log_analytics["name"]
          workspace_resource_id = lookup(var.log_analytics_workspace_ids, each.value.destinations.log_analytics["log_analytics_workspace_variable_name"], null)
         } 
      }
      dynamic "storage_blob" {
        for_each = each.value.destinations["enable_storage_blob"]  == true ? [1] : []
        content {
          container_name = each.value.destinations.storage_blob["container_name"]
          name = each.value.destinations.storage_blob["name"]
          storage_account_id = lookup(var.storage_account_ids, each.value.destinations.storage_blob["storage_account_variable_name"], null)
         } 
      }
    }
}


我将下面的值传递给它,其中log_analytics_workspace_variable_name和storage_account_variable_name是我用于创建log anytics工作区和存储帐户的map变量的键:

data_collection_rules = {
  data_collection_rule1 = {             
    location            = "canadacentral"  
    name                = "data_collection_rule1"   
    resource_group_name = "test-rg" 
    data_flow = {
      data_flow1 ={
        destinations = ["data-collection-destination-monitoring-matrics"] 
        streams = ["Microsoft-InsightsMetrics"] 
        built_in_transform = null 
        output_stream = null 
        transform_kql = null 
      },
      data_flow2 ={
        destinations = ["data-collection-destination-log-analytics-1"] 
        streams = ["Microsoft-Perf"] 
        built_in_transform = null 
        output_stream = null 
        transform_kql = null 
      },
      data_flow3 ={
        destinations = ["data-collection-destination-storage_blob"] 
        streams = ["Microsoft-Syslog"] 
        built_in_transform = null 
        output_stream = null 
        transform_kql = null 
      }
    }
      destinations = {
        enable_azure_monitor_metrics = true 
        azure_monitor_metrics = {
          name = "data-collection-destination-monitoring-matrics" 
        }
        enable_log_analytics = true             
        log_analytics = {
          log_analytics_1 = {
            name = "data-collection-destination-log-analytics-1" 
            log_analytics_workspace_variable_name = "log_analytics_workspace1"  
          }
        }
        enable_storage_blob = true 
        storage_blob = {
          storage_blob1 = {
            container_name = "sa-container1" 
            name = "data-collection-destination-storage_blob" 
            storage_account_variable_name = "sa1" 
          }
      }
    }
  }
}


当我运行terraform计划时,我得到以下错误:

对于“log_analytics”和“storage_blob”目标中的所有元素,我都得到了相同的错误。
我知道我调用元素的方式导致了这个问题,但我不知道如何解决这个问题。

7jmck4yq

7jmck4yq1#

根据您的规格,log_analytics的类型为:

log_analytics = map(object({
  name = string
  log_analytics_workspace_variable_name = string
}))

字符串
您的each.value.destinations.log_analytics["name"]对应于以下类型:

log_analytics = object({
  name = string
  log_analytics_workspace_variable_name = string
})


旁注:虽然这是有效的语法,但通常使用.语法访问object值,使用[""]语法访问map值。由于变量值符合类型规范(根据错误消息),因此将map键插入查找命名空间路径而不是更改类型会更快:

each.value.destinations.log_analytics["<key>"].name


其中<key>是对应于以下值的键:

object({
  name = string
  log_analytics_workspace_variable_name = string
})


请注意,您需要对具有相同问题的其他查找进行类似的修复,例如:

each.value.destinations.storage_blob["<key>"].name

相关问题