Error: error creating virtual machine: Invalid configuration for device '1'

Hi All,

I started using terraform for a few days now.
My first goal is to create a simple vm on a cluster.
Once i figured that out, i like to create a template to auto deploy mulitipe vm’s.

I think i figured out the basics and was able to create folders and resource pools.
But when creating a blank vm with min req. presets, i got stuck.

When creating a new vm with terraform, do you always need to start from a template?

This is my code from the main file:

terraform {
required_providers {
vsphere = {
source = “hashicorp/vsphere”
version = “2.6.1”
}
}
}

provider “vsphere” {
user = var.vsphere_user
password = var.vsphere_password
vsphere_server = var.vsphere_vcenter
allow_unverified_ssl = true
}

Define VMware vSphere

data “vsphere_datacenter” “dc” {
name = var.vsphere-datacenter
}

data “vsphere_compute_cluster” “cluster” {
name = var.vsphere-cluster
datacenter_id = data.vsphere_datacenter.dc.id
}

data “vsphere_resource_pool” “pool” {
name = var.vsphere_resource_pool
datacenter_id = data.vsphere_datacenter.dc.id
}

data “vsphere_datastore” “ds” {
name = var.vm-datastore
datacenter_id = data.vsphere_datacenter.dc.id
}

data “vsphere_network” “network” {
name = var.vm-network
datacenter_id = data.vsphere_datacenter.dc.id
}

resource “vsphere_virtual_machine” “vm” {
name = “test”
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.ds.id

num_cpus = var.cpu
num_cores_per_socket = var.cores-per-socket
memory = var.ram
guest_id = var.vm-guest-id

network_interface {
network_id = data.vsphere_network.network.id
}

disk {
label = “disk0”
size = var.disksize
}
}

Output:

C:\Tools\Terraform\Git Templates\terraform-vsphere-vsphere_vm-test - fixed - host>terraform apply -auto-approve
data.vsphere_datacenter.dc: Reading…
data.vsphere_datacenter.dc: Read complete after 0s [id=datacenter-3]
data.vsphere_network.network: Reading…
data.vsphere_compute_cluster.cluster: Reading…
data.vsphere_host.host: Reading…
data.vsphere_datastore.ds: Reading…
data.vsphere_network.network: Read complete after 0s [id=network-56]
data.vsphere_datastore.ds: Read complete after 0s [id=datastore-16]
data.vsphere_compute_cluster.cluster: Read complete after 0s [id=domain-c8]
data.vsphere_host.host: Read complete after 0s [id=host-12]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

  • create

Terraform will perform the following actions:

vsphere_virtual_machine.vm will be created

  • resource “vsphere_virtual_machine” “vm” {
    • annotation = (known after apply)

    • boot_retry_delay = 10000

    • change_version = (known after apply)

    • cpu_limit = -1

    • cpu_share_count = (known after apply)

    • cpu_share_level = “normal”

    • datastore_id = “datastore-16”

    • default_ip_address = (known after apply)

    • ept_rvi_mode = “automatic”

    • extra_config_reboot_required = true

    • firmware = “bios”

    • force_power_off = true

    • guest_id = “ubuntu64Guest”

    • guest_ip_addresses = (known after apply)

    • hardware_version = 15

    • host_system_id = (known after apply)

    • hv_mode = “hvAuto”

    • id = (known after apply)

    • ide_controller_count = 2

    • imported = (known after apply)

    • latency_sensitivity = “normal”

    • memory = 2048

    • memory_limit = -1

    • memory_share_count = (known after apply)

    • memory_share_level = “normal”

    • migrate_wait_timeout = 30

    • moid = (known after apply)

    • name = “test”

    • num_cores_per_socket = 1

    • num_cpus = 2

    • power_state = (known after apply)

    • poweron_timeout = 300

    • reboot_required = (known after apply)

    • resource_pool_id = “resgroup-9”

    • run_tools_scripts_after_power_on = true

    • run_tools_scripts_after_resume = true

    • run_tools_scripts_before_guest_shutdown = true

    • run_tools_scripts_before_guest_standby = true

    • sata_controller_count = 0

    • scsi_bus_sharing = “noSharing”

    • scsi_controller_count = 1

    • scsi_type = “pvscsi”

    • shutdown_wait_timeout = 3

    • storage_policy_id = (known after apply)

    • swap_placement_policy = “inherit”

    • tools_upgrade_policy = “manual”

    • uuid = (known after apply)

    • vapp_transport = (known after apply)

    • vmware_tools_status = (known after apply)

    • vmx_path = (known after apply)

    • wait_for_guest_ip_timeout = -1

    • wait_for_guest_net_routable = true

    • wait_for_guest_net_timeout = -1

    • disk {

      • attach = false
      • controller_type = “scsi”
      • datastore_id = “”
      • device_address = (known after apply)
      • disk_mode = “persistent”
      • disk_sharing = “sharingNone”
      • eagerly_scrub = false
      • io_limit = -1
      • io_reservation = 0
      • io_share_count = 0
      • io_share_level = “normal”
      • keep_on_remove = false
      • key = 0
      • label = “test-disk”
      • path = (known after apply)
      • size = 50
      • storage_policy_id = (known after apply)
      • thin_provisioned = true
      • unit_number = 0
      • uuid = (known after apply)
      • write_through = false
        }
    • network_interface {

      • adapter_type = “vmxnet3”
      • bandwidth_limit = -1
      • bandwidth_reservation = 0
      • bandwidth_share_count = (known after apply)
      • bandwidth_share_level = “normal”
      • device_address = (known after apply)
      • key = (known after apply)
      • mac_address = (known after apply)
      • network_id = “network-56”
        }
        }

Plan: 1 to add, 0 to change, 0 to destroy.
vsphere_virtual_machine.vm: Creating…

│ Error: error creating virtual machine: Invalid configuration for device ‘1’.

│ with vsphere_virtual_machine.vm,
│ on main.tf line 42, in resource “vsphere_virtual_machine” “vm”:
│ 42: resource “vsphere_virtual_machine” “vm” {

[Best Guess]

The error message “Invalid configuration for device ‘1’” typically indicates there’s a problem with the way the virtual hardware is configured for the VM in your Terraform code. Since the message isn’t very specific, the root cause could be one of several factors related to the VM’s configuration. Here are a few things to check:

  1. Network Interface Configuration: Ensure that the network_interface block in your Terraform configuration is correctly set up. The network_id should correspond to a valid network in your vSphere environment. Misconfigurations here are common culprits.
  2. Disk Configuration: Look at the disk block to make sure that the configuration is valid. This includes the label, size, and other parameters. Also, ensure that the datastore specified has enough space and is properly accessible.
  3. Resource Pool and Datastore IDs: Verify that the resource_pool_id and datastore_id are correct. These should point to existing and accessible resource pools and datastores in your vSphere environment.
  4. Compatibility Issues: Check if the guest OS type specified in guest_id and other hardware configurations like num_cpus, memory, etc., are supported by your vSphere version. Sometimes, specifying a hardware version (hardware_version) not supported by your vSphere can lead to this error.
  5. Template Requirement: While not always necessary, starting from a VM template can sometimes help avoid configuration issues, especially for more complex setups. If your environment typically uses templates for VM deployment, consider using one as a base for your Terraform configuration.

To narrow down the exact cause, you might need to iteratively comment out sections of your VM resource configuration and run terraform apply to see if the error persists, which can help identify the problematic part of your configuration.

Hi,

I did not find any issues, but just to be sure, i created a new main file with easy simple config.

Now i get a different error:

error powering on virtual machine: powerOn task for vm “/DC/vm/foo” failed: Unable to enumerate all disks.

Cluster info:
vCenter 8.0.2 Build 22617221
vSphere 8.0.2 Build 2238047