For_each and index on dynamic block

HI Guys

Can I have your tips about a way to solve my need.
I have a module to create vm which get information form a csv.
in this csv i define a nb-data-disk and name them with vm-name and the idex of the dynamic block iteration.

but the for each I used to get an index of my set is a map with 2 value

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 dynamique storage account I need one number value from my vm block iteration.

my bypass to read only 1 value by vm interation for my dynamic bloc is create an index in the csv and read it like this and it’s work :

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 colomn in the csv.
I would like to do it with the index generate by my for loop but I have no more idea.

this work 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 whith 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, but I’d also appreciate your advice.

 dynamic storage_data_disk {
    for_each = { for inst in local.nb_data_disk : index(local.nb_data_disk, inst) => inst }
    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)
    }
  }

I progressing

element([for k,v in { for inst in var.storage-foreach : index(var.storage-foreach, inst) => inst } : v ],0)

but the the second argument of element function must be the index of my for each of my vm block,

hum after will see how to do my datadisk naming :face_with_raised_eyebrow:

so bat idea it’s not like that

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

error

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

Additional information about the local vmparameters

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"
        },
    ]