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:
resource "local_file" "outputdata" {
filename = "${path.module}/tf_output.txt"
content = <<EOT
%{ for ip_addr in data.azurerm_public_ip.FEs-PIP.*.ip_address ~}
${ip_addr}
%{ endfor ~}
EOT
}