2014-07-28 19:43:00 +02:00
---
layout: "intro"
page_title: "Input Variables"
sidebar_current: "gettingstarted-variables"
2014-10-22 05:21:56 +02:00
description: |-
2015-01-03 19:31:53 +01:00
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.
2014-07-28 19:43:00 +02:00
---
# Input Variables
You now have enough Terraform knowledge to create useful
2016-10-17 19:35:39 +02:00
configurations, but we're still hard-coding access keys,
AMIs, etc. To become truly shareable and version
controlled, we need to parameterize the configurations. This page
2014-07-28 19:43:00 +02:00
introduces input variables as a way to do this.
## Defining Variables
Let's first extract our access key, secret key, and region
into a few variables. Create another file `variables.tf` with
2016-10-17 19:35:39 +02:00
the following contents.
2016-10-29 02:13:09 +02:00
-> **Note** : that the file can be named anything, since Terraform loads all
2016-10-17 19:35:39 +02:00
files ending in `.tf` in a directory.
2014-07-28 19:43:00 +02:00
```
variable "access_key" {}
variable "secret_key" {}
variable "region" {
2016-07-12 00:37:51 +02:00
default = "us-east-1"
2014-07-28 19:43:00 +02:00
}
```
2016-05-09 01:38:35 +02:00
This defines three variables within your Terraform configuration. The first
two have empty blocks `{}` . The third sets a default. If a default value is
set, the variable is optional. Otherwise, the variable is required. If you run
`terraform plan` now, Terraform will prompt you for the values for unset string
variables.
2014-07-28 19:43:00 +02:00
## Using Variables in Configuration
Next, replace the AWS provider configuration with the following:
```
provider "aws" {
2016-07-12 00:37:51 +02:00
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
region = "${var.region}"
2014-07-28 19:43:00 +02:00
}
```
This uses more interpolations, this time prefixed with `var.` . This
tells Terraform that you're accessing variables. This configures
the AWS provider with the given variables.
## Assigning Variables
2015-04-22 06:37:03 +02:00
There are multiple ways to assign variables. Below is also the order
2016-11-29 20:59:27 +01:00
in which variable values are chosen. The following is the descending order
of precedence in which variables are considered.
2014-07-28 19:43:00 +02:00
2016-10-17 19:35:39 +02:00
#### Command-line flags
You can set variables directly on the command-line with the
2014-07-28 19:43:00 +02:00
`-var` flag. Any command in Terraform that inspects the configuration
accepts this flag, such as `apply` , `plan` , and `refresh` :
```
$ terraform plan \
-var 'access_key=foo' \
-var 'secret_key=bar'
...
```
2014-10-13 20:57:43 +02:00
Once again, setting variables this way will not save them, and they'll
have to be input repeatedly as commands are executed.
2016-10-17 19:35:39 +02:00
#### From a file
To persist variable values, create a file and assign variables within
this file. Create a file named `terraform.tfvars` with the following
contents:
2014-07-28 19:43:00 +02:00
```
access_key = "foo"
secret_key = "bar"
```
2016-10-17 19:35:39 +02:00
If a `terraform.tfvars` file is present in the current directory,
2014-08-12 19:21:30 +02:00
Terraform automatically loads it to populate variables. If the file is
named something else, you can use the `-var-file` flag directly to
2016-10-17 19:35:39 +02:00
specify a file. These files are the same syntax as Terraform
configuration files. And like Terraform configuration files, these files
can also be JSON.
#### From environment variables
2014-07-28 19:43:00 +02:00
2016-10-17 19:35:39 +02:00
Terraform will read environment variables in the form of `TF_VAR_name`
to find the value for a variable. For example, the `TF_VAR_access_key`
variable can be set to set the `access_key` variable.
2015-04-22 06:37:03 +02:00
2015-08-30 08:59:21 +02:00
We don't recommend saving usernames and password to version control, But you
can create a local secret variables file and use `-var-file` to load it.
You can use multiple `-var-file` arguments in a single command, with some
checked in to version control and others not checked in. For example:
```
$ terraform plan \
-var-file="secret.tfvars" \
-var-file="production.tfvars"
```
2016-11-29 20:59:27 +01:00
-> **Note** : Environment variables can only populate string-type variables.
List and map type variables must be populated via one of the other mechanisms.
2014-07-28 19:43:00 +02:00
2016-10-17 19:35:39 +02:00
#### UI Input
If you execute `terraform plan` or apply without doing anything,
Terraform will ask you to input the variables interactively. These
variables are not saved, but provides a nice user experience for getting
started with Terraform.
2016-10-29 02:13:09 +02:00
-> **Note** : UI Input is only supported for string variables. List and map
2016-10-17 19:35:39 +02:00
variables must be populated via one of the other mechanisms.
#### Variable Defaults
2016-07-12 00:37:51 +02:00
2016-10-17 19:35:39 +02:00
If no value is assigned to a variable via any of these methods and the
variable has a `default` key in its declaration, that value will be used
for the variable.
2016-07-12 00:37:51 +02:00
2016-11-29 20:59:27 +01:00
< a id = "lists" > < / a >
## Lists
Lists are defined either explicitly or implicity
```
# implicitly by using brackets [...]
variable "cidrs" { default = [] }
# explicitly
variable "cidrs" { type = "list" }
```
You can specify lists in a `terraform.tfvars` file:
```
cidrs = [ "10.0.0.0/16", "10.1.0.0/16" ]
```
2015-07-01 20:11:21 +02:00
< a id = "mappings" > < / a >
2016-07-12 00:37:51 +02:00
< a id = "maps" > < / a >
## Maps
2014-07-28 19:43:00 +02:00
We've replaced our sensitive strings with variables, but we still
2016-10-17 19:35:39 +02:00
are hard-coding AMIs. Unfortunately, AMIs are specific to the region
2014-07-28 19:43:00 +02:00
that is in use. One option is to just ask the user to input the proper
AMI for the region, but Terraform can do better than that with
2016-07-12 00:37:51 +02:00
_maps_.
2014-07-28 19:43:00 +02:00
2016-07-12 00:37:51 +02:00
Maps are a way to create variables that are lookup tables. An example
will show this best. Let's extract our AMIs into a map and add
2016-10-17 19:35:39 +02:00
support for the `us-west-2` region as well:
2014-07-28 19:43:00 +02:00
```
variable "amis" {
2016-07-12 00:37:51 +02:00
type = "map"
default = {
us-east-1 = "ami-13be557e"
us-west-2 = "ami-06b94666"
}
2014-07-28 19:43:00 +02:00
}
```
2016-10-17 19:35:39 +02:00
A variable can have a `map` type assigned explicitly, or it can be implicitly
2016-07-12 00:37:51 +02:00
declared as a map by specifying a default value that is a map. The above
demonstrates both.
2014-07-28 19:43:00 +02:00
2016-10-27 20:07:19 +02:00
Then, replace the `aws_instance` with the following:
2014-07-28 19:43:00 +02:00
```
resource "aws_instance" "example" {
2016-07-12 00:37:51 +02:00
ami = "${lookup(var.amis, var.region)}"
instance_type = "t2.micro"
2014-07-28 19:43:00 +02:00
}
```
This introduces a new type of interpolation: a function call. The
`lookup` function does a dynamic lookup in a map for a key. The
key is `var.region` , which specifies that the value of the region
variables is the key.
2014-07-29 00:47:34 +02:00
While we don't use it in our example, it is worth noting that you
2016-07-12 00:37:51 +02:00
can also do a static lookup of a map directly with
2016-04-11 19:40:06 +02:00
`${var.amis["us-east-1"]}` .
2014-07-28 19:43:00 +02:00
2016-07-12 00:37:51 +02:00
< a id = "assigning-maps" > < / a >
## Assigning Maps
2015-07-01 20:11:21 +02:00
2016-07-12 00:37:51 +02:00
We set defaults above, but maps can also be set using the `-var` and
`-var-file` values. For example:
2014-07-28 19:43:00 +02:00
```
2016-07-12 00:37:51 +02:00
$ terraform plan -var 'amis={ us-east-1 = "foo", us-west-2 = "bar" }'
2014-07-28 19:43:00 +02:00
...
```
2016-10-29 02:13:09 +02:00
-> **Note** : Even if every key will be assigned as input, the variable must be
2016-07-12 00:37:51 +02:00
established as a map by setting its default to `{}` .
2015-07-01 20:11:21 +02:00
2016-07-12 00:37:51 +02:00
Here is an example of setting a map's keys from a file. Starting with these
2015-07-01 20:11:21 +02:00
variable definitions:
```
variable "region" {}
variable "amis" {
2016-07-12 00:37:51 +02:00
type = "map"
2015-07-01 20:11:21 +02:00
}
```
You can specify keys in a `terraform.tfvars` file:
```
2016-07-12 00:37:51 +02:00
amis = {
us-east-1 = "ami-abc123"
us-west-2 = "ami-def456"
}
2015-07-01 20:11:21 +02:00
```
And access them via `lookup()` :
```
output "ami" {
2015-11-07 04:02:05 +01:00
value = "${lookup(var.amis, var.region)}"
2015-07-01 20:11:21 +02:00
}
```
Like so:
```
$ terraform apply -var region=us-west-2
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
ami = ami-def456
```
2014-07-28 19:43:00 +02:00
## Next
Terraform provides variables for parameterizing your configurations.
2016-07-12 00:37:51 +02:00
Maps let you build lookup tables in cases where that makes sense.
2014-07-28 19:43:00 +02:00
Setting and using variables is uniform throughout your configurations.
In the next section, we'll take a look at
[output variables ](/intro/getting-started/outputs.html ) as a mechanism
to expose certain values more prominently to the Terraform operator.