2014-07-28 19:43:00 +02:00
---
layout: "docs"
page_title: "Configuring Variables"
sidebar_current: "docs-config-variables"
2014-10-22 05:21:56 +02:00
description: |-
Variables define the parameterization of Terraform configurations. Variables can be overridden via the CLI. Variable usage is covered in more detail in the getting started guide. This page covers configuration syntax for variables.
2014-07-28 19:43:00 +02:00
---
# Variable Configuration
Variables define the parameterization of Terraform configurations.
Variables can be overridden via the CLI. Variable usage is
covered in more detail in the
[getting started guide ](/intro/getting-started/variables.html ).
This page covers configuration syntax for variables.
This page assumes you're familiar with the
[configuration syntax ](/docs/configuration/syntax.html )
already.
## Example
A variable configuration looks like the following:
2017-04-05 17:29:27 +02:00
```hcl
2016-01-21 20:33:16 +01:00
variable "key" {
2016-07-12 00:37:51 +02:00
type = "string"
2016-01-21 20:33:16 +01:00
}
2014-07-28 19:43:00 +02:00
variable "images" {
2016-07-12 00:37:51 +02:00
type = "map"
2016-01-21 20:33:16 +01:00
2016-07-12 00:37:51 +02:00
default = {
us-east-1 = "image-1234"
us-west-2 = "image-4567"
}
}
variable "zones" {
default = ["us-east-1a", "us-east-1b"]
2014-07-28 19:43:00 +02:00
}
```
2014-08-07 09:19:56 +02:00
## Description
2014-07-28 19:43:00 +02:00
2016-07-12 00:37:51 +02:00
The `variable` block configures a single input variable for
2014-07-28 19:43:00 +02:00
a Terraform configuration. Multiple variables blocks can be used to
add multiple variables.
2016-01-21 20:33:16 +01:00
The `name` given to the variable block is the name used to
2014-07-28 19:43:00 +02:00
set the variable via the CLI as well as reference the variable
throughout the Terraform configuration.
Within the block (the `{ }` ) is configuration for the variable.
These are the parameters that can be set:
2017-04-05 17:29:27 +02:00
- `type` (optional) - If set this defines the type of the variable. Valid values
are `string` , `list` , and `map` . If this field is omitted, the variable type
will be inferred based on the `default` . If no `default` is provided, the type
is assumed to be `string` .
2014-07-28 19:43:00 +02:00
2017-04-05 17:29:27 +02:00
- `default` (optional) - This sets a default value for the variable. If no
default is provided, the variable is considered required and Terraform will
error if it is not set. The default value can be any of the data types
Terraform supports. This is covered in more detail below.
2014-07-28 19:43:00 +02:00
2017-04-05 17:29:27 +02:00
- `description` (optional) - A human-friendly description for the variable. This
is primarily for documentation for users using your Terraform configuration. A
future version of Terraform will expose these descriptions as part of some
Terraform CLI command.
2014-07-28 19:43:00 +02:00
2016-10-29 02:13:09 +02:00
-> **Note** : Default values can be strings, lists, or maps. If a default is
2016-10-17 19:35:39 +02:00
specified, it must match the declared type of the variable.
2014-07-28 19:43:00 +02:00
2016-10-24 11:05:25 +02:00
### Strings
2014-07-28 19:43:00 +02:00
String values are simple and represent a basic key to value
mapping where the key is the variable name. An example is:
2017-04-05 17:29:27 +02:00
```hcl
2014-07-28 19:43:00 +02:00
variable "key" {
2016-07-12 00:37:51 +02:00
type = "string"
default = "value"
2014-07-28 19:43:00 +02:00
}
```
2016-10-24 11:05:25 +02:00
A multi-line string value can be provided using heredoc syntax.
2017-04-05 17:29:27 +02:00
```hcl
2016-10-24 11:05:25 +02:00
variable "long_key" {
type = "string"
default = < < EOF
This is a long key.
Running over several lines.
EOF
}
```
### Maps
2014-07-28 19:43:00 +02:00
A map allows a key to contain a lookup table. This is useful
for some values that change depending on some external pivot.
A common use case for this is mapping cloud images to regions.
An example:
2017-04-05 17:29:27 +02:00
```hcl
2014-07-28 19:43:00 +02:00
variable "images" {
2016-07-12 00:37:51 +02:00
type = "map"
default = {
us-east-1 = "image-1234"
us-west-2 = "image-4567"
}
}
```
2016-10-24 11:05:25 +02:00
### Lists
2016-07-12 00:37:51 +02:00
A list can also be useful to store certain variables. For example:
2016-01-21 20:33:16 +01:00
2017-04-05 17:29:27 +02:00
```hcl
2016-07-12 00:37:51 +02:00
variable "users" {
type = "list"
default = ["admin", "ubuntu"]
2014-07-28 19:43:00 +02:00
}
```
2016-10-07 17:23:11 +02:00
The usage of maps, lists, strings, etc. is documented fully in the
2014-07-28 19:43:00 +02:00
[interpolation syntax ](/docs/configuration/interpolation.html )
page.
2016-03-02 00:08:57 +01:00
## Syntax
The full syntax is:
2017-04-05 17:29:27 +02:00
```text
2016-03-02 00:08:57 +01:00
variable NAME {
2016-07-12 00:37:51 +02:00
[type = TYPE]
[default = DEFAULT]
[description = DESCRIPTION]
2016-03-02 00:08:57 +01:00
}
```
where `DEFAULT` is:
2017-04-05 17:29:27 +02:00
```text
2016-03-02 00:08:57 +01:00
VALUE
2016-07-12 00:37:51 +02:00
[
VALUE,
...
]
2016-03-02 00:08:57 +01:00
{
2016-07-12 00:37:51 +02:00
KEY = VALUE
...
2016-03-02 00:08:57 +01:00
}
```
2016-10-31 19:11:18 +01:00
### Booleans
Although it appears Terraform supports boolean types, they are instead
silently converted to string types. The implications of this are subtle and
should be completely understood if you plan on using boolean values.
It is instead recommended you avoid using boolean values for now and use
2017-04-05 17:29:27 +02:00
explicit strings. A future version of Terraform will properly support booleans
and using the current behavior could result in backwards-incompatibilities in
the future.
2016-10-31 19:11:18 +01:00
For a configuration such as the following:
2017-04-05 17:29:27 +02:00
```hcl
2016-10-31 19:11:18 +01:00
variable "active" {
2017-04-05 17:29:27 +02:00
default = false
2016-10-31 19:11:18 +01:00
}
```
The false is converted to a string `"0"` when running Terraform.
Then, depending on where you specify overrides, the behavior can differ:
2017-04-05 17:29:27 +02:00
- Variables with boolean values in a `tfvars` file will likewise be converted to
"0" and "1" values.
2016-10-31 19:11:18 +01:00
2017-04-05 17:29:27 +02:00
- Variables specified via the `-var` command line flag will be literal strings
"true" and "false", so care should be taken to explicitly use "0" or "1".
2016-10-31 19:11:18 +01:00
2017-04-05 17:29:27 +02:00
- Variables specified with the `TF_VAR_` environment variables will be literal
string values, just like `-var` .
2016-10-31 19:11:18 +01:00
A future version of Terraform will fully support first-class boolean
types which will make the behavior of booleans consistent as you would
expect. This may break some of the above behavior.
When passing boolean-like variables as parameters to resource configurations
that expect boolean values, they are converted consistently:
2017-04-05 17:29:27 +02:00
- "1", "true", "t" all become `true`
- "0", "false", "f" all become `false`
2016-10-31 19:11:18 +01:00
The behavior of conversion above will likely not change in future
Terraform versions. Therefore, simply using string values rather than
booleans for variables is recommended.
2015-04-22 06:37:03 +02:00
## Environment Variables
Environment variables can be used to set the value of a variable.
The key of the environment variable must be `TF_VAR_name` and the value
is the value of the variable.
For example, given the configuration below:
2017-04-05 17:29:27 +02:00
```hcl
2015-04-22 06:37:03 +02:00
variable "image" {}
```
The variable can be set via an environment variable:
2017-04-05 17:29:27 +02:00
```shell
2015-04-22 06:37:03 +02:00
$ TF_VAR_image=foo terraform apply
2016-07-12 00:37:51 +02:00
```
Maps and lists can be specified using environment variables as well using
[HCL ](/docs/configuration/syntax.html#HCL ) syntax in the value.
2016-10-17 19:35:39 +02:00
For a list variable like so:
2016-07-12 00:37:51 +02:00
2017-04-05 17:29:27 +02:00
```hcl
2016-07-12 00:37:51 +02:00
variable "somelist" {
type = "list"
}
```
The variable could be set like so:
2017-04-05 17:29:27 +02:00
```shell
2016-07-12 00:37:51 +02:00
$ TF_VAR_somelist='["ami-abc123", "ami-bcd234"]' terraform plan
```
Similarly, for a map declared like:
2017-04-05 17:29:27 +02:00
```hcl
2016-07-12 00:37:51 +02:00
variable "somemap" {
type = "map"
}
```
The value can be set like this:
2017-04-05 17:29:27 +02:00
```shell
2016-07-12 00:37:51 +02:00
$ TF_VAR_somemap='{foo = "bar", baz = "qux"}' terraform plan
2015-04-22 06:37:03 +02:00
```
2016-03-02 00:08:57 +01:00
## Variable Files
2014-07-28 19:43:00 +02:00
2016-10-07 17:23:11 +02:00
Variables can be collected in files and passed all at once using the
2016-10-17 19:35:39 +02:00
`-var-file=foo.tfvars` flag.
2016-10-17 18:29:57 +02:00
2017-06-22 03:22:07 +02:00
For all files which match `terraform.tfvars` or `*.auto.tfvars` present in the
current directory, Terraform automatically loads them to populate variables. If
the file is located somewhere else, you can pass the path to the file using the
`-var-file` flag.
2016-10-17 18:39:12 +02:00
Variables files use HCL or JSON to define variable values. Strings, lists or
maps may be set in the same manner as the default value in a `variable` block
in Terraform configuration. For example:
2016-05-25 17:38:32 +02:00
2017-04-05 17:29:27 +02:00
```hcl
2016-04-29 15:06:36 +02:00
foo = "bar"
xyz = "abc"
2017-04-05 17:29:27 +02:00
2016-07-12 00:37:51 +02:00
somelist = [
"one",
"two",
]
2017-04-05 17:29:27 +02:00
2016-07-12 00:37:51 +02:00
somemap = {
foo = "bar"
bax = "qux"
}
2016-04-29 15:06:36 +02:00
```
2016-03-02 00:08:57 +01:00
2016-10-17 19:35:39 +02:00
The `-var-file` flag can be used multiple times per command invocation:
2014-07-28 19:43:00 +02:00
2017-04-05 17:29:27 +02:00
```shell
$ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
2014-07-28 19:43:00 +02:00
```
2017-04-05 17:29:27 +02:00
-> **Note** : Variable files are evaluated in the order in which they are
specified on the command line. If a variable is defined in more than one
variable file, the last value specified is effective.
2016-03-02 00:08:57 +01:00
2016-12-10 21:05:58 +01:00
### Variable Merging
2017-03-12 15:11:19 +01:00
When variables are conflicting, map values are merged and all other values are
2016-12-10 21:05:58 +01:00
overridden. Map values are always merged.
For example, if you set a variable twice on the command line:
2017-04-05 17:29:27 +02:00
```shell
$ terraform apply -var foo=bar -var foo=baz
2016-12-10 21:05:58 +01:00
```
Then the value of `foo` will be `baz` since it was the last value seen.
However, for maps, the values are merged:
2017-04-05 17:29:27 +02:00
```shell
$ terraform apply -var 'foo={foo="bar"}' -var 'foo={bar="baz"}'
2016-12-10 21:05:58 +01:00
```
The resulting value of `foo` will be:
2017-04-05 17:29:27 +02:00
```shell
2016-12-10 21:05:58 +01:00
{
foo = "bar"
bar = "baz"
}
```
There is no way currently to unset map values in Terraform. Whenever a map
is modified either via variable input or being passed into a module, the
values are always merged.
### Variable Precedence
2014-07-28 19:43:00 +02:00
2016-03-02 00:08:57 +01:00
Both these files have the variable `baz` defined:
_foo.tfvars_
2016-10-17 19:35:39 +02:00
2017-04-05 17:29:27 +02:00
```hcl
2016-04-29 15:06:36 +02:00
baz = "foo"
2014-07-28 19:43:00 +02:00
```
2016-03-02 00:08:57 +01:00
_bar.tfvars_
2016-10-17 19:35:39 +02:00
2017-04-05 17:29:27 +02:00
```hcl
2016-04-29 15:06:36 +02:00
baz = "bar"
2014-07-28 19:43:00 +02:00
```
2016-03-02 00:08:57 +01:00
2017-06-22 03:22:07 +02:00
When they are passed in the following order:
2016-03-02 00:08:57 +01:00
2017-04-05 17:29:27 +02:00
```shell
2017-06-22 03:22:07 +02:00
$ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars
2016-03-02 00:08:57 +01:00
```
The result will be that `baz` will contain the value `bar` because `bar.tfvars`
has the last definition loaded.
2017-03-08 05:09:48 +01:00
Definitions passed using the `-var-file` flag will always be evaluated after
those in the working directory.