github.com/loicalbertin/terraform@v0.6.15-0.20170626182346-8e2583055467/website/docs/configuration/variables.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Configuring Variables" 4 sidebar_current: "docs-config-variables" 5 description: |- 6 Variables define the parameterization of Terraform configurations. Variables can be overridden via the CLI. Variable usage is covered in more detail in the getting started guide. This page covers configuration syntax for variables. 7 --- 8 9 # Variable Configuration 10 11 Variables define the parameterization of Terraform configurations. 12 Variables can be overridden via the CLI. Variable usage is 13 covered in more detail in the 14 [getting started guide](/intro/getting-started/variables.html). 15 This page covers configuration syntax for variables. 16 17 This page assumes you're familiar with the 18 [configuration syntax](/docs/configuration/syntax.html) 19 already. 20 21 ## Example 22 23 A variable configuration looks like the following: 24 25 ```hcl 26 variable "key" { 27 type = "string" 28 } 29 30 variable "images" { 31 type = "map" 32 33 default = { 34 us-east-1 = "image-1234" 35 us-west-2 = "image-4567" 36 } 37 } 38 39 variable "zones" { 40 default = ["us-east-1a", "us-east-1b"] 41 } 42 ``` 43 44 ## Description 45 46 The `variable` block configures a single input variable for 47 a Terraform configuration. Multiple variables blocks can be used to 48 add multiple variables. 49 50 The `name` given to the variable block is the name used to 51 set the variable via the CLI as well as reference the variable 52 throughout the Terraform configuration. 53 54 Within the block (the `{ }`) is configuration for the variable. 55 These are the parameters that can be set: 56 57 - `type` (optional) - If set this defines the type of the variable. Valid values 58 are `string`, `list`, and `map`. If this field is omitted, the variable type 59 will be inferred based on the `default`. If no `default` is provided, the type 60 is assumed to be `string`. 61 62 - `default` (optional) - This sets a default value for the variable. If no 63 default is provided, the variable is considered required and Terraform will 64 error if it is not set. The default value can be any of the data types 65 Terraform supports. This is covered in more detail below. 66 67 - `description` (optional) - A human-friendly description for the variable. This 68 is primarily for documentation for users using your Terraform configuration. A 69 future version of Terraform will expose these descriptions as part of some 70 Terraform CLI command. 71 72 -> **Note**: Default values can be strings, lists, or maps. If a default is 73 specified, it must match the declared type of the variable. 74 75 ### Strings 76 77 String values are simple and represent a basic key to value 78 mapping where the key is the variable name. An example is: 79 80 ```hcl 81 variable "key" { 82 type = "string" 83 default = "value" 84 } 85 ``` 86 87 A multi-line string value can be provided using heredoc syntax. 88 89 ```hcl 90 variable "long_key" { 91 type = "string" 92 default = <<EOF 93 This is a long key. 94 Running over several lines. 95 EOF 96 } 97 ``` 98 99 ### Maps 100 101 A map allows a key to contain a lookup table. This is useful 102 for some values that change depending on some external pivot. 103 A common use case for this is mapping cloud images to regions. 104 An example: 105 106 ```hcl 107 variable "images" { 108 type = "map" 109 default = { 110 us-east-1 = "image-1234" 111 us-west-2 = "image-4567" 112 } 113 } 114 ``` 115 116 ### Lists 117 118 A list can also be useful to store certain variables. For example: 119 120 ```hcl 121 variable "users" { 122 type = "list" 123 default = ["admin", "ubuntu"] 124 } 125 ``` 126 127 The usage of maps, lists, strings, etc. is documented fully in the 128 [interpolation syntax](/docs/configuration/interpolation.html) 129 page. 130 131 ## Syntax 132 133 The full syntax is: 134 135 ```text 136 variable NAME { 137 [type = TYPE] 138 [default = DEFAULT] 139 [description = DESCRIPTION] 140 } 141 ``` 142 143 where `DEFAULT` is: 144 145 ```text 146 VALUE 147 148 [ 149 VALUE, 150 ... 151 ] 152 153 { 154 KEY = VALUE 155 ... 156 } 157 ``` 158 159 ### Booleans 160 161 Although it appears Terraform supports boolean types, they are instead 162 silently converted to string types. The implications of this are subtle and 163 should be completely understood if you plan on using boolean values. 164 165 It is instead recommended you avoid using boolean values for now and use 166 explicit strings. A future version of Terraform will properly support booleans 167 and using the current behavior could result in backwards-incompatibilities in 168 the future. 169 170 For a configuration such as the following: 171 172 ```hcl 173 variable "active" { 174 default = false 175 } 176 ``` 177 178 The false is converted to a string `"0"` when running Terraform. 179 180 Then, depending on where you specify overrides, the behavior can differ: 181 182 - Variables with boolean values in a `tfvars` file will likewise be converted to 183 "0" and "1" values. 184 185 - Variables specified via the `-var` command line flag will be literal strings 186 "true" and "false", so care should be taken to explicitly use "0" or "1". 187 188 - Variables specified with the `TF_VAR_` environment variables will be literal 189 string values, just like `-var`. 190 191 A future version of Terraform will fully support first-class boolean 192 types which will make the behavior of booleans consistent as you would 193 expect. This may break some of the above behavior. 194 195 When passing boolean-like variables as parameters to resource configurations 196 that expect boolean values, they are converted consistently: 197 198 - "1", "true", "t" all become `true` 199 - "0", "false", "f" all become `false` 200 201 The behavior of conversion above will likely not change in future 202 Terraform versions. Therefore, simply using string values rather than 203 booleans for variables is recommended. 204 205 ## Environment Variables 206 207 Environment variables can be used to set the value of a variable. 208 The key of the environment variable must be `TF_VAR_name` and the value 209 is the value of the variable. 210 211 For example, given the configuration below: 212 213 ```hcl 214 variable "image" {} 215 ``` 216 217 The variable can be set via an environment variable: 218 219 ```shell 220 $ TF_VAR_image=foo terraform apply 221 ``` 222 223 Maps and lists can be specified using environment variables as well using 224 [HCL](/docs/configuration/syntax.html#HCL) syntax in the value. 225 226 For a list variable like so: 227 228 ```hcl 229 variable "somelist" { 230 type = "list" 231 } 232 ``` 233 234 The variable could be set like so: 235 236 ```shell 237 $ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan 238 ``` 239 240 Similarly, for a map declared like: 241 242 ```hcl 243 variable "somemap" { 244 type = "map" 245 } 246 ``` 247 248 The value can be set like this: 249 250 ```shell 251 $ TF_VAR_somemap='{foo = "bar", baz = "qux"}' terraform plan 252 ``` 253 254 ## Variable Files 255 256 Variables can be collected in files and passed all at once using the 257 `-var-file=foo.tfvars` flag. 258 259 If a file named `terraform.tfvars` is present in the current directory, 260 Terraform automatically loads it to populate variables. If the file is named 261 something else, you can pass the path to the file using the `-var-file` 262 flag. 263 264 Variables files use HCL or JSON to define variable values. Strings, lists or 265 maps may be set in the same manner as the default value in a `variable` block 266 in Terraform configuration. For example: 267 268 ```hcl 269 foo = "bar" 270 xyz = "abc" 271 272 somelist = [ 273 "one", 274 "two", 275 ] 276 277 somemap = { 278 foo = "bar" 279 bax = "qux" 280 } 281 ``` 282 283 The `-var-file` flag can be used multiple times per command invocation: 284 285 ```shell 286 $ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 287 ``` 288 289 -> **Note**: Variable files are evaluated in the order in which they are 290 specified on the command line. If a variable is defined in more than one 291 variable file, the last value specified is effective. 292 293 ### Variable Merging 294 295 When variables are conflicting, map values are merged and all other values are 296 overridden. Map values are always merged. 297 298 For example, if you set a variable twice on the command line: 299 300 ```shell 301 $ terraform apply -var foo=bar -var foo=baz 302 ``` 303 304 Then the value of `foo` will be `baz` since it was the last value seen. 305 306 However, for maps, the values are merged: 307 308 ```shell 309 $ terraform apply -var 'foo={foo="bar"}' -var 'foo={bar="baz"}' 310 ``` 311 312 The resulting value of `foo` will be: 313 314 ```shell 315 { 316 foo = "bar" 317 bar = "baz" 318 } 319 ``` 320 321 There is no way currently to unset map values in Terraform. Whenever a map 322 is modified either via variable input or being passed into a module, the 323 values are always merged. 324 325 ### Variable Precedence 326 327 Both these files have the variable `baz` defined: 328 329 _foo.tfvars_ 330 331 ```hcl 332 baz = "foo" 333 ``` 334 335 _bar.tfvars_ 336 337 ```hcl 338 baz = "bar" 339 ``` 340 341 When they are passed in the following order: 342 343 ```shell 344 $ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 345 ``` 346 347 The result will be that `baz` will contain the value `bar` because `bar.tfvars` 348 has the last definition loaded.