#StackBounty: #terraform #terraform-provider-vsphere Terraform vsphere_tag unwanted deletion

Bounty: 100

I’m using Terraform to deploy some dev and prod VMs on our VMware vCenter infrastructure and use vsphere tags to define responsibilities of VMs. I therefore added the following to the (sub)module:

  resource "vsphere_tag" "tag" {
    name        = "SYS-Team"
    category_id = "Responsibility"
    description = "Systems group"
  }

  ...

  resource "vsphere_virtual_machine" "web" {
    tags             = [vsphere_tag.tag.id]
    ...
  }

Now, when I destroy e.g. the dev infra, it also deletes the prod vsphere tag and leave the VMs without the tag.

I tried to skip the deletion with the lifecycle, but then I would need to separately delete each resource which I don’t like.

lifecycle {
  prevent_destroy = true
}

Is there a way to add an existing tag without having the resource managed by Terraform? Something hardcoded without having the tag included as a resource like:

  resource "vsphere_virtual_machine" "web" {
    tags             = [{
      name        = "SYS-Team"
      category_id = "Responsibility"
      description = "Systems group"
      }
    ]
    ...
  }


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.