github.com/anuaimi/terraform@v0.6.4-0.20150904235404-2bf9aec61da8/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 recommend using the "terraform.tfvars" file, and ignoring it from
    99  version control.
   100  
   101  <a id="mappings"></a>
   102  ## Mappings
   103  
   104  We've replaced our sensitive strings with variables, but we still
   105  are hardcoding AMIs. Unfortunately, AMIs are specific to the region
   106  that is in use. One option is to just ask the user to input the proper
   107  AMI for the region, but Terraform can do better than that with
   108  _mappings_.
   109  
   110  Mappings are a way to create variables that are lookup tables. An example
   111  will show this best. Let's extract our AMIs into a mapping and add
   112  support for the "us-west-2" region as well:
   113  
   114  ```
   115  variable "amis" {
   116  	default = {
   117  		us-east-1 = "ami-aa7ab6c2"
   118  		us-west-2 = "ami-23f78e13"
   119  	}
   120  }
   121  ```
   122  
   123  A variable becomes a mapping when it has a default value that is a
   124  map like above. There is no way to create a required map.
   125  
   126  Then, replace the "aws\_instance" with the following:
   127  
   128  ```
   129  resource "aws_instance" "example" {
   130  	ami = "${lookup(var.amis, var.region)}"
   131  	instance_type = "t1.micro"
   132  }
   133  ```
   134  
   135  This introduces a new type of interpolation: a function call. The
   136  `lookup` function does a dynamic lookup in a map for a key. The
   137  key is `var.region`, which specifies that the value of the region
   138  variables is the key.
   139  
   140  While we don't use it in our example, it is worth noting that you
   141  can also do a static lookup of a mapping directly with
   142  `${var.amis.us-east-1}`.
   143  
   144  <a id="assigning-mappings"></a>
   145  ## Assigning Mappings
   146  
   147  We set defaults above, but mappings can also be set using the `-var` and
   148  `-var-file` values. For example, if the user wanted to specify an alternate AMI
   149  for us-east-1:
   150  
   151  ```
   152  $ terraform plan -var 'amis.us-east-1=foo'
   153  ...
   154  ```
   155  
   156  **Note**: even if every key will be assigned as input, the variable must be
   157  established as a mapping by setting its default to `{}`.
   158  
   159  Here is an example of setting a mapping's keys from a file. Starting with these
   160  variable definitions:
   161  
   162  ```
   163  variable "region" {}
   164  variable "amis" {
   165    default = {}
   166  }
   167  ```
   168  
   169  You can specify keys in a `terraform.tfvars` file:
   170  
   171  ```
   172  amis.us-east-1 = "ami-abc123"
   173  amis.us-west-2 = "ami-def456"
   174  ```
   175  
   176  And access them via `lookup()`:
   177  
   178  ```
   179  output "ami" {
   180    value = "${lookup(var.amis, var.region)}
   181  }
   182  ```
   183  
   184  Like so:
   185  
   186  ```
   187  $ terraform apply -var region=us-west-2
   188  
   189  Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
   190  
   191  Outputs:
   192  
   193    ami = ami-def456
   194  
   195  ```
   196  
   197  ## Next
   198  
   199  Terraform provides variables for parameterizing your configurations.
   200  Mappings let you build lookup tables in cases where that makes sense.
   201  Setting and using variables is uniform throughout your configurations.
   202  
   203  In the next section, we'll take a look at
   204  [output variables](/intro/getting-started/outputs.html) as a mechanism
   205  to expose certain values more prominently to the Terraform operator.