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