github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  
    28  variable "images" {
    29  	default = {
    30  		us-east-1 = "image-1234"
    31  		us-west-2 = "image-4567"
    32  	}
    33  }
    34  ```
    35  
    36  ## Description
    37  
    38  The `variable`  block configures a single input variable for
    39  a Terraform configuration. Multiple variables blocks can be used to
    40  add multiple variables.
    41  
    42  The `NAME` given to the variable block is the name used to
    43  set the variable via the CLI as well as reference the variable
    44  throughout the Terraform configuration.
    45  
    46  Within the block (the `{ }`) is configuration for the variable.
    47  These are the parameters that can be set:
    48  
    49    * `default` (optional) - If set, this sets a default value
    50      for the variable. If this isn't set, the variable is required
    51      and Terraform will error if not set. The default value can be
    52      a string or a mapping. This is covered in more detail below.
    53  
    54    * `description` (optional) - A human-friendly description for
    55      the variable. This is primarily for documentation for users
    56      using your Terraform configuration. A future version of Terraform
    57      will expose these descriptions as part of some Terraform CLI
    58      command.
    59  
    60  ------
    61  
    62  **Default values** can be either strings or maps. If a default
    63  value is omitted and the variable is required, the value assigned
    64  via the CLI must be a string.
    65  
    66  String values are simple and represent a basic key to value
    67  mapping where the key is the variable name. An example is:
    68  
    69  ```
    70  variable "key" {
    71  	default = "value"
    72  }
    73  ```
    74  
    75  A map allows a key to contain a lookup table. This is useful
    76  for some values that change depending on some external pivot.
    77  A common use case for this is mapping cloud images to regions.
    78  An example:
    79  
    80  ```
    81  variable "images" {
    82  	default = {
    83  		us-east-1 = "image-1234"
    84  		us-west-2 = "image-4567"
    85  	}
    86  }
    87  ```
    88  
    89  The usage of maps, strings, etc. is documented fully in the
    90  [interpolation syntax](/docs/configuration/interpolation.html)
    91  page.
    92  
    93  ## Environment Variables
    94  
    95  Environment variables can be used to set the value of a variable.
    96  The key of the environment variable must be `TF_VAR_name` and the value
    97  is the value of the variable.
    98  
    99  For example, given the configuration below:
   100  
   101  ```
   102  variable "image" {}
   103  ```
   104  
   105  The variable can be set via an environment variable:
   106  
   107  ```
   108  $ TF_VAR_image=foo terraform apply
   109  ...
   110  ```
   111  
   112  ## Syntax
   113  
   114  The full syntax is:
   115  
   116  ```
   117  variable NAME {
   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  ```