github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/website/source/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 ``` 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. 58 Valid values are `string`, `list`, and `map`. If this field is omitted, the 59 variable type will be inferred based on the `default`. If no `default` is 60 provided, the type is assumed to be `string`. 61 62 * `default` (optional) - This sets a default value for the variable. 63 If no default is provided, the variable is considered required and 64 Terraform will error if it is not set. The default value can be any of the 65 data types Terraform supports. This is covered in more detail below. 66 67 * `description` (optional) - A human-friendly description for 68 the variable. This is primarily for documentation for users 69 using your Terraform configuration. A future version of Terraform 70 will expose these descriptions as part of some Terraform CLI 71 command. 72 73 ------ 74 75 -> **Note**: Default values can be strings, lists, or maps. If a default is 76 specified, it must match the declared type of the variable. 77 78 ### Strings 79 80 String values are simple and represent a basic key to value 81 mapping where the key is the variable name. An example is: 82 83 ``` 84 variable "key" { 85 type = "string" 86 default = "value" 87 } 88 ``` 89 90 A multi-line string value can be provided using heredoc syntax. 91 92 ``` 93 variable "long_key" { 94 type = "string" 95 default = <<EOF 96 This is a long key. 97 Running over several lines. 98 EOF 99 } 100 ``` 101 102 ### Maps 103 104 A map allows a key to contain a lookup table. This is useful 105 for some values that change depending on some external pivot. 106 A common use case for this is mapping cloud images to regions. 107 An example: 108 109 ``` 110 variable "images" { 111 type = "map" 112 default = { 113 us-east-1 = "image-1234" 114 us-west-2 = "image-4567" 115 } 116 } 117 ``` 118 119 ### Lists 120 121 A list can also be useful to store certain variables. For example: 122 123 ``` 124 variable "users" { 125 type = "list" 126 default = ["admin", "ubuntu"] 127 } 128 ``` 129 130 The usage of maps, lists, strings, etc. is documented fully in the 131 [interpolation syntax](/docs/configuration/interpolation.html) 132 page. 133 134 ## Syntax 135 136 The full syntax is: 137 138 ``` 139 variable NAME { 140 [type = TYPE] 141 [default = DEFAULT] 142 [description = DESCRIPTION] 143 } 144 ``` 145 146 where `DEFAULT` is: 147 148 ``` 149 VALUE 150 151 [ 152 VALUE, 153 ... 154 ] 155 156 { 157 KEY = VALUE 158 ... 159 } 160 ``` 161 162 ## Environment Variables 163 164 Environment variables can be used to set the value of a variable. 165 The key of the environment variable must be `TF_VAR_name` and the value 166 is the value of the variable. 167 168 For example, given the configuration below: 169 170 ``` 171 variable "image" {} 172 ``` 173 174 The variable can be set via an environment variable: 175 176 ``` 177 $ TF_VAR_image=foo terraform apply 178 ``` 179 180 Maps and lists can be specified using environment variables as well using 181 [HCL](/docs/configuration/syntax.html#HCL) syntax in the value. 182 183 For a list variable like so: 184 185 ``` 186 variable "somelist" { 187 type = "list" 188 } 189 ``` 190 191 The variable could be set like so: 192 193 ``` 194 $ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan 195 ``` 196 197 Similarly, for a map declared like: 198 199 ``` 200 variable "somemap" { 201 type = "map" 202 } 203 ``` 204 205 The value can be set like this: 206 207 ``` 208 $ TF_VAR_somemap='{foo = "bar", baz = "qux"}' terraform plan 209 ``` 210 211 ## Variable Files 212 213 <a id="variable-files"></a> 214 215 Variables can be collected in files and passed all at once using the 216 `-var-file=foo.tfvars` flag. 217 218 If a file named `terraform.tfvars` is present in the current directory, 219 Terraform automatically loads it to populate variables. If the file is named 220 something else, you can pass the path to the file using the `-var-file` 221 flag. 222 223 Variables files use HCL or JSON to define variable values. Strings, lists or 224 maps may be set in the same manner as the default value in a `variable` block 225 in Terraform configuration. For example: 226 227 ``` 228 foo = "bar" 229 xyz = "abc" 230 somelist = [ 231 "one", 232 "two", 233 ] 234 somemap = { 235 foo = "bar" 236 bax = "qux" 237 } 238 ``` 239 240 The `-var-file` flag can be used multiple times per command invocation: 241 242 ``` 243 terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 244 ``` 245 246 -> **Note**: Variable files are evaluated in the order in which they are specified 247 on the command line. If a variable is defined in more than one variable file, 248 the last value specified is effective. 249 250 ### Precedence example 251 252 Both these files have the variable `baz` defined: 253 254 _foo.tfvars_ 255 256 ``` 257 baz = "foo" 258 ``` 259 260 _bar.tfvars_ 261 262 ``` 263 baz = "bar" 264 ``` 265 266 When they are passed in the following order: 267 268 ``` 269 terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 270 ``` 271 272 The result will be that `baz` will contain the value `bar` because `bar.tfvars` 273 has the last definition loaded.