Home > Software engineering >  Object Merge Failure
Object Merge Failure

Time:10-27

I a trying to merge a variable definition for log analytics with some other values. Don't have the syntax correct here and when I try to use the local value (local.laws) in other areas of the code, it errors out saying "This object does not have an attribute named resource_group_name." Clearly, the merge is not doing what I want. Code below.

Variable Definition

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)
  }))
}

INPUT

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###
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

│ 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".

CodePudding user response:

I think you wanted the following:

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) 
  }
}

and then

data "azurerm_resource_group" "resource_groups" {
  for_each = local.laws
  name     = each.value.resource_group_name
}
  • Related