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!