terraform templatefile - ghdrako/doc_snipets GitHub Wiki

templatefile reads the file at the given path and renders its content as a template using a supplied set of template variables.

templatefile(path, vars)

The "vars" argument must be a map. Within the template file, each of the keys in the map is available as a variable for interpolation.

*.tftpl is the recommended naming pattern to use for your template files.

This function can be used only with files that already exist on disk at the beginning of a Terraform run. Functions do not participate in the dependency graph, so this function cannot be used with files that are generated dynamically during a Terraform operation.

Example

backends.tftpl:
%{ for addr in ip_addrs ~}
backend ${addr}:${port}
%{ endfor ~}

 templatefile("${path.module}/backends.tftpl", { port = 8080, ip_addrs = ["10.0.0.1", "10.0.0.2"] })
config.tftpl:
%{ for config_key, config_value in config }
set ${config_key} = ${config_value}
%{ endfor ~}
templatefile(
               "${path.module}/config.tftpl",
               {
                 config = {
                   "x"   = "y"
                   "foo" = "bar"
                   "key" = "value"
                 }
               }
              )