Terraform: Trigger a null_resource on every run?

Created on 12 Jul 2016  ยท  3Comments  ยท  Source: hashicorp/terraform

Hi there,

Is there a way to trigger a null_resource on every terraform run?

For example:

resource "null_resource" "db_subnets" {
  triggers {
    db_subnet_ids = "${join(",", aws_subnet.db_subnet.*.id)}"
  }
  provisioner "local-exec" {
        command = "echo ${join(",", aws_subnet.db_subnet.*.id)} >> subnets.txt"
  }
}

This creates the subnets.txt file when the subnets are created. However, when I delete the subnets.txt file, it will not be recreated on the next TerraForm run.

It would like to recreate it on every run.

question

Most helpful comment

Hi @jeroenjacobs1205!

Probably the easiest way to do this would be to put something that doesn't stabilize in the triggers map. The uuid() function might do the trick, since it should generate a fresh uuid on each Terraform run.

Note that in Terraform 0.7 it will be possible for outputs to be lists as well as strings, so in that version it may work out better to export your list of subnets as an output and then use terraform output subnet_ids >subnets..txt to obtain this value after Terraform has completed... that way, the list of subnets is persisted in Terraform's state file and you can easily reproduce it when needed without running terraform apply again.

All 3 comments

Hi @jeroenjacobs1205!

Probably the easiest way to do this would be to put something that doesn't stabilize in the triggers map. The uuid() function might do the trick, since it should generate a fresh uuid on each Terraform run.

Note that in Terraform 0.7 it will be possible for outputs to be lists as well as strings, so in that version it may work out better to export your list of subnets as an output and then use terraform output subnet_ids >subnets..txt to obtain this value after Terraform has completed... that way, the list of subnets is persisted in Terraform's state file and you can easily reproduce it when needed without running terraform apply again.

Answered above.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

carl-youngblood picture carl-youngblood  ยท  3Comments

thebenwaters picture thebenwaters  ยท  3Comments

jrnt30 picture jrnt30  ยท  3Comments

ketzacoatl picture ketzacoatl  ยท  3Comments

rkulagowski picture rkulagowski  ยท  3Comments