github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/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.  The first
    33  two have empty blocks `{}`. The third sets a default. If a default value is
    34  set, the variable is optional. Otherwise, the variable is required. If you run
    35  `terraform plan` now, Terraform will prompt you for the values for unset string
    36  variables.
    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 multiple ways to assign variables. Below is also the order
    57  in which variable values are chosen. If they're found in an option first
    58  below, then the options below are ignored.
    59  
    60  **UI Input:** If you execute `terraform plan` or apply without doing
    61  anything, Terraform will ask you to input the variables interactively.
    62  These variables are not saved, but provides a nice user experience for
    63  getting started with Terraform.
    64  
    65  **Command-line flags:** You can set it directly on the command-line with the
    66  `-var` flag. Any command in Terraform that inspects the configuration
    67  accepts this flag, such as `apply`, `plan`, and `refresh`:
    68  
    69  ```
    70  $ terraform plan \
    71    -var 'access_key=foo' \
    72    -var 'secret_key=bar'
    73  ...
    74  ```
    75  
    76  Once again, setting variables this way will not save them, and they'll
    77  have to be input repeatedly as commands are executed.
    78  
    79  **From a file:** To persist variable values, create
    80  a file and assign variables within this file. Create a file named
    81  "terraform.tfvars" with the following 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 configuration
    92  files. And like Terraform configuration files, these files can also be JSON.
    93  
    94  **From environment variables:** Terraform will read environment variables
    95  in the form of `TF_VAR_name` to find the value for a variable. For example,
    96  the `TF_VAR_access_key` variable can be set to set the `access_key` variable.
    97  
    98  We don't recommend saving usernames and password to version control, But you
    99  can create a local secret variables file and use `-var-file` to load it.
   100  
   101  You can use multiple `-var-file` arguments in a single command, with some
   102  checked in to version control and others not checked in. For example:
   103  
   104  ```
   105  $ terraform plan \
   106    -var-file="secret.tfvars" \
   107    -var-file="production.tfvars"
   108  ```
   109  
   110  <a id="mappings"></a>
   111  ## Mappings
   112  
   113  We've replaced our sensitive strings with variables, but we still
   114  are hardcoding AMIs. Unfortunately, AMIs are specific to the region
   115  that is in use. One option is to just ask the user to input the proper
   116  AMI for the region, but Terraform can do better than that with
   117  _mappings_.
   118  
   119  Mappings are a way to create variables that are lookup tables. An example
   120  will show this best. Let's extract our AMIs into a mapping and add
   121  support for the "us-west-2" region as well:
   122  
   123  ```
   124  variable "amis" {
   125      type = "map"
   126  	default = {
   127  		us-east-1 = "ami-13be557e"
   128  		us-west-2 = "ami-06b94666"
   129  	}
   130  }
   131  ```
   132  
   133  A variable becomes a mapping when it has a type of "map" assigned, or has a
   134  default value that is a map like above.
   135  
   136  Then, replace the "aws\_instance" with the following:
   137  
   138  ```
   139  resource "aws_instance" "example" {
   140  	ami = "${lookup(var.amis, var.region)}"
   141  	instance_type = "t2.micro"
   142  }
   143  ```
   144  
   145  This introduces a new type of interpolation: a function call. The
   146  `lookup` function does a dynamic lookup in a map for a key. The
   147  key is `var.region`, which specifies that the value of the region
   148  variables is the key.
   149  
   150  While we don't use it in our example, it is worth noting that you
   151  can also do a static lookup of a mapping directly with
   152  `${var.amis["us-east-1"]}`.
   153  
   154  <a id="assigning-mappings"></a>
   155  ## Assigning Mappings
   156  
   157  We set defaults above, but mappings can also be set using the `-var` and
   158  `-var-file` values. For example, if the user wanted to specify an alternate AMI
   159  for us-east-1:
   160  
   161  ```
   162  $ terraform plan -var 'amis.us-east-1=foo'
   163  ...
   164  ```
   165  
   166  **Note**: even if every key will be assigned as input, the variable must be
   167  established as a mapping by setting its default to `{}`.
   168  
   169  Here is an example of setting a mapping's keys from a file. Starting with these
   170  variable definitions:
   171  
   172  ```
   173  variable "region" {}
   174  variable "amis" {
   175    default = {}
   176  }
   177  ```
   178  
   179  You can specify keys in a `terraform.tfvars` file:
   180  
   181  ```
   182  amis.us-east-1 = "ami-abc123"
   183  amis.us-west-2 = "ami-def456"
   184  ```
   185  
   186  And access them via `lookup()`:
   187  
   188  ```
   189  output "ami" {
   190    value = "${lookup(var.amis, var.region)}"
   191  }
   192  ```
   193  
   194  Like so:
   195  
   196  ```
   197  $ terraform apply -var region=us-west-2
   198  
   199  Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
   200  
   201  Outputs:
   202  
   203    ami = ami-def456
   204  
   205  ```
   206  
   207  ## Next
   208  
   209  Terraform provides variables for parameterizing your configurations.
   210  Mappings let you build lookup tables in cases where that makes sense.
   211  Setting and using variables is uniform throughout your configurations.
   212  
   213  In the next section, we'll take a look at
   214  [output variables](/intro/getting-started/outputs.html) as a mechanism
   215  to expose certain values more prominently to the Terraform operator.