Is there a way to trigger a null resource based on when a boolean variable is true in Terraform?
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.
@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!
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”
}}
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
.