How to rewrite null_resource with local-exec provisioner when destroy (to prepare for deprecation after 0.12.8)

Hi @lchastel,

When using null_resource, you can use the triggers map both to signal when the provisioners need to re-run (the usual purpose) and to retain values you can access via self during the destroy phase. For example:

resource "null_resource" "project_mgmt" {
  triggers = {
    project_id = google_project.my_project.project_id
  }

  provisioner "local-exec" {
    command = <<-EOC
gcloud pubsub topics publish projects/project_mgmt/topics/register --message '{"project":"${self.triggers.project_id}"}'
EOC
  }
  
  provisioner "local-exec" {
    when    = destroy
    command = <<-EOD
gcloud pubsub topics publish projects/project_mgmt/topics/unregister --message '{"project":"${self.triggers.project_id}"}'
EOD
  }
}

This makes the project id part of the stored state of the null_resource itself and thus avoids the dependency issues during the destroy phase that this deprecation warning is referring to.

5 Likes