# Trigger null resource based on boolean variable

**URL:** <https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400>\
**Category:** Terraform\
**Created:** [August 15, 2019, 9:42pm UTC](https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400 "2019-08-15T21:42:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crmartis](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/crmartis/32/1848_2.png) [@crmartis](https://discuss.hashicorp.com/u/crmartis)\
**Post date:** [August 15, 2019, 9:42pm UTC](https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400/1 "2019-08-15T21:42:28Z")

</div>

Is there a way to trigger a null resource based on when a boolean variable is true in Terraform?

---

<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:** [August 16, 2019, 1:13am UTC](https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400/2 "2019-08-16T01:13:28Z")

</div>

Hi @crmartis,

The `null_resource` `triggers` reacts to _changes_ rather than to specific values. It’s fine to use boolean values in the triggers map – Terraform will automatically convert them to strings like `"true"` and `"false"` – but unfortunately there isn’t any way to trigger only when the value changes to `true` or constantly while the value is `true`.

If you can share some more details about the underlying problem you’re trying to solve, I might be able to suggest a different way to solve it.

---

<div class="post-metadata">

**Author:** ![crmartis](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/crmartis/32/1848_2.png) [@crmartis](https://discuss.hashicorp.com/u/crmartis)\
**Post date:** [August 16, 2019, 1:54am UTC](https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400/3 "2019-08-16T01:54:48Z")

</div>

@apparentlymart  
Thanks for offering to help.  
I have a null\_resource that is meant to be triggered whenever the AWS launch config changes. However, if var. always\_switch is true, I want it to be triggered always. Below was my best solution, but the problem scenario occurs when the null\_resource and provisioner will run simply when I turn the var. always\_switch back to false, since the\_trigger evalutes a change. Ideally, it would be good to only work when it is true. Is there a way to conditionally ignore\_changes or at least ignore the provisioner maybe, when it is changing back to false?

> resource “null\_resource” “change\_detected\_ASG2” {  
> triggers = {  
> the\_trigger= var.always\_switch?timestamp():aws\_launch\_configuration.autoscale\_launch\_config1.id  
> }  
> depends\_on = [aws\_launch\_configuration.autoscale\_launch\_config1]  
> lifecycle {  
> create\_before\_destroy = true  
> ignore\_changes = [triggers]  
> }  
> provisioner “local-exec” {  
> command = “checktoProceed.sh ASG2”  
> }
> 
> }

To be honest, I am actually trying to do all this as part of a work-around because I cannot put any conditions in depends\_on fields. Thus, as you can tell from the names of the provisioner and resource, this is part of a network of null resources that are meant to check and send signals via text files on which resource proceeds first, based on the conditions. Maybe I should start another thread for that problem, but if there is any more elegant way of actually putting conditionals in depends\_on, then I would not need to do all this…

Any advice would be helpful. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![crmartis](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/crmartis/32/1848_2.png) [@crmartis](https://discuss.hashicorp.com/u/crmartis)\
**Post date:** [August 16, 2019, 7:13am UTC](https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400/4 "2019-08-16T07:13:33Z")

</div>

UPDATE: So it turns out that I found a way around…I added a condition on the command of the null\_resource so the provisioner doesn’t run when switching back to false. It still means that when you do this switch back to false, the very next “terraform apply” will change the null\_resource to just change its triggers value but the provisioner won’t run so it doesn’t actually do anything. And in all other scenarios after that I think it works fine…

> resource “null\_resource” “change\_detected\_ASG1” {
> 
> triggers = {  
> the\_trigger= var.always\_switch?timestamp():aws\_launch\_configuration.autoscale\_launch\_config1.id  
> }  
> depends\_on = [aws\_launch\_configuration.autoscale\_launch\_config1]  
> lifecycle {create\_before\_destroy = true}  
> provisioner “local-exec” {  
> command = var.always\_switch?“checktoProceed.sh ASG1”:“echo blank step”  
> }
> 
> }

---

<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:** [August 16, 2019, 4:19pm UTC](https://discuss.hashicorp.com/t/trigger-null-resource-based-on-boolean-variable/2400/5 "2019-08-16T16:19:27Z")

</div>

Thanks for following up, @crmartis!

Indeed, using either `timestamp()` or `uuid()` in a conditional was my first idea after reading your full use-case too. As you noted, that does mean that switching _back_ to `false` will replace it one more time, but it will then be stable after that.

The idea of having the provisioner effectively disable itself in the `false` case is a good trick to avoid that extra unnecessary provision when switching back to `false`.
