github.com/bpineau/terraform@v0.8.0-rc1.0.20161126184705-a8886012d185/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 of precedence in which variables are considered.
    60  
    61  #### Command-line flags
    62  
    63  You can set variables 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  #### From a file
    78  
    79  To persist variable values, create a file and assign variables within
    80  this file. Create a file named `terraform.tfvars` with the following
    81  contents:
    82  
    83  ```
    84  access_key = "foo"
    85  secret_key = "bar"
    86  ```
    87  
    88  If a `terraform.tfvars` file is present in the current directory,
    89  Terraform automatically loads it to populate variables. If the file is
    90  named something else, you can use the `-var-file` flag directly to
    91  specify a file. These files are the same syntax as Terraform
    92  configuration files. And like Terraform configuration files, these files
    93  can also be JSON.
    94  
    95  #### From environment variables
    96  
    97  Terraform will read environment variables in the form of `TF_VAR_name`
    98  to find the value for a variable. For example, the `TF_VAR_access_key`
    99  variable can be set to set the `access_key` variable.
   100  
   101  We don't recommend saving usernames and password to version control, But you
   102  can create a local secret variables file and use `-var-file` to load it.
   103  
   104  You can use multiple `-var-file` arguments in a single command, with some
   105  checked in to version control and others not checked in. For example:
   106  
   107  ```
   108  $ terraform plan \
   109    -var-file="secret.tfvars" \
   110    -var-file="production.tfvars"
   111  ```
   112  
   113  #### UI Input
   114  
   115  If you execute `terraform plan` or apply without doing anything,
   116  Terraform will ask you to input the variables interactively.  These
   117  variables are not saved, but provides a nice user experience for getting
   118  started with Terraform.
   119  
   120  -> **Note**: UI Input is only supported for string variables. List and map
   121  variables must be populated via one of the other mechanisms.
   122  
   123  #### Variable Defaults
   124  
   125  If no value is assigned to a variable via any of these methods and the
   126  variable has a `default` key in its declaration, that value will be used
   127  for the variable.
   128  
   129  <a id="mappings"></a>
   130  <a id="maps"></a>
   131  ## Maps
   132  
   133  We've replaced our sensitive strings with variables, but we still
   134  are hard-coding AMIs. Unfortunately, AMIs are specific to the region
   135  that is in use. One option is to just ask the user to input the proper
   136  AMI for the region, but Terraform can do better than that with
   137  _maps_.
   138  
   139  Maps are a way to create variables that are lookup tables. An example
   140  will show this best. Let's extract our AMIs into a map and add
   141  support for the `us-west-2` region as well:
   142  
   143  ```
   144  variable "amis" {
   145    type = "map"
   146    default = {
   147      us-east-1 = "ami-13be557e"
   148      us-west-2 = "ami-06b94666"
   149    }
   150  }
   151  ```
   152  
   153  A variable can have a `map` type assigned explicitly, or it can be implicitly
   154  declared as a map by specifying a default value that is a map. The above
   155  demonstrates both.
   156  
   157  Then, replace the `aws_instance` with the following:
   158  
   159  ```
   160  resource "aws_instance" "example" {
   161    ami           = "${lookup(var.amis, var.region)}"
   162    instance_type = "t2.micro"
   163  }
   164  ```
   165  
   166  This introduces a new type of interpolation: a function call. The
   167  `lookup` function does a dynamic lookup in a map for a key. The
   168  key is `var.region`, which specifies that the value of the region
   169  variables is the key.
   170  
   171  While we don't use it in our example, it is worth noting that you
   172  can also do a static lookup of a map directly with
   173  `${var.amis["us-east-1"]}`.
   174  
   175  <a id="assigning-maps"></a>
   176  ## Assigning Maps
   177  
   178  We set defaults above, but maps can also be set using the `-var` and
   179  `-var-file` values. For example:
   180  
   181  ```
   182  $ terraform plan -var 'amis={ us-east-1 = "foo", us-west-2 = "bar" }'
   183  ...
   184  ```
   185  
   186  -> **Note**: Even if every key will be assigned as input, the variable must be
   187  established as a map by setting its default to `{}`.
   188  
   189  Here is an example of setting a map's keys from a file. Starting with these
   190  variable definitions:
   191  
   192  ```
   193  variable "region" {}
   194  variable "amis" {
   195    type = "map"
   196  }
   197  ```
   198  
   199  You can specify keys in a `terraform.tfvars` file:
   200  
   201  ```
   202  amis = {
   203    us-east-1 = "ami-abc123"
   204    us-west-2 = "ami-def456"
   205  }
   206  ```
   207  
   208  And access them via `lookup()`:
   209  
   210  ```
   211  output "ami" {
   212    value = "${lookup(var.amis, var.region)}"
   213  }
   214  ```
   215  
   216  Like so:
   217  
   218  ```
   219  $ terraform apply -var region=us-west-2
   220  
   221  Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
   222  
   223  Outputs:
   224  
   225    ami = ami-def456
   226  
   227  ```
   228  
   229  ## Next
   230  
   231  Terraform provides variables for parameterizing your configurations.
   232  Maps let you build lookup tables in cases where that makes sense.
   233  Setting and using variables is uniform throughout your configurations.
   234  
   235  In the next section, we'll take a look at
   236  [output variables](/intro/getting-started/outputs.html) as a mechanism
   237  to expose certain values more prominently to the Terraform operator.