我尝试将日志分析的变量定义与其他一些值合并。这里的语法不正确,当我尝试在代码的其他区域使用本地值 (local.laws) 时,它会错误地显示“此对象没有名为 resource_group_name 的属性”。显然,合并并没有达到我想要的效果。代码如下。
变量定义
variable "log_analytics_workspace" {
description = "A list of log analytics workspaces and their arguments."
type = list(object({
name = string
resource_group_name = string
location = string
sku = string #Free, PerNode, Premium, Standard, Standalone, Unlimited, CapacityReservation, and PerGB2018. Defaults to PerGB2018.
retention_in_days = number #Possible values are either 7 (Free Tier only) or range between 30 and 730.
daily_quota_gb = number #The workspace daily quota for ingestion in GB. Defaults to -1 (unlimited) if omitted.
reservation_capacity_in_gb_per_day = number
tags = map(string)
}))
}
输入
log_analytics_workspace = [
{
name = "law-eastus-dev-01"
resource_group_name = "rg-eastus-dev-01"
location = "eastus"
sku = "PerGB2018"
retention_in_days = 30
daily_quota_gb = 10
reservation_capacity_in_gb_per_day = 100
tags = {
"law" = "DEV"
}
}
]
本地人
###LOCALS###
locals {
laws = {
for_each = { for law in var.log_analytics_workspace : law.name => merge(
{
internet_ingestion_enabled = false
internet_query_enabled = false
cmk_for_query_forced = false
}) }
}
}
##Data Source for Resource Groups
data "azurerm_resource_group" "resource_groups" {
for_each = local.laws
name = each.value.resource_group_name
}
错误
│ Error: Unsupported attribute
│
│ on modules/loganalytics/main.tf line 17, in data "azurerm_resource_group" "resource_groups":
│ 17: name = each.value.resource_group_name
│ ├────────────────
│ │ each.value is object with 1 attribute "law-eastus-dev-01"
│
│ This object does not have an attribute named "resource_group_name".
最佳答案
我认为您想要以下内容:
locals {
laws = { for law in var.log_analytics_workspace : law.name => merge(
{
internet_ingestion_enabled = false
internet_query_enabled = false
cmk_for_query_forced = false
}, law)
}
}
然后
data "azurerm_resource_group" "resource_groups" {
for_each = local.laws
name = each.value.resource_group_name
}
关于azure - 对象合并失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74215403/