github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/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 **Default values** can be strings, lists, or maps. If a default is specified, 76 it must match the declared type of the variable. 77 78 String values are simple and represent a basic key to value 79 mapping where the key is the variable name. An example is: 80 81 ``` 82 variable "key" { 83 type = "string" 84 default = "value" 85 } 86 ``` 87 88 A map allows a key to contain a lookup table. This is useful 89 for some values that change depending on some external pivot. 90 A common use case for this is mapping cloud images to regions. 91 An example: 92 93 ``` 94 variable "images" { 95 type = "map" 96 default = { 97 us-east-1 = "image-1234" 98 us-west-2 = "image-4567" 99 } 100 } 101 ``` 102 103 A list can also be useful to store certain variables. For example: 104 105 ``` 106 variable "users" { 107 type = "list" 108 default = ["admin", "ubuntu"] 109 } 110 ``` 111 112 The usage of maps, list, strings, etc. is documented fully in the 113 [interpolation syntax](/docs/configuration/interpolation.html) 114 page. 115 116 ## Syntax 117 118 The full syntax is: 119 120 ``` 121 variable NAME { 122 [type = TYPE] 123 [default = DEFAULT] 124 [description = DESCRIPTION] 125 } 126 ``` 127 128 where `DEFAULT` is: 129 130 ``` 131 VALUE 132 133 [ 134 VALUE, 135 ... 136 ] 137 138 { 139 KEY = VALUE 140 ... 141 } 142 ``` 143 144 ## Environment Variables 145 146 Environment variables can be used to set the value of a variable. 147 The key of the environment variable must be `TF_VAR_name` and the value 148 is the value of the variable. 149 150 For example, given the configuration below: 151 152 ``` 153 variable "image" {} 154 ``` 155 156 The variable can be set via an environment variable: 157 158 ``` 159 $ TF_VAR_image=foo terraform apply 160 ``` 161 162 Maps and lists can be specified using environment variables as well using 163 [HCL](/docs/configuration/syntax.html#HCL) syntax in the value. 164 165 Given the variable declarations: 166 167 ``` 168 variable "somelist" { 169 type = "list" 170 } 171 ``` 172 173 The variable could be set like so: 174 175 ``` 176 $ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan 177 ``` 178 179 Similarly, for a map declared like: 180 181 ``` 182 variable "somemap" { 183 type = "map" 184 } 185 ``` 186 187 The value can be set like this: 188 189 ``` 190 $ TF_VAR_somemap='{foo = "bar", baz = "qux"}' terraform plan 191 ``` 192 193 ## Variable Files 194 195 <a id="variable-files"></a> 196 197 Variables can be collected in files and passed all at once using the 198 `-var-file=foo.tfvars` flag. The format for variables in `.tfvars` 199 files is [HCL](/docs/configuration/syntax.html#HCL), with top level key/value 200 pairs: 201 202 203 ``` 204 foo = "bar" 205 xyz = "abc" 206 somelist = [ 207 "one", 208 "two", 209 ] 210 somemap = { 211 foo = "bar" 212 bax = "qux" 213 } 214 ``` 215 216 The flag can be used multiple times per command invocation: 217 218 ``` 219 terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 220 ``` 221 222 **Note** If a variable is defined in more than one file passed, the last 223 variable file (reading left to right) will be the definition used. Put more 224 simply, the last time a variable is defined is the one which will be used. 225 226 ### Precedence example: 227 228 Both these files have the variable `baz` defined: 229 230 _foo.tfvars_ 231 ``` 232 baz = "foo" 233 ``` 234 235 _bar.tfvars_ 236 ``` 237 baz = "bar" 238 ``` 239 240 When they are passed in the following order: 241 242 ``` 243 terraform apply -var-file=foo.tfvars -var-file=bar.tfvars 244 ``` 245 246 The result will be that `baz` will contain the value `bar` because `bar.tfvars` 247 has the last definition loaded.