Error: could not find resource pool ID "<resource_pool>"

Using the VSphere Provider I’m trying to use Terraform to create multiple VM.

I’m using a variable “deploy”:

variable "deploy" {
  type    = list
  default = []
}

and created a list of values for the resource

resource "vsphere_virtual_machine" "deploy" {
  count = length(var.deploy)
  name  = var.deploy[count.index]["hostname"]

  resource_pool_id     = var.deploy[count.index]["resource_pool"]
  num_cpus             = var.deploy[count.index]["CPU_Count"]
  num_cores_per_socket = var.deploy[count.index]["cores"]
  memory               = var.deploy[count.index]["RAM"]
  annotation           = var.deploy[count.index]["Tag"]
  folder               = var.deploy[count.index]["folder_path"]

with the following tfvars:

deploy  = [
    {
        hostname       = "test"
#        template       = "ubuntu-server-18.04.3-LTS"
        AssetTag       = "1234322"  # This will create an entry in the advanced settings of the VM
        CPU_Count      = "1"
        cores          = "2"
        RAM            = "4000"
        Tag            = "Test"  
        disk0          = "30"
        folder_path    = "<path>"
        resource_pool  = "<resource_pool>"
        vlan           = "<vlan_id>"
        domain         = "<domain>"
        ip             = "10.64.x.x"
        netmask        = "23"
        gateway        = "10.64.x.1"
        dns1           = "10.x.x.11"
        dns2           = "10.x.x.12"
    }
]

but when I execute terraform plan I have the following error:

Error: could not find resource pool ID "<resource_pool>": ServerFaultCode: The object 'vim.ResourcePool:<resource_pool>' has already been deleted or has not been completely created

  on main.tf line 39, in resource "vsphere_virtual_machine" "deploy":
  39: resource "vsphere_virtual_machine" "deploy" {

If I use a general variable assignment in terraform.tfvars (and not in the deploy section) and I change the entry from:
resource_pool_id = var.deploy[count.index][“resource_pool”]
to
resource_pool_id = data.vsphere_resource_pool.pool.id

I have no error.

Any idea why?

Hello! Welcome to the forum!

Have you taken a look at Terraform’s version of for_each? I think that may be a cleaner way to achieve what you’re trying to do

Hi salaxander, for_each seems a good solution but the docs on terraform are ridiculous and all the examples I’m finding are not functioning.

There is a solid example?