# Re-triggering shell script in terraform

**URL:** <https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988>\
**Category:** Terraform\
**Created:** [September 24, 2019, 6:10am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988 "2019-09-24T06:10:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhineshbabuelango](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dhineshbabuelango/32/2223_2.png) [@dhineshbabuelango](https://discuss.hashicorp.com/u/dhineshbabuelango)\
**Post date:** [September 24, 2019, 6:10am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/1 "2019-09-24T06:10:57Z")

</div>

Hi All, I am using null\_resource to trigger my shell script in terraform.  
Is there a way terraform apply can re trigger my shell script if I update my shell script.

---

<div class="post-metadata">

**Author:** ![sl1pm4t](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/sl1pm4t/32/2112_2.png) [@sl1pm4t](https://discuss.hashicorp.com/u/sl1pm4t)\
**Post date:** [September 24, 2019, 11:42pm UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/2 "2019-09-24T23:42:42Z")

</div>

Hi,

You could add a `triggers` block to the `null_resource` with the sha256 hash of the script contents.  
That way when the script changes, the hash changes, triggering the `null_resource` to re-apply.

> **[Null Resource - Terraform by HashiCorp](https://www.terraform.io/docs/providers/null/resource.html)**
>
> A resource that does nothing.

  

> **[sha256 - Functions - Configuration Language - Terraform by HashiCorp](https://www.terraform.io/docs/configuration/functions/sha256.html)**
>
> The sha256 function computes the SHA256 hash of a given string and encodes it with hexadecimal digits.

Something like:

```auto
resource "null_resource" "run_script" {
  triggers = {
    script_hash = "${sha256(var.bash_script)}"
  }
...
}

```

The sha256 hash is optional, but might help keep your .tfstate a little more compact if you use this pattern a lot (so the whole bash script isn’t stored in the state file repeatedly).

---

<div class="post-metadata">

**Author:** ![dhineshbabuelango](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dhineshbabuelango/32/2223_2.png) [@dhineshbabuelango](https://discuss.hashicorp.com/u/dhineshbabuelango)\
**Post date:** [September 25, 2019, 2:02am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/3 "2019-09-25T02:02:13Z")

</div>

Thanks Matt for your response, I will try this.  
Is there a way, we can trigger a shell script only during terraform destroy.

---

<div class="post-metadata">

**Author:** ![sl1pm4t](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/sl1pm4t/32/2112_2.png) [@sl1pm4t](https://discuss.hashicorp.com/u/sl1pm4t)\
**Post date:** [September 25, 2019, 2:16am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/4 "2019-09-25T02:16:42Z")

</div>

Yes, flag the `provisioner` with `when = "destroy"`.

**Example**

```auto
  provisioner "local-exec" {
    when = "destroy"
    command = "echo 'Destroy-time provisioner'"
  }

```

> **[Provisioners - Terraform by HashiCorp](https://www.terraform.io/docs/provisioners/index.html#destroy-time-provisioners)**
>
> Provisioners are used to execute scripts on a local or remote machine as part of resource creation or destruction.

---

<div class="post-metadata">

**Author:** ![dhineshbabuelango](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dhineshbabuelango/32/2223_2.png) [@dhineshbabuelango](https://discuss.hashicorp.com/u/dhineshbabuelango)\
**Post date:** [September 25, 2019, 6:53am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/5 "2019-09-25T06:53:42Z")

</div>

Thanks Matt for your response 🙂  
Regarding the below, is it possible to check the entire folder so that if there is any change it will trigger instead of checking only one file. My requirement is I have a helm folder, triggering that folder through shell scripts, if there is any change in the charts, templates, values file, terraform apply should trigger that.

 ![image](https://global.discourse-cdn.com/hashicorp/original/2X/e/eb6744eeb2d415f51a771e3528c2774a48d36749.png)

---

<div class="post-metadata">

**Author:** ![dhineshbabuelango](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dhineshbabuelango/32/2223_2.png) [@dhineshbabuelango](https://discuss.hashicorp.com/u/dhineshbabuelango)\
**Post date:** [September 25, 2019, 10:17am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/6 "2019-09-25T10:17:34Z")

</div>

I tried something like below, but it is not detecting any changes.

 ![image](https://global.discourse-cdn.com/hashicorp/original/2X/4/4ade5a23557370fd49d6a0454dbb88355777bfea.png)

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [September 25, 2019, 4:22pm UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/7 "2019-09-25T16:22:47Z")

</div>

Terraform doesn’t have a single primitive for hashing a whole directory, but if you are using Terraform v0.12.8 or later then you can construct such a thing using the new `fileset` function in conjunction with the `filesha256` function:

```auto
  triggers = {
    file_hashes = jsonencode({
      for fn in fileset("${path.module}/helm-files", "**") :
      fn => filesha256("${path.module}/helm-files/${fn}")
    })
  }

```

I used `jsonencode` here because `triggers` values are required to be strings, and so JSON encoding was a convenient way to flatten that constructed map into a single string. It doesn’t really matter which encoding you use there, but using JSON may help `terraform plan`'s change output to show you specifically what changed inside that object, because it’s often able to detect when a string contains JSON and show a structural changeset instead of a string-oriented one.

---

<div class="post-metadata">

**Author:** ![dhineshbabuelango](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dhineshbabuelango/32/2223_2.png) [@dhineshbabuelango](https://discuss.hashicorp.com/u/dhineshbabuelango)\
**Post date:** [September 26, 2019, 3:23am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/8 "2019-09-26T03:23:43Z")

</div>

Thanks Maat for your response. Its working fine 🙂 and now I got what I expected 🙂

---

<div class="post-metadata">

**Author:** ![dhineshbabuelango](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/dhineshbabuelango/32/2223_2.png) [@dhineshbabuelango](https://discuss.hashicorp.com/u/dhineshbabuelango)\
**Post date:** [September 28, 2019, 8:12am UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/9 "2019-09-28T08:12:13Z")

</div>

Hi Matt, for the below statement, is it possible to pass any path as variable.

```auto
  triggers = {
    file_hashes = jsonencode({
      for fn in fileset("${path.module}/helm-files", "**") :
      fn => filesha256("${path.module}/helm-files/${fn}")
    })
  }

```

Below is the example script I am using.

![image](https://global.discourse-cdn.com/hashicorp/original/2X/c/cb9a200804c9525c402874dfa24a611fdc64472e.png)

In this instead of /Users/dhineshbabu.elango/Documents/istio/istio-1.2.5 if I put any variable like ${var.path}. it is not accepting.

Even in my shell script I am passing the value by hardcoding. The istio version if I set that in variable and pass that as ${var.version} it is not working.  
 ![image](https://global.discourse-cdn.com/hashicorp/original/2X/f/f48e30729c55bf0237fc4fe3deaf128229495215.png)

---

<div class="post-metadata">

**Author:** ![nandeepmannava](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/nandeepmannava/32/9496_2.png) [@nandeepmannava](https://discuss.hashicorp.com/u/nandeepmannava)\
**Post date:** [February 2, 2021, 11:27pm UTC](https://discuss.hashicorp.com/t/re-triggering-shell-script-in-terraform/2988/10 "2021-02-02T23:27:24Z")

</div>

Hello,

I am using it in a similar way. When I need a change in the shell script the null resource should run, But that not happening in my case. Could you please help me with this?

```auto
data "template_file" "extension_data" {
  template = file("${path.module}/scripts/install.sh")

  vars = {
    USERNAME = "test"
 
  }
}

resource "null_resource" "cluster" {
  triggers = {
    cluster_instance_ids = join(",", aws_instance.web.*.id)
    script_hash = sha256("${path.module}/scripts/install.sh")
    
  }
  connection {
    host = element(aws_instance.web.*.public_ip, 0)
    user = "ec2-user"
    private_key = file("Mykey.pem")
  }

  provisioner "file" {
    content = data.template_file.extension_data.rendered
    destination = "/tmp/install.sh"
  }

  provisioner "remote-exec" {
    # Bootstrap script called with private_ip of each node in the clutser
    inline = ["set -e",
    "chmod +x /tmp/install.sh",
    "sudo sh /tmp/install.sh"]
  }

```

Thanks
