azure - 对象合并失败

标签 azure terraform

我尝试将日志分析的变量定义与其他一些值合并。这里的语法不正确,当我尝试在代码的其他区域使用本地值 (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/

相关文章:

Azure 存储(Blob、队列、表)使用 for_each 和 locals 登录 Terraform

Terraform:如何将多个子网关联到路由表?

Terraform - 使用 splat 运算符不工作从模块创建输出

azure - 功能配置已更改原因 'Get Web App Publishing Profile'

azure - New-AzScheduledQueryRule 返回 BadRequest

azure - CAS azure saml2 即时身份验证问题太旧或将来

azure - 无法在同一 ARM 模板部署中创建和引用 keyvault secret

amazon-s3 - 如何授予 lambda 权限将文件上传到 `terraform` 中的 s3 存储桶?

terraform - 尝试使用 etag 时找不到文件

使用 CI 时 Azure Functions 修改时间触发间隔