github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/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  variable "zones" {
    40    default = ["us-east-1a", "us-east-1b"]
    41  }
    42  ```
    43  
    44  ## Description
    45  
    46  The `variable` block configures a single input variable for
    47  a Terraform configuration. Multiple variables blocks can be used to
    48  add multiple variables.
    49  
    50  The `name` given to the variable block is the name used to
    51  set the variable via the CLI as well as reference the variable
    52  throughout the Terraform configuration.
    53  
    54  Within the block (the `{ }`) is configuration for the variable.
    55  These are the parameters that can be set:
    56  
    57    * `type` (optional) - If set this defines the type of the variable.
    58      Valid values are `string`, `list`, and `map`. If this field is omitted, the
    59      variable type will be inferred based on the `default`. If no `default` is
    60      provided, the type is assumed to be `string`.
    61  
    62    * `default` (optional) - This sets a default value for the variable.
    63      If no default is provided, the variable is considered required and
    64      Terraform will error if it is not set. The default value can be any of the
    65      data types Terraform supports. 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  -> **Note**: Default values can be strings, lists, or maps. If a default is
    76  specified, it must match the declared type of the variable.
    77  
    78  ### Strings
    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    default = "value"
    87  }
    88  ```
    89  
    90  A multi-line string value can be provided using heredoc syntax.
    91  
    92  ```
    93  variable "long_key" {
    94    type = "string"
    95    default = <<EOF
    96  This is a long key.
    97  Running over several lines.
    98  EOF
    99  }
   100  ```
   101  
   102  ### Maps
   103  
   104  A map allows a key to contain a lookup table. This is useful
   105  for some values that change depending on some external pivot.
   106  A common use case for this is mapping cloud images to regions.
   107  An example:
   108  
   109  ```
   110  variable "images" {
   111    type = "map"
   112    default = {
   113      us-east-1 = "image-1234"
   114      us-west-2 = "image-4567"
   115    }
   116  }
   117  ```
   118  
   119  ### Lists
   120  
   121  A list can also be useful to store certain variables. For example:
   122  
   123  ```
   124  variable "users" {
   125    type    = "list"
   126    default = ["admin", "ubuntu"]
   127  }
   128  ```
   129  
   130  The usage of maps, lists, strings, etc. is documented fully in the
   131  [interpolation syntax](/docs/configuration/interpolation.html)
   132  page.
   133  
   134  ## Syntax
   135  
   136  The full syntax is:
   137  
   138  ```
   139  variable NAME {
   140    [type = TYPE]
   141    [default = DEFAULT]
   142    [description = DESCRIPTION]
   143  }
   144  ```
   145  
   146  where `DEFAULT` is:
   147  
   148  ```
   149  VALUE
   150  
   151  [
   152    VALUE,
   153    ...
   154  ]
   155  
   156  {
   157    KEY = VALUE
   158    ...
   159  }
   160  ```
   161  
   162  ### Booleans
   163  
   164  Although it appears Terraform supports boolean types, they are instead
   165  silently converted to string types. The implications of this are subtle and
   166  should be completely understood if you plan on using boolean values.
   167  
   168  It is instead recommended you avoid using boolean values for now and use
   169  explicit strings. A future version of Terraform will properly support
   170  booleans and using the current behavior could result in backwards-incompatibilities
   171  in the future.
   172  
   173  For a configuration such as the following:
   174  
   175  ```
   176  variable "active" {
   177      default = false
   178  }
   179  ```
   180  
   181  The false is converted to a string `"0"` when running Terraform.
   182  
   183  Then, depending on where you specify overrides, the behavior can differ:
   184  
   185    * Variables with boolean values in a `tfvars` file will likewise be
   186      converted to "0" and "1" values.
   187  
   188    * Variables specified via the `-var` command line flag will be literal
   189      strings "true" and "false", so care should be taken to explicitly use
   190      "0" or "1".
   191  
   192    * Variables specified with the `TF_VAR_` environment variables will
   193      be literal string values, just like `-var`.
   194  
   195  A future version of Terraform will fully support first-class boolean
   196  types which will make the behavior of booleans consistent as you would
   197  expect. This may break some of the above behavior.
   198  
   199  When passing boolean-like variables as parameters to resource configurations
   200  that expect boolean values, they are converted consistently:
   201  
   202    * "1", "true", "t" all become `true`
   203    * "0", "false", "f" all become `false`
   204  
   205  The behavior of conversion above will likely not change in future
   206  Terraform versions. Therefore, simply using string values rather than
   207  booleans for variables is recommended.
   208  
   209  ## Environment Variables
   210  
   211  Environment variables can be used to set the value of a variable.
   212  The key of the environment variable must be `TF_VAR_name` and the value
   213  is the value of the variable.
   214  
   215  For example, given the configuration below:
   216  
   217  ```
   218  variable "image" {}
   219  ```
   220  
   221  The variable can be set via an environment variable:
   222  
   223  ```
   224  $ TF_VAR_image=foo terraform apply
   225  ```
   226  
   227  Maps and lists can be specified using environment variables as well using
   228  [HCL](/docs/configuration/syntax.html#HCL) syntax in the value.
   229  
   230  For a list variable like so:
   231  
   232  ```
   233  variable "somelist" {
   234    type = "list"
   235  }
   236  ```
   237  
   238  The variable could be set like so:
   239  
   240  ```
   241  $ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan
   242  ```
   243  
   244  Similarly, for a map declared like:
   245  
   246  ```
   247  variable "somemap" {
   248    type = "map"
   249  }
   250  ```
   251  
   252  The value can be set like this:
   253  
   254  ```
   255  $ TF_VAR_somemap='{foo = "bar", baz = "qux"}' terraform plan
   256  ```
   257  
   258  ## Variable Files
   259  
   260  <a id="variable-files"></a>
   261  
   262  Variables can be collected in files and passed all at once using the
   263  `-var-file=foo.tfvars` flag.
   264  
   265  If a file named `terraform.tfvars` is present in the current directory,
   266  Terraform automatically loads it to populate variables. If the file is named
   267  something else, you can pass the path to the file using the `-var-file`
   268  flag.
   269  
   270  Variables files use HCL or JSON to define variable values. Strings, lists or
   271  maps may be set in the same manner as the default value in a `variable` block
   272  in Terraform configuration. For example:
   273  
   274  ```
   275  foo = "bar"
   276  xyz = "abc"
   277  somelist = [
   278    "one",
   279    "two",
   280  ]
   281  somemap = {
   282    foo = "bar"
   283    bax = "qux"
   284  }
   285  ```
   286  
   287  The `-var-file` flag can be used multiple times per command invocation:
   288  
   289  ```
   290  terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
   291  ```
   292  
   293  -> **Note**: Variable files are evaluated in the order in which they are specified
   294  on the command line. If a variable is defined in more than one variable file,
   295  the last value specified is effective.
   296  
   297  ### Precedence example
   298  
   299  Both these files have the variable `baz` defined:
   300  
   301  _foo.tfvars_
   302  
   303  ```
   304  baz = "foo"
   305  ```
   306  
   307  _bar.tfvars_
   308  
   309  ```
   310  baz = "bar"
   311  ```
   312  
   313  When they are passed in the following order:
   314  
   315  ```
   316  terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
   317  ```
   318  
   319  The result will be that `baz` will contain the value `bar` because `bar.tfvars`
   320  has the last definition loaded.