github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/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 two ways to assign variables.
    55  
    56  First, you can set it directly on the command-line with the
    57  `-var` flag. Any command in Terraform that inspects the configuration
    58  accepts this flag, such as `apply`, `plan`, and `refresh`:
    59  
    60  ```
    61  $ terraform plan \
    62    -var 'access_key=foo' \
    63    -var 'secret_key=bar'
    64  ...
    65  ```
    66  
    67  Second, you can create a file and assign variables directly. Create
    68  a file named "terraform.tfvars" with the following contents:
    69  
    70  ```
    71  access_key = "foo"
    72  secret_key = "bar"
    73  ```
    74  
    75  If a "terraform.tfvars" file is present, Terraform automatically loads
    76  it to populate variables. If the file is named something else, you can
    77  use the `-var-file` flag directly to specify a file.
    78  
    79  We recommend using the "terraform.tfvars" file, and ignoring it from
    80  version control.
    81  
    82  ## Mappings
    83  
    84  We've replaced our sensitive strings with variables, but we still
    85  are hardcoding AMIs. Unfortunately, AMIs are specific to the region
    86  that is in use. One option is to just ask the user to input the proper
    87  AMI for the region, but Terraform can do better than that with
    88  _mappings_.
    89  
    90  Mappings are a way to create variables that are lookup tables. An example
    91  will show this best. Let's extract our AMIs into a mapping and add
    92  support for the "us-west-2" region as well:
    93  
    94  ```
    95  variable "amis" {
    96  	default = {
    97  		"us-east-1": "ami-aa7ab6c2",
    98  		"us-west-2": "ami-23f78e13",
    99  	}
   100  }
   101  ```
   102  
   103  A variable becomes a mapping when it has a default value that is a
   104  map like above. There is no way to create a required map.
   105  
   106  Then, replace the "aws\_instance" with the following:
   107  
   108  ```
   109  resource "aws_instance" "example" {
   110  	ami = "${lookup(var.amis, var.region)}"
   111  	instance_type = "t1.micro"
   112  }
   113  ```
   114  
   115  This introduces a new type of interpolation: a function call. The
   116  `lookup` function does a dynamic lookup in a map for a key. The
   117  key is `var.region`, which specifies that the value of the region
   118  variables is the key.
   119  
   120  While we don't use it in our example, it is worth noting that you
   121  can also do a static lookup of a mapping directly with
   122  `${var.amis.us-east-1}`.
   123  
   124  We set defaults, but mappings can also be overridden using the
   125  `-var` and `-var-file` values. For example, if the user wanted to
   126  specify an alternate AMI for us-east-1:
   127  
   128  ```
   129  $ terraform plan -var 'amis.us-east-1=foo'
   130  ...
   131  ```
   132  
   133  ## Next
   134  
   135  Terraform provides variables for parameterizing your configurations.
   136  Mappings let you build lookup tables in cases where that make sense.
   137  Setting and using variables is uniform throughout your configurations.
   138  
   139  In the next section, we'll take a look at
   140  [output variables](/intro/getting-started/outputs.html) as a mechanism
   141  to expose certain values more prominently to the Terraform operator.