Using Count Does Not Remove Resources From Infrastructure On Apply

I’m fairly new to Terraform, and I’m attempting to follow the instructions for the destroy-time provisioner count-based execution method. However, when I set count = 0 in my resource definition, it appears that resources are removed from the state file successfully, but the previously created resources aren’t removed from the infrastructure.

I’m using Terraform v0.14.4 for testing right now. The providers being used are Nutanix and Infoblox. Here’s the code that’s in the VM definition .tf file:

# Define VM resource settings for creation:
variable "vm_spec" {
  type = map(any)
  default = {
    "is_enabled"   = 0
    "os_image"     = "Server 2019 Template"
    "num_cpus"     = 4
    "mb_ram"       = 8192
    "network_vlan" = "terraform-vlan-4"
    ...

Here is the relevant code for the VM provisioner module:

# Build Nutanix VM based on provided variables:
resource "nutanix_virtual_machine" "vm" {
  count                = var.is_enabled ? 1 : 0
  name                 = var.vm_name
  num_vcpus_per_socket = 1
  num_sockets          = var.num_cpus
  memory_size_mib      = var.mb_ram
...

I have tried using “count = var.is_enabled”, as well as the “count = var.is_enabled ? 1 : 0”. Neither option seems to deprovision resources when the “is_enabled” variable is set to 0 in the VM definition. However, when I look at the state file using “terraform state list” when “is_enabled” is set to 0, the VM resource doesn’t show in the state file. Running a “terraform plan” or “terraform destroy” with that state says that there are 0 changes to apply, even though the resources still exist in the infrastructure.

I’m sure that I’m doing something incorrectly to get the resources to deprovision, but I can’t figure out what it is. Can anyone point me in the right direction?

I can add more of the Terraform code being used if more info is needed.

Thanks!

I got this to work correctly. It seems that there were two issues that caused the problem.

First, it appears that a VM created with the Nutanix provider can only be modified once if the guest_customization_sysprep argument is set. Modifying the VM definition after creation causes it to be destroyed and rebuilt, since that argument can only be applied on create. That’s why there ended up being another VM instance in the infrastructure. Putting the sysprep customization into the template image and removing the guest_customization_sysprep block allowed VMs to be destroyed when their count was set to 0.

The second issue was that the apply was being run through GoCD, and I’d forgotten to change the location of the state file. So, when count was set to 0 and the checkout was done, the state file got removed. Without the state file Terraform could not see any actions to apply and the resources were never deprovisioned. Moving the state file out of the checkout directory with the “local” backend solved this problem. I’ll move it to remote state when done testing.

Thanks to everyone who checked out the topic!