Selecting 1 value from complex object error

Hi,

I created the following resources:

  resource "azurerm_resource_group" "rg" {
  for_each   = var.resourcegroups
    name     = each.value.name
    location = each.value.location
}

and in resourcegroups.auto.tfvars

resourcegroups = {
  rg1 = {
    name = "euw-rg-fb-generic-p"
    location = "westeurope"
  }

  rg2 = {
    name = "euw-rg-fb-applicationdelivery-p"
    location = "westeurope"
  }
}

when i want to reference the specific resourcegroup rg1 in another resource

resource "azurerm_subnet" "sn_hub" {
  for_each               = var.network.subnets
    name                 = each.value.name
    resource_group_name  = azurerm_resource_group.rg[var.resourcegroups.rg1.name].name
    virtual_network_name = azurerm_virtual_network.vnet_hub.name
    address_prefixes     = each.value.address_prefixes
  }

i get the below error

  on main.tf line 39, in resource "azurerm_virtual_network" "vnet_hub":
  39:   resource_group_name = azurerm_resource_group.rg[var.resourcegroups.rg1.*.name].name
    |----------------
    | azurerm_resource_group.rg is object with 2 attributes
    | var.resourcegroups.rg1 is object with 2 attributes

The given key does not identify an element in this collection value: string
required.

What i want to accomplish is to reference a specific value in combination with a implicit reference on a dynamic way.

Hi @dkooll,

The error message you shared seems to be referring to a different resource than the one in the configuration snippet you shared: the error refers to azurerm_virtual_network.vnet_hub while you shared azurerm_subnet.sn_hub.

Could you please share a fuller example of all of the parts of the configuration that are involved in what you are trying to do here? That will hopefully help to find the answer.

Hi, @apparentlymart

sorry, you are right

this is main.tf

#----------------------------------------------------------------------------------------
# Resource groups
#----------------------------------------------------------------------------------------

resource "azurerm_resource_group" "rg" {
  for_each   = var.resourcegroups
    name     = each.value.name
    location = each.value.location
}

#----------------------------------------------------------------------------------------
# Vnet / subnets
#----------------------------------------------------------------------------------------

resource "azurerm_virtual_network" "vnet_hub" {
  name                = var.network.vnet.name
  address_space       = var.network.vnet.address_space
  location            = var.network.vnet.location
  resource_group_name = azurerm_resource_group.rg[var.resourcegroups.rg1].name
}

this is resoursegroups.auto.tfvars

resourcegroups = {
  rg1 = {
    name = "euw-rg-fb-generic-p"
    location = "westeurope"
  }

  rg2 = {
    name = "euw-rg-fb-applicationdelivery-p"
    location = "westeurope"
  }
}

this is variables.tf

variable "resourcegroups" {
  description = ""
}

and this is the error i got

Error: Invalid index

      on main.tf line 39, in resource "azurerm_virtual_network" "vnet_hub":
      39:   resource_group_name = azurerm_resource_group.rg[var.resourcegroups.rg1].name
        |----------------
        | azurerm_resource_group.rg is object with 2 attributes
        | var.resourcegroups.rg1 is object with 2 attributes

The given key does not identify an element in this collection value: string
required.

so i specified multiple resourcegroups in a tfvars file and i want to pick one resourcegroup in this line

resource_group_name  = azurerm_resource_group.rg[var.resourcegroups.rg1.name].name 

and i want get the name of the resourcegroup also from the tfvars file as a variable. Basicly i want to configure everything using the complex object variable.

Hi @dkooll,

Terraform is returning this error because in your configuration here you’ve used var.resourcegroups.rg1 as an index of azurerm_resource_group.rg, which fails because that is an object, not a string.

The follow-up example you shared, using var.resourcegroups.rg1.name instead, is the solution I would’ve suggested: that way the index will be a string, as expected. Did you get a different error when you tried that?

Hi, @apparentlymart

Now i got

Error: Invalid index

  on main.tf line 39, in resource "azurerm_virtual_network" "vnet_hub":
  39:   resource_group_name = azurerm_resource_group.rg[var.resourcegroups.rg1.name].name
    |----------------
    | azurerm_resource_group.rg is object with 2 attributes
    | var.resourcegroups.rg1.name is "euw-rg-fb-generic-p"

The given key does not identify an element in this collection value.

Hmm looking again at your examples I see that the keys in var.resourcegroups don’t match the name values. Is there a reason why that must be true? This would be easier to accomplish if your resource group map keys and your resource group names were the same. There are some ways to make this work if you do need them to be different, but I think it’s best to explore the simpler option first before getting into that…

Ok, what would be the simple option