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?
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.
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.