Example error in one of the cases:
Invalid template interpolation value: Cannot include the given value in a string template: string required
Weird thing is that the output below works, but all I need is it to be written to a file:
output “FEs-IPs” {
description = “IPs of all FEs provisoned.”
value = data.azurerm_public_ip.FEs-PIP.*.ip_address
}
What’s the purpose of having that output?
Usually you would define output variables and post process those with running terraform output -json .....
Another option is to create a file with the file resource:
if you put required variable outputs within a locals block, then you get access to any resource/module which might be needed.
I’ve updated the code above.
Error: Reference to "count" in non-counted context
on coaching.tf line 36, in locals:
36: AppServersPips = concat(element(data.azurerm_public_ip.FEs-PIP.*.ip_address, count.index),"")
The "count" object can only be used in "module", "resource", and "data"
blocks, and only when the "count" argument is set.
I’m really getting confused on if whether I’m doing something wrong or if this isn’t possible at all with Terraform
Indeed you cannot use count.index without having an appropriate object.
I still don’t understand why terraform output -json might not support your use case and the local-exec provisioner is needed.
Do you have a repo to use for further testing?
The local_file resource type is the best way to create a file on local disk with Terraform.
Using a provisioner to do it would be much harder because rather than using a template to generate a file you’d instead be using a template to generate a script which in turn generates a file, which will be hard to write (as you’ve seen) and read.
@joaooamaral is right that it’s not really typical for a Terraform configuration to generate local files on disk – output values are the usual approach – but if you do need to do it then you can use local_file in conjunction with the Terraform template syntax: