Files
opentf/website/docs/configuration/functions/templatefile.html.md
Martin Atkins c753df6a93 lang/funcs: templatefile function
This function is similar to the template_file data source offered by the
template provider, but having it built in to the language makes it more
convenient to use, allowing templates to be rendered from files anywhere
an inline template would normally be allowed:

    user_data = templatefile("${path.module}/userdata.tmpl", {
      hostname = format("petserver%02d", count.index)
    })

Unlike the template_file data source, this function allows values of any
type in its variables map, passing them through verbatim to the template.
Its tighter integration with Terraform also allows it to return better
error messages with source location information from the template itself.

The template_file data source was originally created to work around the
fact that HIL didn't have any support for map values at the time, and
even once map support was added it wasn't very usable. With HCL2
expressions, there's little reason left to use a data source to render
a template; the only remaining reason left to use template_file is to
render a template that is constructed dynamically during the Terraform
run, which is a very rare need.
2018-12-21 08:06:14 -08:00

2.3 KiB

layout, page_title, sidebar_current, description
layout page_title sidebar_current description
functions templatefile - Functions - Configuration Language docs-funcs-file-templatefile The templatefile function reads the file at the given path and renders its content as a template.

templatefile Function

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 template syntax is the same as for string templates in the main Terraform language, including interpolation sequences delimited with ${ ... }. This function just allows longer template sequences to be factored out into a separate file for readability.

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. The template may also use any other function available in the Terraform language, except that recursive calls to templatefile are not permitted.

Strings in the Terraform language are sequences of Unicode characters, so this function will interpret the file contents as UTF-8 encoded text and return the resulting Unicode characters. If the file contains invalid UTF-8 sequences then this function will produce an error.

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. We do not recommend using dynamic templates in Terraform configurations, but in rare situations where this is necessary you can use the template_file data source to render templates while respecting resource dependencies.

Examples

Given a template file backends.tmpl with the following content:

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

The templatefile function renders the template:

> templatefile("${path.module}/backends.tmpl", { port = 8080, ip_addrs = ["10.0.0.1", "10.0.0.2"] })
backend 10.0.0.1:8080
backend 10.0.0.2:8080

  • file reads a file from disk and returns its literal contents without any template interpretation.