Terraform Null Resource execute every time

In Terraform, the null_resource implements the standard resource lifecycle but takes no further action.  If the plan does not change, the null_resource will not run.  Make sense but what if you want the null_resource to run every time you do an apply.  If you are constrained by your environment and can only use Terraform to execute commands on the server you can use triggers.

The triggers argument allows specifying an arbitrary set of values that, when changed, will cause the resource to be replaced.  In other words, the null_resource will re-execute.

Terraform Null Resource

Example:

 

resource "null_resource" "packer" {
    triggers {
        build_number = "${timestamp()}"
    }
    provisioner "local-exec" {
        command = "execute my command"
    }
}

The build_number is updated with the timestamp of the running server. This makes the build_number change every time and this allows the null_resource to execute every time.