Destroy time provisioner not working

Hi,

i am using terraform version 0.11.6 and aws as a provider.

i am trying to execute remote-exec provisoner during the terraform destroy.

But unfortunately destruction is completing without executing the provisioners.

resource “null_resource” “cluster_destroy” {
provisioner “remote-exec” {
inline = [“whoami; pwd” ]
when = “destroy”
connection {
host = “{element(aws_instance.app_cluster.*.private_ip, count.index)}" type = "ssh" user = "{var.ec2_user}”
}
}
}
###terraform logs###

module.app_cluster.null_resource.cluster_destroy: Destroying… (ID: 4352914941946205343)
module.app_cluster.null_resource.cluster_destroy: Destroying… (ID: 8245031149560721486)
module.app_cluster.null_resource.cluster_destroy: Destroying… (ID: 5921619339057075038)
module.app_cluster.null_resource.cluster: Destroying… (ID: 8029620455479941004)
module.app_cluster.null_resource.cluster_destroy: Destroying… (ID: 8355076874575608750)
module.app_cluster.null_resource.cluster: Destroying… (ID: 6145362674166048035)
module.app_cluster.null_resource.cluster: Destruction complete after 0s
module.app_cluster.null_resource.cluster_destroy: Destruction complete after 0s
module.app_cluster.null_resource.cluster: Destruction complete after 0s
module.app_cluster.null_resource.cluster_destroy: Destruction complete after 0s
module.app_cluster.null_resource.cluster_destroy: Destruction complete after 0s
module.app_cluster.null_resource.cluster_destroy: Destruction complete after 0s

2 Likes

Hi,

This should work, one thing I have noticed however is that in your connection block you are using the meta parameter count.index, however your null_resource does not specify the count.

Also the interpolation syntax seems to be missing elements ${var.ec2_user} and ${element...}. This could just be code formatting.

connection {
  host = “element(awsinstance.appcluster.*.privateip,count.index)"
  type="ssh"
  user="{var.ec2_user}"
}

Below is a simple example which works as expected:

resource "random_string" "password" {
  count = 3

  length           = 16
  special          = true
  override_special = "/@\" "
}

resource "null_resource" "cluster" {
  count = 3

  provisioner "local-exec" {
    when    = "destroy"
    command = "echo  '${element(random_string.password.*.result, count.index)}'"
  }
}
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

null_resource.cluster[1]: Destroying... (ID: 5079794921211708881)
null_resource.cluster[0]: Destroying... (ID: 1948285964517026733)
null_resource.cluster[1]: Provisioning with 'local-exec'...
null_resource.cluster[0]: Provisioning with 'local-exec'...
null_resource.cluster[2]: Destroying... (ID: 1970227507830099825)
null_resource.cluster[2]: Provisioning with 'local-exec'...
null_resource.cluster[0] (local-exec): Executing: ["/bin/sh" "-c" "echo  'kW\"ScgyyWzbA@/pD'"]
null_resource.cluster[1] (local-exec): Executing: ["/bin/sh" "-c" "echo  'm3Av rw/nw4Q NSE'"]
null_resource.cluster[2] (local-exec): Executing: ["/bin/sh" "-c" "echo  'qpF/FRAKiJGkx9FP'"]
null_resource.cluster[2] (local-exec): qpF/FRAKiJGkx9FP
null_resource.cluster[1] (local-exec): m3Av rw/nw4Q NSE
null_resource.cluster[0] (local-exec): kW"ScgyyWzbA@/pD
null_resource.cluster[2]: Destruction complete after 0s
null_resource.cluster[1]: Destruction complete after 0s
null_resource.cluster[0]: Destruction complete after 0s
random_string.password[1]: Destroying... (ID: none)
random_string.password[2]: Destroying... (ID: none)
random_string.password[0]: Destroying... (ID: none)
random_string.password[1]: Destruction complete after 0s
random_string.password[2]: Destruction complete after 0s
random_string.password[0]: Destruction complete after 0s
1 Like

Hi Nic,

thanks for your response.

some of the curly braces i have missed it while copy pasting my original code.

the above examples shows for local-exec. i would like to execute commands inside the VM’s that i am going to delete(remote-exec).

will the above example work for remote-exec as well?

Yes it should do,

The provisioner life cycle is the same, just used local-exec as it was easier for an example.

1 Like