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

     1  ---
     2  layout: "language"
     3  page_title: "Strings and Templates - Configuration Language"
     4  description: "String literals and template sequences interpolate values and manipulate text. Learn about both quoted and heredoc string syntax."
     5  
     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](/docs/language/functions/jsonencode.html) or
    81  [the `yamlencode` function](/docs/language/functions/yamlencode.html) 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  
   141  ## String Templates
   142  
   143  Within quoted and heredoc string expressions, the sequences `${` and `%{` begin
   144  _template sequences_. Templates let you directly embed expressions into a string
   145  literal, to dynamically construct strings from other values.
   146  
   147  ### Interpolation
   148  
   149  A `${ ... }` sequence is an _interpolation,_ which evaluates the expression
   150  given between the markers, converts the result to a string if necessary, and
   151  then inserts it into the final string:
   152  
   153  ```hcl
   154  "Hello, ${var.name}!"
   155  ```
   156  
   157  In the above example, the named object `var.name` is accessed and its value
   158  inserted into the string, producing a result like "Hello, Juan!".
   159  
   160  ### Directives
   161  
   162  A `%{ ... }` sequence is a _directive_, which allows for conditional
   163  results and iteration over collections, similar to conditional
   164  and `for` expressions.
   165  
   166  The following directives are supported:
   167  
   168  * The `%{if <BOOL>}`/`%{else}`/`%{endif}` directive chooses between two templates based
   169    on the value of a bool expression:
   170  
   171      ```hcl
   172      "Hello, %{ if var.name != "" }${var.name}%{ else }unnamed%{ endif }!"
   173      ```
   174  
   175      The `else` portion may be omitted, in which case the result is an empty
   176      string if the condition expression returns `false`.
   177  
   178  * The `%{for <NAME> in <COLLECTION>}` / `%{endfor}` directive iterates over the
   179    elements of a given collection or structural value and evaluates a given
   180    template once for each element, concatenating the results together:
   181  
   182      ```hcl
   183      <<EOT
   184      %{ for ip in aws_instance.example.*.private_ip }
   185      server ${ip}
   186      %{ endfor }
   187      EOT
   188      ```
   189  
   190      The name given immediately after the `for` keyword is used as a temporary
   191      variable name which can then be referenced from the nested template.
   192  
   193  ### Whitespace Stripping
   194  
   195  To allow template directives to be formatted for readability without adding
   196  unwanted spaces and newlines to the result, all template sequences can include
   197  optional _strip markers_ (`~`), immediately after the opening characters or
   198  immediately before the end. When a strip marker is present, the template
   199  sequence consumes all of the literal whitespace (spaces and newlines) either
   200  before the sequence (if the marker appears at the beginning) or after (if the
   201  marker appears at the end):
   202  
   203  ```hcl
   204  <<EOT
   205  %{ for ip in aws_instance.example.*.private_ip ~}
   206  server ${ip}
   207  %{ endfor ~}
   208  EOT
   209  ```
   210  
   211  In the above example, the newline after each of the directives is not included
   212  in the output, but the newline after the `server ${ip}` sequence is retained,
   213  causing only one line to be generated for each element:
   214  
   215  ```
   216  server 10.1.16.154
   217  server 10.1.16.1
   218  server 10.1.16.34
   219  ```
   220  
   221  When using template directives, we recommend always using the "heredoc" string
   222  literal form and then formatting the template over multiple lines for
   223  readability. Quoted string literals should usually include only interpolation
   224  sequences.