terraformhardwarevsphere

How to create multiple vsphere VMs of different name, different CPUs and memory requirement using terraform using for_each


I have a scenario-

i have to provision multiple vsphere vms of different users with different cpus and memory requirement i am trying below code but its failing.

my terraform tfvars looks like -

  example_users = name1 {
  hardware = {cpus = 8
    memory = 10
}
},
 name2= {
   hardware = { cpus =9
   memory = 8
} 
},

and so-on

main.tf=

    for_each = var.example_users 
    cpus = each.value.hardware.cpus
    memory = each.value.harware.memory 

can someone help


Solution

  • Terraform is looking for maps that are key:value pairs, so you could update your formatting a bit and likely have a functional config. I would also remove the nested hardware map as well since Terraform would need additional logic to understand the inner information.

    tfvars:

    example_users = {
      name1 = {
        cpus   = 8
        memory = 10
      }
      name2 = {
        cpus   = 9
        memory = 8
      }
    }
    

    main.tf:

    for_each = var.example_users 
    cpus = each.value["cpus"]
    memory = each.value["memory"]