How to destroy a specific element from a resource

Current Terraform Version

terraform -v
v0.12.19

Use-cases

i have created many vms into one resource and i want to delete only one vm tagged by its key

terraform state list
 cloudplatform_compute_instance.vms["AmbariMaas"]

I am using a custommized provider

resource "cloudplatform_compute_instance" "vms" {
    for_each = {
      for server in local.all_servers : "${server.name}" => server
    }
    name = each.value.name
    flavor_ref = data.cloudplatform_compute_flavor.flavor[each.value.flavor].id
    port = cloudplatform_compute_port.fixed_ip[each.value.port].id
    image_ref = data.cloudplatform_compute_image.test.id
    key_name = var.ssh_key_name
    availability_zone = var.az
    description = ""
    tags = each.value.tags
    server_group = cloudplatform_compute_server_group.cluster[each.value.group].id

resource "cloudplatform_compute_volume" "volumes" {
    for_each = {
      for volume in local.all_volumes : "${volume.suffix}.${volume.server}" => volume
    }
    name = format("%s-%s", each.value.server, each.value.suffix )
    size = each.value.size
    volume_type = "tiefighter"
    availability_zone = var.az
}

resource "cloudplatform_compute_volume_attachement" "mount" {
    for_each = {
      for volume in local.all_volumes : "${volume.suffix}.${volume.server}" => volume
    }
    volume_id = cloudplatform_compute_volume.volumes[each.key].id
    server_id = cloudplatform_compute_instance.vms[each.value.server].id
}

in terraform.tfvars

servers_definitions = {
    AmbariMaas = {
      names =  ["AmbariMaas"]
      tags = [ "MaasDev", "hdf-masternode-01" ]
      group = "maas_master_group"
      flavor = "Large-mem16 4vCPU-16GB"
      volumes =  {
        volume1 = {
          "suffix" =  "hadoop"
          "size" = "450"
          "moun_dir" = "/DATA/hadoop"
        }
        volume2 = {
          "suffix" =  "log"
          "size" = "50"
          "moun_dir" = "/DATA/log"
        }
      }
    }
}

in vars.tf

locals {
  all_volumes = flatten([
    for server in var.servers_definitions : [
      for name in server.names : [
        for volume in server.volumes : {
          size = volume.size
          suffix  = volume.suffix
          server = name
        }
      ]
    ]
  ])
  all_servers = flatten([
    for server in var.servers_definitions : [
      for name in server.names : [
        {
          name =  name
          port =  format("%sPort", name)
          tags =  server.tags
          flavor =  server.flavor
          group = server.group
          volumes = server.volumes
        }
      ]
    ]
  ])

}

Attempted Solutions

I have tried this command
terraform plan -destroy -target 'cloudplatform_compute_instance.vms["AmbariMaas"]'
But it will planify to destroy the vm as well as its all resource dependecies (volumes and mount) of other servers.
when i try to destroy a specific element of mount resource, it is working because it does not have dependencies.
Do you recommend another method to define my global architecture of servers?

Hi @slim-azaiz,

The usual way to destroy something is to remove it from the configuration and run terraform apply. In your case here, that would mean removing the AmbariMaas element from server_definitions, which will in turn cause the cloudplatform_compute_instance.vms["AmbariMaas"] to be planned for destruction.

If your goal is to keep it in the configuration but to recreate it, then you can use the terraform taint command, which tells Terraform that you consider the instance to be “broken” in some way (it doesn’t matter if it actually is “broken”) and then the next plan will include destroying and then recreating that instance.

terraform taint 'cloudplatform_compute_instance.vms["AmbariMaas"]'
terraform plan

The terraform destroy and terraform plan -destroy commands are there for totally destroying everything associated with a configuration, not for destroying individual objects. Although the -target option does sometimes let you achieve that, this is not the intended use of -target and so you can’t rely on it as a way to destroy individual objects without modifying the configuration.

Hello,

Thanks for your response.
My goal is to destroy the resource and its dependencies.
In other words:

cloudplatform_compute_instance.vms[“AmbariMaas”]
cloudplatform_compute_volumes.volume[“AmbariMaas”]
cloudplatform_compute_volume_attachement[“AmbariMaas”]

But the target option destroys the vm and the whole volume resource
as well as the whole attachement resource

cloudplatform_compute_instance.vms[“AmbariMaas”]
cloudplatform_compute_volumes.volume
cloudplatform_compute_volume_attachement

Regards,
Slim