Terraform: Allow local-exec to be fired 1x when using count > 1 with aws_instance

Created on 22 Feb 2016  ยท  4Comments  ยท  Source: hashicorp/terraform

When using a aws_instance block with a count set to greater than 0 and using a provisioner of local-exec, the command executes for each instance in count.

Is there a way to control the execution?

question

All 4 comments

Coming from an ansible approach to infrastructure management, having a concept similar to with_items would increase the flexibility of working with terraform for me.

https://github.com/hashicorp/terraform/issues/1604

Hi @johnt337 - you can do this will a null_resource:

resource "aws_instance" "foo" {
  count = 10
  # ...
}
resource "null_resource" "after-foo" {
  depends_on = ["aws_instance.foo"]
  provisioner "local-exec" {
    inline = "echo 'Instance Ids: ${join(",", aws_instance.foo.*.id)}'"
  }
}

Hope this helps! Let me know if you have any further questions on this.

Sorry for the delay, was bogged down yesterday dealing with a prod issue.
This worked great. I had to swap "inline" for "command" but outside of that it ran as expected.

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