github.com/maheshbr/terraform@v0.3.1-0.20141020033300-deec7194a3ea/website/source/intro/getting-started/variables.html.md (about)

     1  ---
     2  layout: "intro"
     3  page_title: "Input Variables"
     4  sidebar_current: "gettingstarted-variables"
     5  ---
     6  
     7  # Input Variables
     8  
     9  You now have enough Terraform knowledge to create useful
    10  configurations, but we're still hardcoding access keys,
    11  AMIs, etc. To become truly shareable and commitable to version
    12  control, we need to parameterize the configurations. This page
    13  introduces input variables as a way to do this.
    14  
    15  ## Defining Variables
    16  
    17  Let's first extract our access key, secret key, and region
    18  into a few variables. Create another file `variables.tf` with
    19  the following contents. Note that the file can be named anything,
    20  since Terraform loads all files ending in `.tf` in a directory.
    21  
    22  ```
    23  variable "access_key" {}
    24  variable "secret_key" {}
    25  variable "region" {
    26  	default = "us-east-1"
    27  }
    28  ```
    29  
    30  This defines three variables within your Terraform configuration.
    31  The first two have empty blocks `{}`. The third sets a default. If
    32  a default value is set, the variable is optional. Otherwise, the
    33  variable is required. If you run `terraform plan` now, Terraform will
    34  error since the required variables are not set.
    35  
    36  ## Using Variables in Configuration
    37  
    38  Next, replace the AWS provider configuration with the following:
    39  
    40  ```
    41  provider "aws" {
    42  	access_key = "${var.access_key}"
    43  	secret_key = "${var.secret_key}"
    44  	region = "${var.region}"
    45  }
    46  ```
    47  
    48  This uses more interpolations, this time prefixed with `var.`. This
    49  tells Terraform that you're accessing variables. This configures
    50  the AWS provider with the given variables.
    51  
    52  ## Assigning Variables
    53  
    54  There are three ways to assign variables.
    55  
    56  First, if you execute `terraform plan` or apply without doing
    57  anythiing, Terraform will ask you to input the variables interactively.
    58  These variables are not saved, but provides a nice user experience for
    59  getting started with Terraform.
    60  
    61  For another option, you can set it directly on the command-line with the
    62  `-var` flag. Any command in Terraform that inspects the configuration
    63  accepts this flag, such as `apply`, `plan`, and `refresh`:
    64  
    65  ```
    66  $ terraform plan \
    67    -var 'access_key=foo' \
    68    -var 'secret_key=bar'
    69  ...
    70  ```
    71  
    72  Once again, setting variables this way will not save them, and they'll
    73  have to be input repeatedly as commands are executed.
    74  
    75  The third way, and the way to persist variable values, is to create
    76  a file and assign variables within this file. Create a file named
    77  "terraform.tfvars" with the following contents:
    78  
    79  ```
    80  access_key = "foo"
    81  secret_key = "bar"
    82  ```
    83  
    84  If a "terraform.tfvars" file is present in the current directory,
    85  Terraform automatically loads it to populate variables. If the file is
    86  named something else, you can use the `-var-file` flag directly to
    87  specify a file. These files are the same syntax as Terraform configuration
    88  files. And like Terraform configuration files, these files can also be JSON.
    89  
    90  We recommend using the "terraform.tfvars" file, and ignoring it from
    91  version control.
    92  
    93  ## Mappings
    94  
    95  We've replaced our sensitive strings with variables, but we still
    96  are hardcoding AMIs. Unfortunately, AMIs are specific to the region
    97  that is in use. One option is to just ask the user to input the proper
    98  AMI for the region, but Terraform can do better than that with
    99  _mappings_.
   100  
   101  Mappings are a way to create variables that are lookup tables. An example
   102  will show this best. Let's extract our AMIs into a mapping and add
   103  support for the "us-west-2" region as well:
   104  
   105  ```
   106  variable "amis" {
   107  	default = {
   108  		us-east-1 = "ami-aa7ab6c2"
   109  		us-west-2 = "ami-23f78e13"
   110  	}
   111  }
   112  ```
   113  
   114  A variable becomes a mapping when it has a default value that is a
   115  map like above. There is no way to create a required map.
   116  
   117  Then, replace the "aws\_instance" with the following:
   118  
   119  ```
   120  resource "aws_instance" "example" {
   121  	ami = "${lookup(var.amis, var.region)}"
   122  	instance_type = "t1.micro"
   123  }
   124  ```
   125  
   126  This introduces a new type of interpolation: a function call. The
   127  `lookup` function does a dynamic lookup in a map for a key. The
   128  key is `var.region`, which specifies that the value of the region
   129  variables is the key.
   130  
   131  While we don't use it in our example, it is worth noting that you
   132  can also do a static lookup of a mapping directly with
   133  `${var.amis.us-east-1}`.
   134  
   135  We set defaults, but mappings can also be overridden using the
   136  `-var` and `-var-file` values. For example, if the user wanted to
   137  specify an alternate AMI for us-east-1:
   138  
   139  ```
   140  $ terraform plan -var 'amis.us-east-1=foo'
   141  ...
   142  ```
   143  
   144  ## Next
   145  
   146  Terraform provides variables for parameterizing your configurations.
   147  Mappings let you build lookup tables in cases where that makes sense.
   148  Setting and using variables is uniform throughout your configurations.
   149  
   150  In the next section, we'll take a look at
   151  [output variables](/intro/getting-started/outputs.html) as a mechanism
   152  to expose certain values more prominently to the Terraform operator.