github.com/erriapo/terraform@v0.6.12-0.20160203182612-0340ea72354f/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 40 ## Description 41 42 The `variable` block configures a single input variable for 43 a Terraform configuration. Multiple variables blocks can be used to 44 add multiple variables. 45 46 The `name` given to the variable block is the name used to 47 set the variable via the CLI as well as reference the variable 48 throughout the Terraform configuration. 49 50 Within the block (the `{ }`) is configuration for the variable. 51 These are the parameters that can be set: 52 53 * `type` (optional) - If set this defines the type of the variable. 54 Valid values are `string` and `map`. In older versions of Terraform 55 this parameter did not exist, and the type was inferred from the 56 default value, defaulting to `string` if no default was set. If a 57 type is not specified, the previous behavior is maintained. It is 58 recommended to set variable types explicitly in preference to relying 59 on inferrence - this allows variables of type `map` to be set in the 60 `terraform.tfvars` file without requiring a default value to be set. 61 62 * `default` (optional) - If set, this sets a default value 63 for the variable. If this isn't set, the variable is required 64 and Terraform will error if not set. The default value can be 65 a string or a mapping. 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 either strings or maps, and if specified 76 must match the declared type of the variable. If no value is supplied 77 for a variable of type `map`, the values must be supplied in a 78 `terraform.tfvars` file - they cannot be input via the console. 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 87 default = "value" 88 } 89 ``` 90 91 A map allows a key to contain a lookup table. This is useful 92 for some values that change depending on some external pivot. 93 A common use case for this is mapping cloud images to regions. 94 An example: 95 96 ``` 97 variable "images" { 98 type = "map" 99 100 default = { 101 us-east-1 = "image-1234" 102 us-west-2 = "image-4567" 103 } 104 } 105 ``` 106 107 The usage of maps, strings, etc. is documented fully in the 108 [interpolation syntax](/docs/configuration/interpolation.html) 109 page. 110 111 ## Environment Variables 112 113 Environment variables can be used to set the value of a variable. 114 The key of the environment variable must be `TF_VAR_name` and the value 115 is the value of the variable. 116 117 For example, given the configuration below: 118 119 ``` 120 variable "image" {} 121 ``` 122 123 The variable can be set via an environment variable: 124 125 ``` 126 $ TF_VAR_image=foo terraform apply 127 ... 128 ``` 129 130 ## Syntax 131 132 The full syntax is: 133 134 ``` 135 variable NAME { 136 [type = TYPE] 137 [default = DEFAULT] 138 [description = DESCRIPTION] 139 } 140 ``` 141 142 where `DEFAULT` is: 143 144 ``` 145 VALUE 146 147 { 148 KEY = VALUE 149 ... 150 } 151 ```