github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration-0-11/variables.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Input Variables - 0.11 Configuration Language" 4 sidebar_current: "docs-conf-old-variables" 5 description: |- 6 Input variables are parameters for Terraform modules. 7 This page covers configuration syntax for variables. 8 --- 9 10 # Input Variables 11 12 -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12 13 and later, see 14 [Configuration Language: Input Variables](../configuration/variables.html). 15 16 Input variables serve as parameters for a Terraform module. 17 18 When used in the root module of a configuration, variables can be set from CLI 19 arguments and environment variables. For [_child_ modules](./modules.html), 20 they allow values to pass from parent to child. 21 22 This page assumes you're familiar with the 23 [configuration syntax](./syntax.html) 24 already. 25 26 ## Example 27 28 Input variables can be defined as follows: 29 30 ```hcl 31 variable "key" { 32 type = "string" 33 } 34 35 variable "images" { 36 type = "map" 37 38 default = { 39 us-east-1 = "image-1234" 40 us-west-2 = "image-4567" 41 } 42 } 43 44 variable "zones" { 45 type = "list" 46 default = ["us-east-1a", "us-east-1b"] 47 } 48 ``` 49 50 ## Description 51 52 The `variable` block configures a single input variable for a Terraform module. 53 Each block declares a single variable. 54 55 The name given in the block header is used to assign a value to the variable 56 via the CLI and to reference the variable elsewhere in the configuration. 57 58 Within the block body (between `{ }`) is configuration for the variable, 59 which accepts the following arguments: 60 61 - `type` (Optional) - If set this defines the type of the variable. Valid values 62 are `string`, `list`, and `map`. If this field is omitted, the variable type 63 will be inferred based on `default`. If no `default` is provided, the type 64 is assumed to be `string`. 65 66 - `default` (Optional) - This sets a default value for the variable. If no 67 default is provided, Terraform will raise an error if a value is not provided 68 by the caller. The default value can be of any of the supported data types, 69 as described below. If `type` is also set, the given value must be 70 of the specified type. 71 72 - `description` (Optional) - A human-friendly description for the variable. This 73 is primarily for documentation for users using your Terraform configuration. 74 When a module is published in [Terraform Registry](https://registry.terraform.io/), 75 the given description is shown as part of the documentation. 76 77 The name of a variable can be any valid identifier. However, due to the 78 interpretation of [module configuration blocks](./modules.html), 79 the names `source`, `version` and `providers` are reserved for Terraform's own 80 use and are thus not recommended for any module intended to be used as a 81 child module. 82 83 The default value of an input variable must be a _literal_ value, containing 84 no interpolation expressions. To assign a name to an expression so that it 85 may be re-used within a module, use [Local Values](./locals.html) 86 instead. 87 88 ### Strings 89 90 String values are simple and represent a basic key to value 91 mapping where the key is the variable name. An example is: 92 93 ```hcl 94 variable "key" { 95 type = "string" 96 default = "value" 97 } 98 ``` 99 100 A multi-line string value can be provided using heredoc syntax. 101 102 ```hcl 103 variable "long_key" { 104 type = "string" 105 default = <<EOF 106 This is a long key. 107 Running over several lines. 108 EOF 109 } 110 ``` 111 112 Terraform performs automatic conversion from string values to numeric and 113 boolean values based on context, so in practice string variables may be used 114 to set arguments of any primitive type. For boolean values in particular 115 there are some caveats, described under [_Booleans_](#booleans) below. 116 117 ### Maps 118 119 A map value is a lookup table from string keys to string values. This is 120 useful for selecting a value based on some other provided value. 121 122 A common use of maps is to create a table of machine images per region, 123 as follows: 124 125 ```hcl 126 variable "images" { 127 type = "map" 128 default = { 129 "us-east-1" = "image-1234" 130 "us-west-2" = "image-4567" 131 } 132 } 133 ``` 134 135 ### Lists 136 137 A list value is an ordered sequence of strings indexed by integers starting 138 with zero. For example: 139 140 ```hcl 141 variable "users" { 142 type = "list" 143 default = ["admin", "ubuntu"] 144 } 145 ``` 146 147 ### Booleans 148 149 Although Terraform can automatically convert between boolean and string 150 values, there are some subtle implications of these conversions that should 151 be completely understood when using boolean values with input variables. 152 153 It is recommended for now to specify boolean values for variables as the 154 strings `"true"` and `"false"`, to avoid some caveats in the conversion 155 process. A future version of Terraform will properly support boolean values 156 and so relying on the current behavior could result in 157 backwards-incompatibilities at that time. 158 159 For a configuration such as the following: 160 161 ```hcl 162 variable "active" { 163 default = false 164 } 165 ``` 166 167 The false is converted to a string `"0"` when running Terraform. 168 169 Then, depending on where you specify overrides, the behavior can differ: 170 171 - Variables with boolean values in a `tfvars` file will likewise be converted to 172 "0" and "1" values. 173 174 - Variables specified via the `-var` command line flag will be literal strings 175 "true" and "false", so care should be taken to explicitly use "0" or "1". 176 177 - Variables specified with the `TF_VAR_` environment variables will be literal 178 string values, just like `-var`. 179 180 A future version of Terraform will fully support first-class boolean 181 types which will make the behavior of booleans consistent as you would 182 expect. This may break some of the above behavior. 183 184 When passing boolean-like variables as parameters to resource configurations 185 that expect boolean values, they are converted consistently: 186 187 - "1" and "true" become `true` 188 - "0" and "false" become `false` 189 190 The behavior of conversion in _this_ direction (string to boolean) will _not_ 191 change in future Terraform versions. Therefore, using these string values 192 rather than literal booleans is recommended when using input variables. 193 194 ## Environment Variables 195 196 Environment variables can be used to set the value of an input variable in 197 the root module. The name of the environment variable must be 198 `TF_VAR_` followed by the variable name, and the value is the value of the 199 variable. 200 201 For example, given the configuration below: 202 203 ```hcl 204 variable "image" {} 205 ``` 206 207 The variable can be set via an environment variable: 208 209 ```shell 210 $ TF_VAR_image=foo terraform apply 211 ``` 212 213 Maps and lists can be specified using environment variables as well using 214 [HCL](./syntax.html#HCL) syntax in the value. 215 216 For a list variable like so: 217 218 ```hcl 219 variable "somelist" { 220 type = "list" 221 } 222 ``` 223 224 The variable could be set like so: 225 226 ```shell 227 $ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan 228 ``` 229 230 Similarly, for a map declared like: 231 232 ```hcl 233 variable "somemap" { 234 type = "map" 235 } 236 ``` 237 238 The value can be set like this: 239 240 ```shell 241 $ TF_VAR_somemap='{foo = "bar", baz = "qux"}' terraform plan 242 ``` 243 244 ## Variable Files 245 246 Values for the input variables of a root module can be gathered in 247 _variable definition files_ and passed together using the `-var-file=FILE` 248 option. 249 250 For all files which match `terraform.tfvars` or `*.auto.tfvars` present in the 251 current directory, Terraform automatically loads them to populate variables. If 252 the file is located somewhere else, you can pass the path to the file using the 253 `-var-file` flag. It is recommended to name such files with names ending in 254 `.tfvars`. 255 256 Variables files use HCL or JSON syntax to define variable values. Strings, lists 257 or maps may be set in the same manner as the default value in a `variable` block 258 in Terraform configuration. For example: 259 260 ```hcl 261 foo = "bar" 262 xyz = "abc" 263 264 somelist = [ 265 "one", 266 "two", 267 ] 268 269 somemap = { 270 foo = "bar" 271 bax = "qux" 272 } 273 ``` 274 275 The `-var-file` flag can be used multiple times per command invocation: 276 277 ```shell 278 $ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 279 ``` 280 281 -> **Note**: Variable files are evaluated in the order in which they are 282 specified on the command line. If a particular variable is defined in more than 283 one variable file, the last value specified is effective. 284 285 ### Variable Merging 286 287 When multiple values are provided for the same input variable, map values are 288 merged while all other values are overridden by the last definition. 289 290 For example, if you define a variable twice on the command line: 291 292 ```shell 293 $ terraform apply -var foo=bar -var foo=baz 294 ``` 295 296 Then the value of `foo` will be `baz`, since it was the last definition seen. 297 298 However, for maps, the values are merged: 299 300 ```shell 301 $ terraform apply -var 'foo={quux="bar"}' -var 'foo={bar="baz"}' 302 ``` 303 304 The resulting value of `foo` will be: 305 306 ```shell 307 { 308 quux = "bar" 309 bar = "baz" 310 } 311 ``` 312 313 There is no way currently to unset map values in Terraform. Whenever a map 314 is modified either via variable input or being passed into a module, the 315 values are always merged. 316 317 ### Variable Precedence 318 319 Both these files have the variable `baz` defined: 320 321 _foo.tfvars_ 322 323 ```hcl 324 baz = "foo" 325 ``` 326 327 _bar.tfvars_ 328 329 ```hcl 330 baz = "bar" 331 ``` 332 333 When they are passed in the following order: 334 335 ```shell 336 $ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 337 ``` 338 339 The result will be that `baz` will contain the value `bar` because `bar.tfvars` 340 has the last definition loaded. 341 342 Definition files passed using the `-var-file` flag will always be evaluated after 343 those in the working directory. 344 345 Values passed within definition files or with `-var` will take precedence over 346 `TF_VAR_` environment variables, as environment variables are considered defaults.