github.com/jmbataller/terraform@v0.6.8-0.20151125192640-b7a12e3a580c/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.
    33  The first two have empty blocks `{}`. The third sets a default. If
    34  a default value is set, the variable is optional. Otherwise, the
    35  variable is required. If you run `terraform plan` now, Terraform will
    36  error since the required variables are not set.
    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  	default = {
   126  		us-east-1 = "ami-aa7ab6c2"
   127  		us-west-2 = "ami-23f78e13"
   128  	}
   129  }
   130  ```
   131  
   132  A variable becomes a mapping when it has a default value that is a
   133  map like above. There is no way to create a required map.
   134  
   135  Then, replace the "aws\_instance" with the following:
   136  
   137  ```
   138  resource "aws_instance" "example" {
   139  	ami = "${lookup(var.amis, var.region)}"
   140  	instance_type = "t1.micro"
   141  }
   142  ```
   143  
   144  This introduces a new type of interpolation: a function call. The
   145  `lookup` function does a dynamic lookup in a map for a key. The
   146  key is `var.region`, which specifies that the value of the region
   147  variables is the key.
   148  
   149  While we don't use it in our example, it is worth noting that you
   150  can also do a static lookup of a mapping directly with
   151  `${var.amis.us-east-1}`.
   152  
   153  <a id="assigning-mappings"></a>
   154  ## Assigning Mappings
   155  
   156  We set defaults above, but mappings can also be set using the `-var` and
   157  `-var-file` values. For example, if the user wanted to specify an alternate AMI
   158  for us-east-1:
   159  
   160  ```
   161  $ terraform plan -var 'amis.us-east-1=foo'
   162  ...
   163  ```
   164  
   165  **Note**: even if every key will be assigned as input, the variable must be
   166  established as a mapping by setting its default to `{}`.
   167  
   168  Here is an example of setting a mapping's keys from a file. Starting with these
   169  variable definitions:
   170  
   171  ```
   172  variable "region" {}
   173  variable "amis" {
   174    default = {}
   175  }
   176  ```
   177  
   178  You can specify keys in a `terraform.tfvars` file:
   179  
   180  ```
   181  amis.us-east-1 = "ami-abc123"
   182  amis.us-west-2 = "ami-def456"
   183  ```
   184  
   185  And access them via `lookup()`:
   186  
   187  ```
   188  output "ami" {
   189    value = "${lookup(var.amis, var.region)}"
   190  }
   191  ```
   192  
   193  Like so:
   194  
   195  ```
   196  $ terraform apply -var region=us-west-2
   197  
   198  Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
   199  
   200  Outputs:
   201  
   202    ami = ami-def456
   203  
   204  ```
   205  
   206  ## Next
   207  
   208  Terraform provides variables for parameterizing your configurations.
   209  Mappings let you build lookup tables in cases where that makes sense.
   210  Setting and using variables is uniform throughout your configurations.
   211  
   212  In the next section, we'll take a look at
   213  [output variables](/intro/getting-started/outputs.html) as a mechanism
   214  to expose certain values more prominently to the Terraform operator.