'Unable to get memory or num_cpus from template in Terraform VSphere

While trying to allocate memory and num_cpus for a virtual machine in VSphere, I am trying to extract the memory and num_cpus from the template. But I am getting an error while trying to do so. Here is my resource:

data "vsphere_virtual_machine" "template" {
name = var.vm-template-name
datacenter_id = data.vsphere_datacenter.dc.id
}

resource "vsphere_virtual_machine" "vm" {
  count = var.vm-count
  name = "${var.vm-name}-${count.index + 1}"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id = data.vsphere_datastore.datastore.id
  folder = var.vm-folder
  #####Problem Area############################################
  num_cpus = data.vsphere_virtual_machine.template.num_cpus
  memory = data.vsphere_virtual_machine.template.memory
  #############################################################
  guest_id = data.vsphere_virtual_machine.template.guest_id
  scsi_type = data.vsphere_virtual_machine.template.scsi_type
  firmware = data.vsphere_virtual_machine.template.firmware

  network_interface {
    network_id = data.vsphere_network.network.id
    adapter_type = data.vsphere_virtual_machine.template.network_interface_types[0]
  }

  disk {
      label            = "disk0"
      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
    }

  clone {
    template_uuid = data.vsphere_virtual_machine.template.id

    customize {
        windows_options {
          auto_logon_count = 2
          computer_name = "HarshitDev"
          organization_name = "Philips"
        }
      }
  }
}

And here is the error I am getting when I run 'terraform validate':

Error: Unsupported attribute

on main.tf line 39, in resource "vsphere_virtual_machine" "vm":
39: num_cpus = data.vsphere_virtual_machine.template.num_cpus

This object has no argument, nested block, or exported attribute named "num_cpus".

Error: Unsupported attribute

on main.tf line 40, in resource "vsphere_virtual_machine" "vm":
40: memory = data.vsphere_virtual_machine.template.memory

This object has no argument, nested block, or exported attribute named "memory".



Solution 1:[1]

num_cpus has not been exposed in the vsphere_virtual_machine resource.

Someone has started work on it (see here), but it is yet to be merged.

Solution 2:[2]

You can extract the memory and CPU of the given template through using the same data sources as you are using. However it looks like you are using an older version of the provider plugin for vSphere.

Instead of using your current version, switch to the following version:

terraform {
  required_providers {
  vsphere = {
    source = "hashicorp/vsphere"
    version = "2.1.1"
  }
 }
}

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Matthew Frost
Solution 2 ouflak