github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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  ## Syntax
   112  
   113  The full syntax is:
   114  
   115  ```
   116  variable NAME {
   117  	[type = TYPE]
   118  	[default = DEFAULT]
   119  	[description = DESCRIPTION]
   120  }
   121  ```
   122  
   123  where `DEFAULT` is:
   124  
   125  ```
   126  VALUE
   127  
   128  {
   129  	KEY = VALUE
   130  	...
   131  }
   132  ```
   133  
   134  ## Environment Variables
   135  
   136  Environment variables can be used to set the value of a variable.
   137  The key of the environment variable must be `TF_VAR_name` and the value
   138  is the value of the variable.
   139  
   140  For example, given the configuration below:
   141  
   142  ```
   143  variable "image" {}
   144  ```
   145  
   146  The variable can be set via an environment variable:
   147  
   148  ```
   149  $ TF_VAR_image=foo terraform apply
   150  ...
   151  ```
   152  
   153  ## Variable Files
   154  
   155  Variables can be collected in files and passed all at once using the 
   156  `-var-file=foo.tfvars` flag. The format for variables in `.tfvars`
   157  files is:
   158  
   159  ```
   160  foo = "bar"
   161  xyz = "abc"
   162  ```
   163  
   164  The flag can be used multiple times per command invocation:
   165  
   166  ```
   167  terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
   168  ```
   169  
   170  **Note** If a variable is defined in more than one file passed, the last 
   171  variable file (reading left to right) will be the definition used. Put more 
   172  simply, the last time a variable is defined is the one which will be used.
   173  
   174  ### Precedence example:
   175  
   176  Both these files have the variable `baz` defined:
   177  
   178  _foo.tfvars_
   179  ```
   180  baz = "foo"
   181  ```
   182  
   183  _bar.tfvars_
   184  ```
   185  baz = "bar"
   186  ```
   187  
   188  When they are passed in the following order:
   189  
   190  ```
   191  terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
   192  ```
   193  
   194  The result will be that `baz` will contain the value `bar` because `bar.tfvars`
   195  has the last definition loaded.
   196  
   197