github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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  ## Mappings
   102  
   103  We've replaced our sensitive strings with variables, but we still
   104  are hardcoding AMIs. Unfortunately, AMIs are specific to the region
   105  that is in use. One option is to just ask the user to input the proper
   106  AMI for the region, but Terraform can do better than that with
   107  _mappings_.
   108  
   109  Mappings are a way to create variables that are lookup tables. An example
   110  will show this best. Let's extract our AMIs into a mapping and add
   111  support for the "us-west-2" region as well:
   112  
   113  ```
   114  variable "amis" {
   115  	default = {
   116  		us-east-1 = "ami-aa7ab6c2"
   117  		us-west-2 = "ami-23f78e13"
   118  	}
   119  }
   120  ```
   121  
   122  A variable becomes a mapping when it has a default value that is a
   123  map like above. There is no way to create a required map.
   124  
   125  Then, replace the "aws\_instance" with the following:
   126  
   127  ```
   128  resource "aws_instance" "example" {
   129  	ami = "${lookup(var.amis, var.region)}"
   130  	instance_type = "t1.micro"
   131  }
   132  ```
   133  
   134  This introduces a new type of interpolation: a function call. The
   135  `lookup` function does a dynamic lookup in a map for a key. The
   136  key is `var.region`, which specifies that the value of the region
   137  variables is the key.
   138  
   139  While we don't use it in our example, it is worth noting that you
   140  can also do a static lookup of a mapping directly with
   141  `${var.amis.us-east-1}`.
   142  
   143  We set defaults, but mappings can also be overridden using the
   144  `-var` and `-var-file` values. For example, if the user wanted to
   145  specify an alternate AMI for us-east-1:
   146  
   147  ```
   148  $ terraform plan -var 'amis.us-east-1=foo'
   149  ...
   150  ```
   151  
   152  ## Next
   153  
   154  Terraform provides variables for parameterizing your configurations.
   155  Mappings let you build lookup tables in cases where that makes sense.
   156  Setting and using variables is uniform throughout your configurations.
   157  
   158  In the next section, we'll take a look at
   159  [output variables](/intro/getting-started/outputs.html) as a mechanism
   160  to expose certain values more prominently to the Terraform operator.