Multiple instances in different files

Hello, I want to create and manage multiple virtual instances with Terraform
So provider I use VMware vSphere.

This is my current script:

resorce file:

...
resource "vsphere_virtual_machine" "server" {
  for_each = var.virtual_machines

  # VM-Name
  name             = each.key
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id

  # System
  firmware  = "efi"
  guest_id  = data.vsphere_virtual_machine.template.guest_id
  scsi_type = data.vsphere_virtual_machine.template.scsi_type

  # CPU
  num_cpus               = each.value.system_cores
  num_cores_per_socket   = each.value.system_cores_per_socket
  cpu_hot_add_enabled    = true
  cpu_hot_remove_enabled = true

  # Memory
  memory                 = each.value.system_memory
  memory_hot_add_enabled = true

  # Network
  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = "e1000e"
  }

  # Storage
  # Drive 0 (C)
  disk {
    label            = "disk0"
    unit_number      = 0
    size             = data.vsphere_virtual_machine.template.disks.0.size
    eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
    thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
  }

  # Drive 1 (D)
  disk {
    label            = "disk1"
    unit_number      = 1
    size             = each.value.system_disk1_size
    eagerly_scrub    = data.vsphere_virtual_machine.template.disks.1.eagerly_scrub
    thin_provisioned = data.vsphere_virtual_machine.template.disks.1.thin_provisioned
  }

  # Template clone and OS settings
  clone {
    template_uuid = data.vsphere_virtual_machine.template.id

    customize {
      windows_options {
        computer_name         = each.value.system_name
        admin_password        = random_password.password.result
        join_domain           = each.value.system_domain
        domain_admin_user     = each.value.system_domain_admin_user
        domain_admin_password = each.value.system_domain_admin_password
        auto_logon            = true
      }

      network_interface {
        ipv4_address    = each.value.system_ipv4_address
        ipv4_netmask    = each.value.system_ipv4_netmask
        dns_server_list = each.value.system_dns_server_list
      }

      ipv4_gateway = each.value.system_ipv4_gateway
    }
  }
}

.tfvars file:

vsphere_user                     = "administrator@vsphere.local"
vsphere_password                 = "#Password"
vsphere_server                   = "vsphere.server"
vsphere_datacenter               = "Datacenter"
vsphere_datastore                = "Storage_1"
vsphere_compute_cluster          = "Cluster_1"
vsphere_network                  = "Network_1"
vsphere_virtual_machine_template = "Template_Microsoft_Windows_Server_2019_x64_english"

virtual_machines = {
  server-1 = {
    system_cores            = 2
    system_cores_per_socket = 2
    system_memory           = 2048
    system_ipv4_address     = "172.22.15.11"
    # ...
  }
  server-2 = {
    system_cores            = 2
    system_cores_per_socket = 2
    system_memory           = 2048
    system_ipv4_address     = "172.22.15.12"
    # ...
  }
}

Is there a way to define the parameters of each instance in its own .tfvars file instead of in a map?
The problem is, when I try that, the new instance overwrites the existing instance.

Many thanks for the help.

You can, but each instance/tfvars file would need to be setting a different variable.

So instead of setting the “virtual_machines” variable as a map of parameters you could have something like “virtual_machine_1”, “virtual_machine_2”, etc. To make things easier to handle you could then merge all those variables together as a local variable.

Thank you for your prompt reply! Could you perhaps create a small example of this code?

Hi! where is system name, i have the samecode with you but i didnt see any problem