Sometimes there is a need to always run some terraform local-exec, so we can always execute a null_resource when doing an apply or applying it as a target.

So, how can you make sure to invalidate state for the null resource, so it always runs the local-exec script.

Using timestamp.

The following example always builds a docker image locally, when running both terraform apply generically to the *.tf file, or when targeting the resource directly.

resource "null_resource" "docker_build" {
  triggers = {
    always_run = "${timestamp()}"

  provisioner "local-exec" {
    command = "docker build -t ${var.image_name}:${var.image_tag} ./path-to-docker-file-Folder"

So now, whenever you run:

terraform apply


terraform apply -target=null_resource.docker_build

The state will be invalid given the trigger block definition, with a variable that points to terraform’s timestamp method invocation that will invalidate previous state.

And that’s it.

Thank you for reading, hope it has been helpful.

André Ilhicas dos Santos

Devops Padawan, curious about systems automation, learning new languages, paradigms tools each day.

ilhicas ilhicas