'terraform For_each and index in dynamic block

Can I have your tips about a way to solve my need?

I have a module to create vm which get information from a .csv. In this .csv file, I define a nb-data-disk and name them with vm-name and the index of the dynamic block iteration.

But the for each I used to get an index of my set is a map with 2 values.

local.nb_data_disk output this ["1","2"] corresponding of datadisk nb from 2 distinct row from the csv

So to get the index for name data disk I used this :

for_each = { for inst in local.nb_data_disk : index(local.nb_data_disk, inst) => inst }

output  = {
  "0" = "1"
  "1" = "2"
}

but for my dynamic storage account I need one number value from my vm block iteration.

My bypass to read only 1 value by vm iteration for my dynamic bloc is create an index in the csv and read it like this and it works:

for_each = range(element([ for inst in local.vmparameters : inst.nb_data_disk ],each.value.index))

But I would like to do it without this bypass which are not flexible and require an index column in the csv.

I would like to do it with the index generate by my for loop but I have no more idea.

This works for data disk naming but failed because dynamic block cant iterate through a map. and if I used by bypass the disk naming not iterate with the number of iteration.

So I'm stuck here

Below here the dynamic block with the for each to get the index for the naming but not got to get the disk number value.

As I write, I'm thinking about something, and try it as you can see below but I'd also appreciate your advice.

dynamic storage_data_disk {
    for_each = element([for k,v in { for inst in local.nb_data_disk : index(local.nb_data_disk, inst) => inst } : v ],each.key)
    content {
      name              = format("${each.key}-datadisk%02d",storage_data_disk.key)
      create_option     = "Empty"
      lun               = 0
      disk_size_gb      = element([ for inst in local.vmparameters : inst.data_disk_size_gb ],each.value.index)
      managed_disk_type = element([ for inst in local.vmparameters : inst.data_sa_type ],each.value.index)
    }
  }

This solved by an error evident because my each.key in the for loop is a string

Error: Invalid function argument

on azure-vm/main.tf line 142, in resource "azurerm_virtual_machine" "vm-windows":
142: for_each = element([for k,v in { for inst in local.nb_data_disk : index(local.nb_data_disk, inst) => inst } : v ],each.key)

each.key is "eucceistot01"

Invalid value for "index" parameter: a number is required.

And also I thing the name will be not good as his each key refer to the key of the for loop

name = format("${each.key}-datadisk%02d",storage_data_disk.key)

Some idea how I can achieve this.

For more information the value of local.vmparameters is a map from the csv file.

locals {   
   vmparameters = csvdecode(file(var.pathcsv))  
   vm_os_simple = element([ for inst in local.vmparameters : inst.vm_os_simple ],1)   
   os_type = element([ for inst in local.vmparameters : inst.os_type],1)   
   enable_accelerated_networking = element([ for inst in local.vmparameters : inst.enable_accelerated_networking],1)   
   nb_data_disk = [ for inst in local.vmparameters : inst.nb_data_disk ]

output of csv decode : 
+ csv = [
      + {
          + data_disk_size_gb             = "30"
          + data_sa_type                  = "Standard_LRS"
          + enable_accelerated_networking = ""
          + identity_ids                  = ""
          + identity_type                 = ""
          + index                         = "1"
          + nb_data_disk                  = "1"
          + os_disk_size_gb               = "70"
          + os_type                       = "linux"
          + vm_hostname                   = "eucceistot01"
          + vm_os_simple                  = "CentOS"
          + vm_size                       = "standard_d4s_v4"
        },
      + {
          + data_disk_size_gb             = "30"
          + data_sa_type                  = "Standard_LRS"
          + enable_accelerated_networking = ""
          + identity_ids                  = ""
          + identity_type                 = ""
          + index                         = "1"
          + nb_data_disk                  = "1"
          + os_disk_size_gb               = "127"
          + os_type                       = "windows"
          + vm_hostname                   = "eucceistot02"
          + vm_os_simple                  = "WindowsServer"
          + vm_size                       = "standard_d4s_v4"
        },
    ]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source