github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/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 hardcoding access keys, 13 AMIs, etc. To become truly shareable and committable to version 14 control, 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. Note that the file can be named anything, 22 since Terraform loads all files ending in `.tf` in a directory. 23 24 ``` 25 variable "access_key" {} 26 variable "secret_key" {} 27 variable "region" { 28 default = "us-east-1" 29 } 30 ``` 31 32 This defines three variables within your Terraform configuration. 33 The first two have empty blocks `{}`. The third sets a default. If 34 a default value is set, the variable is optional. Otherwise, the 35 variable is required. If you run `terraform plan` now, Terraform will 36 error since the required variables are not set. 37 38 ## Using Variables in Configuration 39 40 Next, replace the AWS provider configuration with the following: 41 42 ``` 43 provider "aws" { 44 access_key = "${var.access_key}" 45 secret_key = "${var.secret_key}" 46 region = "${var.region}" 47 } 48 ``` 49 50 This uses more interpolations, this time prefixed with `var.`. This 51 tells Terraform that you're accessing variables. This configures 52 the AWS provider with the given variables. 53 54 ## Assigning Variables 55 56 There are three ways to assign variables. 57 58 First, if you execute `terraform plan` or apply without doing 59 anything, Terraform will ask you to input the variables interactively. 60 These variables are not saved, but provides a nice user experience for 61 getting started with Terraform. 62 63 For another option, you can set it directly on the command-line with the 64 `-var` flag. Any command in Terraform that inspects the configuration 65 accepts this flag, such as `apply`, `plan`, and `refresh`: 66 67 ``` 68 $ terraform plan \ 69 -var 'access_key=foo' \ 70 -var 'secret_key=bar' 71 ... 72 ``` 73 74 Once again, setting variables this way will not save them, and they'll 75 have to be input repeatedly as commands are executed. 76 77 The third way, and the way to persist variable values, is to create 78 a file and assign variables within this file. Create a file named 79 "terraform.tfvars" with the following contents: 80 81 ``` 82 access_key = "foo" 83 secret_key = "bar" 84 ``` 85 86 If a "terraform.tfvars" file is present in the current directory, 87 Terraform automatically loads it to populate variables. If the file is 88 named something else, you can use the `-var-file` flag directly to 89 specify a file. These files are the same syntax as Terraform configuration 90 files. And like Terraform configuration files, these files can also be JSON. 91 92 We recommend using the "terraform.tfvars" file, and ignoring it from 93 version control. 94 95 ## Mappings 96 97 We've replaced our sensitive strings with variables, but we still 98 are hardcoding AMIs. Unfortunately, AMIs are specific to the region 99 that is in use. One option is to just ask the user to input the proper 100 AMI for the region, but Terraform can do better than that with 101 _mappings_. 102 103 Mappings are a way to create variables that are lookup tables. An example 104 will show this best. Let's extract our AMIs into a mapping and add 105 support for the "us-west-2" region as well: 106 107 ``` 108 variable "amis" { 109 default = { 110 us-east-1 = "ami-aa7ab6c2" 111 us-west-2 = "ami-23f78e13" 112 } 113 } 114 ``` 115 116 A variable becomes a mapping when it has a default value that is a 117 map like above. There is no way to create a required map. 118 119 Then, replace the "aws\_instance" with the following: 120 121 ``` 122 resource "aws_instance" "example" { 123 ami = "${lookup(var.amis, var.region)}" 124 instance_type = "t1.micro" 125 } 126 ``` 127 128 This introduces a new type of interpolation: a function call. The 129 `lookup` function does a dynamic lookup in a map for a key. The 130 key is `var.region`, which specifies that the value of the region 131 variables is the key. 132 133 While we don't use it in our example, it is worth noting that you 134 can also do a static lookup of a mapping directly with 135 `${var.amis.us-east-1}`. 136 137 We set defaults, but mappings can also be overridden using the 138 `-var` and `-var-file` values. For example, if the user wanted to 139 specify an alternate AMI for us-east-1: 140 141 ``` 142 $ terraform plan -var 'amis.us-east-1=foo' 143 ... 144 ``` 145 146 ## Next 147 148 Terraform provides variables for parameterizing your configurations. 149 Mappings let you build lookup tables in cases where that makes sense. 150 Setting and using variables is uniform throughout your configurations. 151 152 In the next section, we'll take a look at 153 [output variables](/intro/getting-started/outputs.html) as a mechanism 154 to expose certain values more prominently to the Terraform operator.