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