2018-12-21 02:42:42 +01:00
|
|
|
---
|
2020-08-15 03:51:06 +02:00
|
|
|
layout: "language"
|
2018-12-21 02:42:42 +01:00
|
|
|
page_title: "templatefile - Functions - Configuration Language"
|
|
|
|
sidebar_current: "docs-funcs-file-templatefile"
|
|
|
|
description: |-
|
|
|
|
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.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
templatefile(path, vars)
|
|
|
|
```
|
|
|
|
|
|
|
|
The template syntax is the same as for
|
2021-01-15 23:13:53 +01:00
|
|
|
[string templates](/docs/language/expressions/strings.html#string-templates)
|
2020-11-13 03:30:52 +01:00
|
|
|
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.
|
2018-12-21 02:42:42 +01:00
|
|
|
|
|
|
|
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
|
2020-02-22 01:35:27 +01:00
|
|
|
recursive calls to `templatefile` are not permitted. Variable names must
|
|
|
|
each start with a letter, followed by zero or more letters, digits, or
|
|
|
|
underscores.
|
2018-12-21 02:42:42 +01:00
|
|
|
|
|
|
|
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
|
2020-09-21 18:04:46 +02:00
|
|
|
dynamically during a Terraform operation.
|
2018-12-21 02:42:42 +01:00
|
|
|
|
2021-06-18 18:20:00 +02:00
|
|
|
`*.tftpl` is the recommended naming pattern to use for your template files.
|
|
|
|
Terraform will not prevent you from using other names, but following this
|
|
|
|
convention will help your editor understand the content and likely provide
|
|
|
|
better editing experience as a result.
|
|
|
|
|
2018-12-21 02:42:42 +01:00
|
|
|
## Examples
|
|
|
|
|
2020-09-09 21:30:06 +02:00
|
|
|
### Lists
|
|
|
|
|
2021-06-18 18:20:00 +02:00
|
|
|
Given a template file `backends.tftpl` with the following content:
|
2018-12-21 02:42:42 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
%{ for addr in ip_addrs ~}
|
|
|
|
backend ${addr}:${port}
|
|
|
|
%{ endfor ~}
|
|
|
|
```
|
|
|
|
|
|
|
|
The `templatefile` function renders the template:
|
|
|
|
|
|
|
|
```
|
2021-06-18 18:20:00 +02:00
|
|
|
> templatefile("${path.module}/backends.tftpl", { port = 8080, ip_addrs = ["10.0.0.1", "10.0.0.2"] })
|
2018-12-21 02:42:42 +01:00
|
|
|
backend 10.0.0.1:8080
|
|
|
|
backend 10.0.0.2:8080
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2020-09-09 21:30:06 +02:00
|
|
|
### Maps
|
|
|
|
|
2021-06-18 18:20:00 +02:00
|
|
|
Given a template file `config.tftpl` with the following content:
|
2020-09-09 21:30:06 +02:00
|
|
|
|
|
|
|
```
|
|
|
|
%{ for config_key, config_value in config }
|
|
|
|
set ${config_key} = ${config_value}
|
|
|
|
%{ endfor ~}
|
|
|
|
```
|
|
|
|
|
|
|
|
The `templatefile` function renders the template:
|
|
|
|
|
|
|
|
```
|
|
|
|
> templatefile(
|
2021-06-18 18:20:00 +02:00
|
|
|
"${path.module}/config.tftpl",
|
2020-11-13 03:30:52 +01:00
|
|
|
{
|
2020-09-09 21:30:06 +02:00
|
|
|
config = {
|
|
|
|
"x" = "y"
|
|
|
|
"foo" = "bar"
|
|
|
|
"key" = "value"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
set foo = bar
|
|
|
|
set key = value
|
|
|
|
set x = y
|
|
|
|
```
|
|
|
|
|
2019-12-10 23:40:12 +01:00
|
|
|
### Generating JSON or YAML from a template
|
|
|
|
|
|
|
|
If the string you want to generate will be in JSON or YAML syntax, it's
|
|
|
|
often tricky and tedious to write a template that will generate valid JSON or
|
|
|
|
YAML that will be interpreted correctly when using lots of individual
|
|
|
|
interpolation sequences and directives.
|
|
|
|
|
|
|
|
Instead, you can write a template that consists only of a single interpolated
|
|
|
|
call to either [`jsonencode`](./jsonencode.html) or
|
|
|
|
[`yamlencode`](./yamlencode.html), specifying the value to encode using
|
2021-01-15 23:13:53 +01:00
|
|
|
[normal Terraform expression syntax](/docs/language/expressions/index.html)
|
2019-12-10 23:40:12 +01:00
|
|
|
as in the following examples:
|
|
|
|
|
|
|
|
```
|
|
|
|
${jsonencode({
|
|
|
|
"backends": [for addr in ip_addrs : "${addr}:${port}"],
|
|
|
|
})}
|
|
|
|
```
|
|
|
|
|
|
|
|
```
|
|
|
|
${yamlencode({
|
|
|
|
"backends": [for addr in ip_addrs : "${addr}:${port}"],
|
|
|
|
})}
|
|
|
|
```
|
|
|
|
|
2021-06-18 18:20:00 +02:00
|
|
|
Given the same input as the `backends.tftpl` example in the previous section,
|
2019-12-10 23:40:12 +01:00
|
|
|
this will produce a valid JSON or YAML representation of the given data
|
|
|
|
structure, without the need to manually handle escaping or delimiters.
|
|
|
|
In the latest examples above, the repetition based on elements of `ip_addrs` is
|
|
|
|
achieved by using a
|
2021-01-15 23:13:53 +01:00
|
|
|
[`for` expression](/docs/language/expressions/for.html)
|
2019-12-10 23:40:12 +01:00
|
|
|
rather than by using
|
2021-01-15 23:13:53 +01:00
|
|
|
[template directives](/docs/language/expressions/strings.html#directives).
|
2019-12-10 23:40:12 +01:00
|
|
|
|
|
|
|
```json
|
|
|
|
{"backends":["10.0.0.1:8080","10.0.0.2:8080"]}
|
|
|
|
```
|
|
|
|
|
|
|
|
If the resulting template is small, you can choose instead to write
|
|
|
|
`jsonencode` or `yamlencode` calls inline in your main configuration files, and
|
|
|
|
avoid creating separate template files at all:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
locals {
|
|
|
|
backend_config_json = jsonencode({
|
|
|
|
"backends": [for addr in ip_addrs : "${addr}:${port}"],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
For more information, see the main documentation for
|
|
|
|
[`jsonencode`](./jsonencode.html) and [`yamlencode`](./yamlencode.html).
|
|
|
|
|
2018-12-21 02:42:42 +01:00
|
|
|
## Related Functions
|
|
|
|
|
|
|
|
* [`file`](./file.html) reads a file from disk and returns its literal contents
|
|
|
|
without any template interpretation.
|