I can create virtual machines from the following Terraform resource (some detail omitted because it isn't relevant to the problem and I want to keep it succinct):
...
resource "vsphere_virtual_machine" "vm-instance" {
count = length(var.vm_instances)
name = var.vm_instances[count.index].name
...
disk {
label = "disk0"
size = var.vm_instances[count.index].disk
...
}
clone {
...
}
}
So the vm_instances variable is defined in the terraform.tfvars file as follows:
vm_instances = [
{name="small_1", disk = 16},
{name="small_2", disk = 16},
{name="small_3", disk = 16},
{name="medium_1", disk = 32},
{name="medium_2", disk = 32},
{name="medium_3", disk = 32},
{name="large_1", disk = 64},
{name="large_2", disk = 64}
]
If I run apply on this configuration, everything gets deployed and works. However, if I then change my list variable (say, insert a small_4
in between small_3
and medium_1
) then I get two related errors:
Error: disk.0: virtual disk "disk0": virtual disks cannot be shrunk (old: 64 new: 32)
Error: disk.0: virtual disk "disk0": virtual disks cannot be shrunk (old: 32 new: 16)
So I understand why the error is thrown but is there a way to force Terraform to destroy such machines to then rebuild? This is the behaviour the clone variables do so why not disk sizing?
The only way I can think for resolving this is to define separate resources for each disk size (a bit of a pain and not ideal for my problem) or destroy all the machines and then build new ones (which kinda negates the point of using Terraform).
Thank you in advance for any responses.
EDIT
I attempted Matt Schuchard's commented suggestion (thanks Matt!) but I it didnt work because I don't think I can use the count
feature in the "vsphere_virtual_disk" resource. It complains it cannot find the 'Disk-#.vmdk' disks.
Also, it appears I still need a disk0 with this external appendage being the second disk (which is actually fine for my problem, but preference would be one disk).
Here is the code for the failed attempt.
resource "vsphere_virtual_disk" "my_disk" {
count = length(var.vm_instances)
vmdk_path = "Disk-${ count.index }.vmdk"
datastore = "myds"
size = var.vm_instances[count.index].disk
datacenter = "mydc"
}
and in my virtual machine resource:
resource "vsphere_virtual_machine" "vm-instance" {
...
disk {
label = "disk0"
size = 16
...
}
disk {
attach = true
path = "Disk-${ count.index }.vmdk"
label = "disk1"
unit_number = 1
}
}