Need help with per VM datastore selection on deployment

Hi everybody

We have cobbled together a Terraform script to deploy a customer’s linux VMs. Now the customer has grown from one datastore to two and I’m running into a brick wall.

My target is to be able to put the datastore name into X.auto.tfvars for each single vm.

I had tried defining a map variable matching the paths in vsphere to these names:

datastores = {
customer_X_glb1 = “EMC Umgebung/customers/X/customer_X_glb1”
customer_X_glb2 = “EMC Umgebung/customers/X/customer_X_glb2”
}

Then in data_sources.tf I defined this:

data “vsphere_datastore” “customer_X_glb1” {
name = var.datastores[“customer_X_glb1”]
datacenter_id = data.vsphere_datacenter.dc.id
}

data “vsphere_datastore” “customer_X_glb2” {
name = var.datastores[“customer_X_glb2”]
datacenter_id = data.vsphere_datacenter.dc.id
}

And in the vm resource in resources.tf:

datastore_id = data.vsphere_datastore.[element(var.Prod_VMs,count.index).datastore].id

This is not allowed it seems.

Other things I’ve tried involved putting the per vm reference directly in data_sources.tf but that didn’t work either.

I’m sure this is a simple and obvious problem but I’m afraid Terraform and I just aren’t compatible…

Can anybody help?