github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/website/docs/language/functions/templatefile.html.md (about)

     1  ---
     2  layout: "language"
     3  page_title: "templatefile - Functions - Configuration Language"
     4  sidebar_current: "docs-funcs-file-templatefile"
     5  description: |-
     6    The templatefile function reads the file at the given path and renders its
     7    content as a template.
     8  ---
     9  
    10  # `templatefile` Function
    11  
    12  `templatefile` reads the file at the given path and renders its content
    13  as a template using a supplied set of template variables.
    14  
    15  ```hcl
    16  templatefile(path, vars)
    17  ```
    18  
    19  The template syntax is the same as for
    20  [string templates](/docs/language/expressions/strings.html#string-templates)
    21  in the main Terraform language, including interpolation sequences delimited with
    22  `${` ... `}`. This function just allows longer template sequences to be factored
    23  out into a separate file for readability.
    24  
    25  The "vars" argument must be a map. Within the template file, each of the keys
    26  in the map is available as a variable for interpolation. The template may
    27  also use any other function available in the Terraform language, except that
    28  recursive calls to `templatefile` are not permitted. Variable names must
    29  each start with a letter, followed by zero or more letters, digits, or
    30  underscores.
    31  
    32  Strings in the Terraform language are sequences of Unicode characters, so
    33  this function will interpret the file contents as UTF-8 encoded text and
    34  return the resulting Unicode characters. If the file contains invalid UTF-8
    35  sequences then this function will produce an error.
    36  
    37  This function can be used only with files that already exist on disk at the
    38  beginning of a Terraform run. Functions do not participate in the dependency
    39  graph, so this function cannot be used with files that are generated
    40  dynamically during a Terraform operation.
    41  
    42  `*.tftpl` is the recommended naming pattern to use for your template files.
    43  Terraform will not prevent you from using other names, but following this
    44  convention will help your editor understand the content and likely provide
    45  better editing experience as a result.
    46  
    47  ## Examples
    48  
    49  ### Lists
    50  
    51  Given a template file `backends.tftpl` with the following content:
    52  
    53  ```
    54  %{ for addr in ip_addrs ~}
    55  backend ${addr}:${port}
    56  %{ endfor ~}
    57  ```
    58  
    59  The `templatefile` function renders the template:
    60  
    61  ```
    62  > templatefile("${path.module}/backends.tftpl", { port = 8080, ip_addrs = ["10.0.0.1", "10.0.0.2"] })
    63  backend 10.0.0.1:8080
    64  backend 10.0.0.2:8080
    65  
    66  ```
    67  
    68  ### Maps
    69  
    70  Given a template file `config.tftpl` with the following content:
    71  
    72  ```
    73  %{ for config_key, config_value in config }
    74  set ${config_key} = ${config_value}
    75  %{ endfor ~}
    76  ```
    77  
    78  The `templatefile` function renders the template:
    79  
    80  ```
    81  > templatefile(
    82                 "${path.module}/config.tftpl",
    83                 {
    84                   config = {
    85                     "x"   = "y"
    86                     "foo" = "bar"
    87                     "key" = "value"
    88                   }
    89                 }
    90                )
    91  set foo = bar
    92  set key = value
    93  set x = y
    94  ```
    95  
    96  ### Generating JSON or YAML from a template
    97  
    98  If the string you want to generate will be in JSON or YAML syntax, it's
    99  often tricky and tedious to write a template that will generate valid JSON or
   100  YAML that will be interpreted correctly when using lots of individual
   101  interpolation sequences and directives.
   102  
   103  Instead, you can write a template that consists only of a single interpolated
   104  call to either [`jsonencode`](./jsonencode.html) or
   105  [`yamlencode`](./yamlencode.html), specifying the value to encode using
   106  [normal Terraform expression syntax](/docs/language/expressions/index.html)
   107  as in the following examples:
   108  
   109  ```
   110  ${jsonencode({
   111    "backends": [for addr in ip_addrs : "${addr}:${port}"],
   112  })}
   113  ```
   114  
   115  ```
   116  ${yamlencode({
   117    "backends": [for addr in ip_addrs : "${addr}:${port}"],
   118  })}
   119  ```
   120  
   121  Given the same input as the `backends.tftpl` example in the previous section,
   122  this will produce a valid JSON or YAML representation of the given data
   123  structure, without the need to manually handle escaping or delimiters.
   124  In the latest examples above, the repetition based on elements of `ip_addrs` is
   125  achieved by using a
   126  [`for` expression](/docs/language/expressions/for.html)
   127  rather than by using
   128  [template directives](/docs/language/expressions/strings.html#directives).
   129  
   130  ```json
   131  {"backends":["10.0.0.1:8080","10.0.0.2:8080"]}
   132  ```
   133  
   134  If the resulting template is small, you can choose instead to write
   135  `jsonencode` or `yamlencode` calls inline in your main configuration files, and
   136  avoid creating separate template files at all:
   137  
   138  ```hcl
   139  locals {
   140    backend_config_json = jsonencode({
   141      "backends": [for addr in ip_addrs : "${addr}:${port}"],
   142    })
   143  }
   144  ```
   145  
   146  For more information, see the main documentation for
   147  [`jsonencode`](./jsonencode.html) and [`yamlencode`](./yamlencode.html).
   148  
   149  ## Related Functions
   150  
   151  * [`file`](./file.html) reads a file from disk and returns its literal contents
   152    without any template interpretation.