github.com/aspring/terraform@v0.8.2-0.20161216122603-6a8619a5db2e/website/source/intro/getting-started/variables.html.md (about) 1 --- 2 layout: "intro" 3 page_title: "Input Variables" 4 sidebar_current: "gettingstarted-variables" 5 description: |- 6 You now have enough Terraform knowledge to create useful configurations, but we're still hardcoding access keys, AMIs, etc. To become truly shareable and committable to version control, we need to parameterize the configurations. This page introduces input variables as a way to do this. 7 --- 8 9 # Input Variables 10 11 You now have enough Terraform knowledge to create useful 12 configurations, but we're still hard-coding access keys, 13 AMIs, etc. To become truly shareable and version 14 controlled, we need to parameterize the configurations. This page 15 introduces input variables as a way to do this. 16 17 ## Defining Variables 18 19 Let's first extract our access key, secret key, and region 20 into a few variables. Create another file `variables.tf` with 21 the following contents. 22 23 -> **Note**: that the file can be named anything, since Terraform loads all 24 files ending in `.tf` in a directory. 25 26 ``` 27 variable "access_key" {} 28 variable "secret_key" {} 29 variable "region" { 30 default = "us-east-1" 31 } 32 ``` 33 34 This defines three variables within your Terraform configuration. The first 35 two have empty blocks `{}`. The third sets a default. If a default value is 36 set, the variable is optional. Otherwise, the variable is required. If you run 37 `terraform plan` now, Terraform will prompt you for the values for unset string 38 variables. 39 40 ## Using Variables in Configuration 41 42 Next, replace the AWS provider configuration with the following: 43 44 ``` 45 provider "aws" { 46 access_key = "${var.access_key}" 47 secret_key = "${var.secret_key}" 48 region = "${var.region}" 49 } 50 ``` 51 52 This uses more interpolations, this time prefixed with `var.`. This 53 tells Terraform that you're accessing variables. This configures 54 the AWS provider with the given variables. 55 56 ## Assigning Variables 57 58 There are multiple ways to assign variables. Below is also the order 59 in which variable values are chosen. The following is the descending order 60 of precedence in which variables are considered. 61 62 #### Command-line flags 63 64 You can set variables directly on the command-line with the 65 `-var` flag. Any command in Terraform that inspects the configuration 66 accepts this flag, such as `apply`, `plan`, and `refresh`: 67 68 ``` 69 $ terraform plan \ 70 -var 'access_key=foo' \ 71 -var 'secret_key=bar' 72 ... 73 ``` 74 75 Once again, setting variables this way will not save them, and they'll 76 have to be input repeatedly as commands are executed. 77 78 #### From a file 79 80 To persist variable values, create a file and assign variables within 81 this file. Create a file named `terraform.tfvars` with the following 82 contents: 83 84 ``` 85 access_key = "foo" 86 secret_key = "bar" 87 ``` 88 89 If a `terraform.tfvars` file is present in the current directory, 90 Terraform automatically loads it to populate variables. If the file is 91 named something else, you can use the `-var-file` flag directly to 92 specify a file. These files are the same syntax as Terraform 93 configuration files. And like Terraform configuration files, these files 94 can also be JSON. 95 96 #### From environment variables 97 98 Terraform will read environment variables in the form of `TF_VAR_name` 99 to find the value for a variable. For example, the `TF_VAR_access_key` 100 variable can be set to set the `access_key` variable. 101 102 We don't recommend saving usernames and password to version control, But you 103 can create a local secret variables file and use `-var-file` to load it. 104 105 You can use multiple `-var-file` arguments in a single command, with some 106 checked in to version control and others not checked in. For example: 107 108 ``` 109 $ terraform plan \ 110 -var-file="secret.tfvars" \ 111 -var-file="production.tfvars" 112 ``` 113 -> **Note**: Environment variables can only populate string-type variables. 114 List and map type variables must be populated via one of the other mechanisms. 115 116 #### UI Input 117 118 If you execute `terraform plan` or apply without doing anything, 119 Terraform will ask you to input the variables interactively. These 120 variables are not saved, but provides a nice user experience for getting 121 started with Terraform. 122 123 -> **Note**: UI Input is only supported for string variables. List and map 124 variables must be populated via one of the other mechanisms. 125 126 #### Variable Defaults 127 128 If no value is assigned to a variable via any of these methods and the 129 variable has a `default` key in its declaration, that value will be used 130 for the variable. 131 132 <a id="lists"></a> 133 ## Lists 134 135 Lists are defined either explicitly or implicity 136 ``` 137 # implicitly by using brackets [...] 138 variable "cidrs" { default = [] } 139 140 # explicitly 141 variable "cidrs" { type = "list" } 142 ``` 143 144 You can specify lists in a `terraform.tfvars` file: 145 ``` 146 cidrs = [ "10.0.0.0/16", "10.1.0.0/16" ] 147 ``` 148 149 <a id="mappings"></a> 150 <a id="maps"></a> 151 ## Maps 152 153 We've replaced our sensitive strings with variables, but we still 154 are hard-coding AMIs. Unfortunately, AMIs are specific to the region 155 that is in use. One option is to just ask the user to input the proper 156 AMI for the region, but Terraform can do better than that with 157 _maps_. 158 159 Maps are a way to create variables that are lookup tables. An example 160 will show this best. Let's extract our AMIs into a map and add 161 support for the `us-west-2` region as well: 162 163 ``` 164 variable "amis" { 165 type = "map" 166 default = { 167 us-east-1 = "ami-13be557e" 168 us-west-2 = "ami-06b94666" 169 } 170 } 171 ``` 172 173 A variable can have a `map` type assigned explicitly, or it can be implicitly 174 declared as a map by specifying a default value that is a map. The above 175 demonstrates both. 176 177 Then, replace the `aws_instance` with the following: 178 179 ``` 180 resource "aws_instance" "example" { 181 ami = "${lookup(var.amis, var.region)}" 182 instance_type = "t2.micro" 183 } 184 ``` 185 186 This introduces a new type of interpolation: a function call. The 187 `lookup` function does a dynamic lookup in a map for a key. The 188 key is `var.region`, which specifies that the value of the region 189 variables is the key. 190 191 While we don't use it in our example, it is worth noting that you 192 can also do a static lookup of a map directly with 193 `${var.amis["us-east-1"]}`. 194 195 <a id="assigning-maps"></a> 196 ## Assigning Maps 197 198 We set defaults above, but maps can also be set using the `-var` and 199 `-var-file` values. For example: 200 201 ``` 202 $ terraform plan -var 'amis={ us-east-1 = "foo", us-west-2 = "bar" }' 203 ... 204 ``` 205 206 -> **Note**: Even if every key will be assigned as input, the variable must be 207 established as a map by setting its default to `{}`. 208 209 Here is an example of setting a map's keys from a file. Starting with these 210 variable definitions: 211 212 ``` 213 variable "region" {} 214 variable "amis" { 215 type = "map" 216 } 217 ``` 218 219 You can specify keys in a `terraform.tfvars` file: 220 221 ``` 222 amis = { 223 us-east-1 = "ami-abc123" 224 us-west-2 = "ami-def456" 225 } 226 ``` 227 228 And access them via `lookup()`: 229 230 ``` 231 output "ami" { 232 value = "${lookup(var.amis, var.region)}" 233 } 234 ``` 235 236 Like so: 237 238 ``` 239 $ terraform apply -var region=us-west-2 240 241 Apply complete! Resources: 0 added, 0 changed, 0 destroyed. 242 243 Outputs: 244 245 ami = ami-def456 246 247 ``` 248 249 ## Next 250 251 Terraform provides variables for parameterizing your configurations. 252 Maps let you build lookup tables in cases where that makes sense. 253 Setting and using variables is uniform throughout your configurations. 254 255 In the next section, we'll take a look at 256 [output variables](/intro/getting-started/outputs.html) as a mechanism 257 to expose certain values more prominently to the Terraform operator.