github.com/hugorut/terraform@v1.1.3/website/docs/language/expressions/strings.mdx (about)

     1  ---
     2  page_title: Strings and Templates - Configuration Language
     3  description: >-
     4    String literals and template sequences interpolate values and manipulate text.
     5    Learn about both quoted and heredoc string syntax.
     6  ---
     7  
     8  # Strings and Templates
     9  
    10  String literals are the most complex kind of literal expression in
    11  Terraform, and also the most commonly used.
    12  
    13  Terraform supports both a quoted syntax and a "heredoc" syntax for strings.
    14  Both of these syntaxes support template sequences for interpolating values and
    15  manipulating text.
    16  
    17  ## Quoted Strings
    18  
    19  A quoted string is a series of characters delimited by straight double-quote
    20  characters (`"`).
    21  
    22  ```
    23  "hello"
    24  ```
    25  
    26  ### Escape Sequences
    27  
    28  In quoted strings, the backslash character serves as an escape
    29  sequence, with the following characters selecting the escape behavior:
    30  
    31  | Sequence     | Replacement                                                                   |
    32  | ------------ | ----------------------------------------------------------------------------- |
    33  | `\n`         | Newline                                                                       |
    34  | `\r`         | Carriage Return                                                               |
    35  | `\t`         | Tab                                                                           |
    36  | `\"`         | Literal quote (without terminating the string)                                |
    37  | `\\`         | Literal backslash                                                             |
    38  | `\uNNNN`     | Unicode character from the basic multilingual plane (NNNN is four hex digits) |
    39  | `\UNNNNNNNN` | Unicode character from supplementary planes (NNNNNNNN is eight hex digits)    |
    40  
    41  There are also two special escape sequences that do not use backslashes:
    42  
    43  | Sequence | Replacement                                                    |
    44  | -------- | -------------------------------------------------------------- |
    45  | `$${`    | Literal `${`, without beginning an interpolation sequence.     |
    46  | `%%{`    | Literal `%{`, without beginning a template directive sequence. |
    47  
    48  ## Heredoc Strings
    49  
    50  Terraform also supports a "heredoc" style of string literal inspired by Unix
    51  shell languages, which allows multi-line strings to be expressed more clearly.
    52  
    53  ```hcl
    54  <<EOT
    55  hello
    56  world
    57  EOT
    58  ```
    59  
    60  A heredoc string consists of:
    61  
    62  - An opening sequence consisting of:
    63    - A heredoc marker (`<<` or `<<-` — two less-than signs, with an optional hyphen for indented heredocs)
    64    - A delimiter word of your own choosing
    65    - A line break
    66  - The contents of the string, which can span any number of lines
    67  - The delimiter word you chose, alone on its own line (with indentation allowed for indented heredocs)
    68  
    69  The `<<` marker followed by any identifier at the end of a line introduces the
    70  sequence. Terraform then processes the following lines until it finds one that
    71  consists entirely of the identifier given in the introducer.
    72  
    73  In the above example, `EOT` is the identifier selected. Any identifier is
    74  allowed, but conventionally this identifier is in all-uppercase and begins with
    75  `EO`, meaning "end of". `EOT` in this case stands for "end of text".
    76  
    77  ### Generating JSON or YAML
    78  
    79  Don't use "heredoc" strings to generate JSON or YAML. Instead, use
    80  [the `jsonencode` function](/language/functions/jsonencode) or
    81  [the `yamlencode` function](/language/functions/yamlencode) so that Terraform
    82  can be responsible for guaranteeing valid JSON or YAML syntax.
    83  
    84  ```hcl
    85    example = jsonencode({
    86      a = 1
    87      b = "hello"
    88    })
    89  ```
    90  
    91  ### Indented Heredocs
    92  
    93  The standard heredoc form (shown above) treats all space characters as literal
    94  spaces. If you don't want each line to begin with spaces, then each line must be
    95  flush with the left margin, which can be awkward for expressions in an
    96  indented block:
    97  
    98  ```hcl
    99  block {
   100    value = <<EOT
   101  hello
   102  world
   103  EOT
   104  }
   105  ```
   106  
   107  To improve on this, Terraform also accepts an _indented_ heredoc string variant
   108  that is introduced by the `<<-` sequence:
   109  
   110  ```hcl
   111  block {
   112    value = <<-EOT
   113    hello
   114      world
   115    EOT
   116  }
   117  ```
   118  
   119  In this case, Terraform analyses the lines in the sequence to find the one
   120  with the smallest number of leading spaces, and then trims that many spaces
   121  from the beginning of all of the lines, leading to the following result:
   122  
   123  ```
   124  hello
   125    world
   126  ```
   127  
   128  ### Escape Sequences
   129  
   130  Backslash sequences are not interpreted as escapes in a heredoc string
   131  expression. Instead, the backslash character is interpreted literally.
   132  
   133  Heredocs support two special escape sequences that do not use backslashes:
   134  
   135  | Sequence | Replacement                                                    |
   136  | -------- | -------------------------------------------------------------- |
   137  | `$${`    | Literal `${`, without beginning an interpolation sequence.     |
   138  | `%%{`    | Literal `%{`, without beginning a template directive sequence. |
   139  
   140  ## String Templates
   141  
   142  Within quoted and heredoc string expressions, the sequences `${` and `%{` begin
   143  _template sequences_. Templates let you directly embed expressions into a string
   144  literal, to dynamically construct strings from other values.
   145  
   146  ### Interpolation
   147  
   148  A `${ ... }` sequence is an _interpolation,_ which evaluates the expression
   149  given between the markers, converts the result to a string if necessary, and
   150  then inserts it into the final string:
   151  
   152  ```hcl
   153  "Hello, ${var.name}!"
   154  ```
   155  
   156  In the above example, the named object `var.name` is accessed and its value
   157  inserted into the string, producing a result like "Hello, Juan!".
   158  
   159  ### Directives
   160  
   161  A `%{ ... }` sequence is a _directive_, which allows for conditional
   162  results and iteration over collections, similar to conditional
   163  and `for` expressions.
   164  
   165  The following directives are supported:
   166  
   167  - The `%{if <BOOL>}`/`%{else}`/`%{endif}` directive chooses between two templates based
   168    on the value of a bool expression:
   169  
   170    ```hcl
   171    "Hello, %{ if var.name != "" }${var.name}%{ else }unnamed%{ endif }!"
   172    ```
   173  
   174    The `else` portion may be omitted, in which case the result is an empty
   175    string if the condition expression returns `false`.
   176  
   177  - The `%{for <NAME> in <COLLECTION>}` / `%{endfor}` directive iterates over the
   178    elements of a given collection or structural value and evaluates a given
   179    template once for each element, concatenating the results together:
   180  
   181    ```hcl
   182    <<EOT
   183    %{ for ip in aws_instance.example.*.private_ip }
   184    server ${ip}
   185    %{ endfor }
   186    EOT
   187    ```
   188  
   189    The name given immediately after the `for` keyword is used as a temporary
   190    variable name which can then be referenced from the nested template.
   191  
   192  ### Whitespace Stripping
   193  
   194  To allow template directives to be formatted for readability without adding
   195  unwanted spaces and newlines to the result, all template sequences can include
   196  optional _strip markers_ (`~`), immediately after the opening characters or
   197  immediately before the end. When a strip marker is present, the template
   198  sequence consumes all of the literal whitespace (spaces and newlines) either
   199  before the sequence (if the marker appears at the beginning) or after (if the
   200  marker appears at the end):
   201  
   202  ```hcl
   203  <<EOT
   204  %{ for ip in aws_instance.example.*.private_ip ~}
   205  server ${ip}
   206  %{ endfor ~}
   207  EOT
   208  ```
   209  
   210  In the above example, the newline after each of the directives is not included
   211  in the output, but the newline after the `server ${ip}` sequence is retained,
   212  causing only one line to be generated for each element:
   213  
   214  ```
   215  server 10.1.16.154
   216  server 10.1.16.1
   217  server 10.1.16.34
   218  ```
   219  
   220  When using template directives, we recommend always using the "heredoc" string
   221  literal form and then formatting the template over multiple lines for
   222  readability. Quoted string literals should usually include only interpolation
   223  sequences.