2014-07-28 19:43:00 +02:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2017-11-09 00:49:32 +01:00
|
|
|
page_title: "Configuring Input Variables"
|
2014-07-28 19:43:00 +02:00
|
|
|
sidebar_current: "docs-config-variables"
|
2014-10-22 05:21:56 +02:00
|
|
|
description: |-
|
2017-11-09 00:49:32 +01:00
|
|
|
Input variables are parameters for Terraform modules.
|
|
|
|
This page covers configuration syntax for variables.
|
2014-07-28 19:43:00 +02:00
|
|
|
---
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
# Input Variable Configuration
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Input variables serve as parameters for a Terraform module.
|
|
|
|
|
|
|
|
When used in the root module of a configuration, variables can be set from CLI
|
|
|
|
arguments and environment variables. For [_child_ modules](/docs/configuration/modules.html),
|
|
|
|
they allow values to pass from parent to child.
|
|
|
|
|
|
|
|
Input variable usage is introduced in the Getting Started guide section
|
|
|
|
[_Input Variables_](/intro/getting-started/variables.html).
|
2014-07-28 19:43:00 +02:00
|
|
|
|
|
|
|
This page assumes you're familiar with the
|
|
|
|
[configuration syntax](/docs/configuration/syntax.html)
|
|
|
|
already.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Input variables can be defined as follows:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
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
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
The `variable` block configures a single input variable for a Terraform module.
|
|
|
|
Each block declares a single variable.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
The name given in the block header is used to assign a value to the variable
|
|
|
|
via the CLI and to reference the variable elsewhere in the configuration.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Within the block body (between `{ }`) is configuration for the variable,
|
|
|
|
which accepts the following arguments:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
- `type` (Optional) - If set this defines the type of the variable. Valid values
|
2017-04-05 17:29:27 +02:00
|
|
|
are `string`, `list`, and `map`. If this field is omitted, the variable type
|
2017-11-09 00:49:32 +01:00
|
|
|
will be inferred based on `default`. If no `default` is provided, the type
|
2017-04-05 17:29:27 +02:00
|
|
|
is assumed to be `string`.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
- `default` (Optional) - This sets a default value for the variable. If no
|
|
|
|
default is provided, Terraform will raise an error if a value is not provided
|
|
|
|
by the caller. The default value can be of any of the supported data types,
|
|
|
|
as described below. If `type` is also set, the given value must be
|
|
|
|
of the specified type.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
- `description` (Optional) - A human-friendly description for the variable. This
|
|
|
|
is primarily for documentation for users using your Terraform configuration.
|
|
|
|
When a module is published in [Terraform Registry](https://registry.terraform.io/),
|
|
|
|
the given description is shown as part of the documentation.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-10-31 01:34:04 +01:00
|
|
|
The name of a variable can be any valid identifier. However, due to the
|
|
|
|
interpretation of [module configuration blocks](/docs/configuration/modules.html),
|
|
|
|
the names `source`, `version` and `providers` are reserved for Terraform's own
|
|
|
|
use and are thus not recommended for any module intended to be used as a
|
|
|
|
child module.
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
The default value of an input variable must be a _literal_ value, containing
|
|
|
|
no interpolation expressions. To assign a name to an expression so that it
|
|
|
|
may be re-used within a module, use [Local Values](/docs/configuration/locals.html)
|
|
|
|
instead.
|
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
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Terraform performs automatic conversion from string values to numeric and
|
|
|
|
boolean values based on context, so in practice string variables may be used
|
|
|
|
to set arguments of any primitive type. For boolean values in particular
|
|
|
|
there are some caveats, described under [_Booleans_](#booleans) below.
|
|
|
|
|
2016-10-24 11:05:25 +02:00
|
|
|
### Maps
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
A map value is a lookup table from string keys to string values. This is
|
|
|
|
useful for selecting a value based on some other provided value.
|
|
|
|
|
|
|
|
A common use of maps is to create a table of machine images per region,
|
|
|
|
as follows:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2014-07-28 19:43:00 +02:00
|
|
|
variable "images" {
|
2017-11-09 00:49:32 +01:00
|
|
|
type = "map"
|
2016-07-12 00:37:51 +02:00
|
|
|
default = {
|
2017-11-09 00:49:32 +01:00
|
|
|
"us-east-1" = "image-1234"
|
|
|
|
"us-west-2" = "image-4567"
|
2016-07-12 00:37:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-10-24 11:05:25 +02:00
|
|
|
### Lists
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
A list value is an ordered sequence of strings indexed by integers starting
|
|
|
|
with zero. 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-31 19:11:18 +01:00
|
|
|
### Booleans
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Although Terraform can automatically convert between boolean and string
|
|
|
|
values, there are some subtle implications of these conversions that should
|
|
|
|
be completely understood when using boolean values with input variables.
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
It is recommended for now to specify boolean values for variables as the
|
|
|
|
strings `"true"` and `"false"`, to avoid some caveats in the conversion
|
|
|
|
process. A future version of Terraform will properly support boolean values
|
|
|
|
and so relying on the current behavior could result in
|
|
|
|
backwards-incompatibilities at that time.
|
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-11-09 00:49:32 +01:00
|
|
|
- "1" and "true" become `true`
|
|
|
|
- "0" and "false" become `false`
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
The behavior of conversion in _this_ direction (string to boolean) will _not_
|
|
|
|
change in future Terraform versions. Therefore, using these string values
|
|
|
|
rather than literal booleans is recommended when using input variables.
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2015-04-22 06:37:03 +02:00
|
|
|
## Environment Variables
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Environment variables can be used to set the value of an input variable in
|
|
|
|
the root module. The name of the environment variable must be
|
|
|
|
`TF_VAR_` followed by the variable name, and the value is the value of the
|
|
|
|
variable.
|
2015-04-22 06:37:03 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Values for the input variables of a root module can be gathered in
|
|
|
|
_variable definition files_ and passed together using the `-var-file=FILE`
|
|
|
|
option.
|
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
|
2017-11-09 00:49:32 +01:00
|
|
|
`-var-file` flag. It is recommended to name such files with names ending in
|
|
|
|
`.tfvars`.
|
2016-10-17 18:39:12 +02:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Variables files use HCL or JSON syntax to define variable values. Strings, lists
|
|
|
|
or maps may be set in the same manner as the default value in a `variable` block
|
2016-10-17 18:39:12 +02:00
|
|
|
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
|
2017-11-09 00:49:32 +01:00
|
|
|
specified on the command line. If a particular 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-11-09 00:49:32 +01:00
|
|
|
When multiple values are provided for the same input variable, map values are
|
|
|
|
merged while all other values are overriden by the last definition.
|
2016-12-10 21:05:58 +01:00
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
For example, if you define a variable twice on the command line:
|
2016-12-10 21:05:58 +01:00
|
|
|
|
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
|
|
|
```
|
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Then the value of `foo` will be `baz`, since it was the last definition seen.
|
2016-12-10 21:05:58 +01:00
|
|
|
|
|
|
|
However, for maps, the values are merged:
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```shell
|
2017-09-01 20:42:06 +02:00
|
|
|
$ terraform apply -var 'foo={quux="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
|
|
|
{
|
2017-09-01 20:42:06 +02:00
|
|
|
quux = "bar"
|
2016-12-10 21:05:58 +01:00
|
|
|
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
|
|
|
|
2017-11-09 00:49:32 +01:00
|
|
|
Definition files passed using the `-var-file` flag will always be evaluated after
|
2017-03-08 05:09:48 +01:00
|
|
|
those in the working directory.
|