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
|
|
|
---
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
# Input Variables
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Input variables serve as parameters for a Terraform module, allowing aspects
|
|
|
|
of the a module to be customized without altering the module's own source code,
|
|
|
|
and allowing modules to be shared between different configurations.
|
2017-11-09 00:49:32 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
When you declare variables in the root module of your configuration, you can
|
|
|
|
set their values using CLI arguments and environment variables.
|
|
|
|
When you declare them in [_child_ modules](/docs/configuration/modules.html),
|
|
|
|
you can use the to pass values from parent to child.
|
2017-11-09 00:49:32 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
## Declaring an Input Variable
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Each input variable accepted by a module must be declared using a `variable`
|
|
|
|
block:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2018-05-06 04:37:47 +02:00
|
|
|
variable "image_id" {
|
|
|
|
type = string
|
2016-01-21 20:33:16 +01:00
|
|
|
}
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
variable "availability_zone_names" {
|
|
|
|
type = list(string)
|
|
|
|
default = ["us-west-1a"]
|
2014-07-28 19:43:00 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
For brevity, input variables are often referred to as just "variables" for
|
|
|
|
short, where it is clear from context what sort of variable is being discussed.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The label after the `variable` keyword is a name for the variable, which must
|
|
|
|
be unique between all variables in the same module. This name is used to
|
|
|
|
assign a value to the variable from outside and to reference the variable's
|
|
|
|
value from within the module.
|
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),
|
2018-05-06 04:37:47 +02:00
|
|
|
the names `source`, `version`, `providers`, `count`, `for_each`, and `lifecycle`
|
|
|
|
are reserved for Terraform's own use and may not be declared as variable names.
|
2016-10-24 11:05:25 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The variable declaration may optionally include a `type` argument, which
|
|
|
|
describes what value types are accepted for the variable, as described
|
|
|
|
in the following section.
|
2016-10-24 11:05:25 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The variable declaration may also include a `default` argument. If present,
|
|
|
|
the variable is considered to be _optional_ and the default value will be used
|
|
|
|
if no overridden value is set when calling the module. The `default` argument
|
|
|
|
requires a literal value and cannot reference other objects in the
|
|
|
|
configuration.
|
2017-11-09 00:49:32 +01:00
|
|
|
|
2018-05-06 05:38:38 +02:00
|
|
|
## Using Input Variable Values
|
|
|
|
|
|
|
|
Within the module that declared a variable, its value can be accessed from
|
|
|
|
within [expressions](/docs/configuration/expressions.html) using an expression
|
|
|
|
like `var.image_id`, where the name after the period corresponds to the label
|
|
|
|
given in the declaration block:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
resource "aws_instance" "example" {
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
ami = var.image_id
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The value assigned to a variable can be accessed only from expressions within
|
|
|
|
the module where it was declared.
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
## Type Constraints
|
2016-10-24 11:05:25 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The `type` argument in a `variable` block allows you to restrict the type of
|
|
|
|
value that will be accepted as the value for a variable. If no type constraint
|
|
|
|
is set then a value of any type is accepted.
|
2017-11-09 00:49:32 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
While type constraints are optional, we recommend specifying them because it
|
|
|
|
serves as helpful additional documentation for users of the module and it
|
|
|
|
allows Terraform to return a helpful error message if the wrong type is used.
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Type constraints are created from a mixture of type keywords and type
|
|
|
|
construction functions. The supported type keywords are:
|
2016-07-12 00:37:51 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
* `string`
|
|
|
|
* `number`
|
|
|
|
* `bool`
|
2016-10-24 11:05:25 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The type construction functions allow you to specify complex types such as
|
|
|
|
collections:
|
2016-01-21 20:33:16 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
* `list(<type>)`
|
|
|
|
* `set(<type>)`
|
|
|
|
* `map(<type>)`
|
|
|
|
* `object({attr_name = <type>, ... })`
|
|
|
|
* `tuple([<type>, ...])`
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The keyword `any` may be used to indicate that any type is acceptable. For
|
|
|
|
more information on the meaning and behavior of these different types,
|
|
|
|
see [the _Expressions_ section](/docs/configuration/expressions.html).
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
If both the `type` and `default` arguments are specified, the given default
|
|
|
|
valuable must be convertible to the specified type.
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
## Input Variable Documentation
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Because the input variables of a module are part of the user interface of
|
|
|
|
the module, you may specify a short description of the purpose of each
|
|
|
|
variable using the optional `description` argument:
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2018-05-06 04:37:47 +02:00
|
|
|
variable "image_id" {
|
|
|
|
type = string
|
|
|
|
description = "The id of the machine image (AMI) to use for the server."
|
2016-10-31 19:11:18 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The description for a variable should be a concise description of the purpose
|
|
|
|
of the variable and what kind of value is expected. This description string
|
|
|
|
may be included in documentation about the module, and so it should be written
|
|
|
|
from the perspective of the user of the module rather than its maintainer. For
|
|
|
|
commentary for module maintainers, use comments.
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
## Assigning Values to Root Module Variables
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
When variables are declared in the root module of your configuration, they
|
|
|
|
can be set in a number of ways:
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
* Individual assignments made on the command line.
|
|
|
|
* Variable definitions files, either specified on the command line or
|
|
|
|
automatically loaded.
|
|
|
|
* Environment variables.
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The following sections describe these options in more detail. This section does
|
|
|
|
not apply to _child_ modules, where values for input variables are instead
|
|
|
|
assigned in the configuration of their parent module, as described in
|
|
|
|
[_Modules_](/docs/configuration/modules.html).
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
### Variables on the Command Line
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
To specify individual modules on the command line, use the `-var` argument
|
|
|
|
that is accepted by the `terraform plan` and `terraform apply` commands:
|
2016-10-31 19:11:18 +01:00
|
|
|
|
2015-04-22 06:37:03 +02:00
|
|
|
```
|
2018-05-06 04:37:47 +02:00
|
|
|
terraform apply -var="image_id=ami-abc123"
|
2016-07-12 00:37:51 +02:00
|
|
|
```
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
### Variable Definitions Files
|
2016-07-12 00:37:51 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
To set lots of variables, it is more convenient to specify their values in
|
|
|
|
a _variable definitions file_, with a filename ending in either `.tfvars`
|
|
|
|
or `.tfvars.json`, and then specify that filename on the command line:
|
2016-07-12 00:37:51 +02:00
|
|
|
|
|
|
|
```
|
2018-05-06 04:37:47 +02:00
|
|
|
terraform apply -var-file="testing.tfvars"
|
2016-07-12 00:37:51 +02:00
|
|
|
```
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
A variable definitions file uses the same basic syntax as Terraform language
|
|
|
|
files, but consists only of variable name assignments:
|
2016-07-12 00:37:51 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2018-05-06 04:37:47 +02:00
|
|
|
image_id = "ami-abc123"
|
|
|
|
availability_zone_names = [
|
|
|
|
"us-east-1a",
|
|
|
|
"us-west-1c",
|
|
|
|
]
|
2015-04-22 06:37:03 +02:00
|
|
|
```
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Terraform also automatically loads a number of variable definitions files
|
|
|
|
automatically if they are present:
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
* Files named exactly `terraform.tfvars` or `terraform.tfvars.json`.
|
|
|
|
* Any files with names ending in either `.auto.tfvars` or `.auto.tfvars.json`.
|
2016-10-17 18:29:57 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Files whose names end with `.json` are parsed instead as JSON objects, with
|
|
|
|
the root object properties corresponding to variable names:
|
2016-10-17 18:39:12 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"image_id": "ami-abc123",
|
|
|
|
"availability_zone_names": ["us-west-1a", "us-west-1c"]
|
2016-07-12 00:37:51 +02:00
|
|
|
}
|
2016-04-29 15:06:36 +02:00
|
|
|
```
|
2016-03-02 00:08:57 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
### Environment Variables
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
As a fallback for the other ways of defining variables, Terraform searches
|
|
|
|
the environment of its own process for environment variables named `TF_VAR_`
|
|
|
|
followed by the name of a declared variable.
|
2016-03-02 00:08:57 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
This can be useful when running Terraform in automation, or when running a
|
|
|
|
sequence of Terraform commands in succession with the same variables.
|
|
|
|
For example, at a `bash` prompt on a Unix system:
|
2016-12-10 21:05:58 +01:00
|
|
|
|
|
|
|
```
|
2018-05-06 04:37:47 +02:00
|
|
|
$ export TF_VAR_image_id=ami-abc123
|
|
|
|
$ terraform plan
|
|
|
|
...
|
2016-12-10 21:05:58 +01:00
|
|
|
```
|
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
On operating systems where environment variable names are case-sensitive,
|
|
|
|
Terraform matches the variable name exactly as given in configuration, and
|
|
|
|
so the required environment variable name will usually have a mix of upper
|
|
|
|
and lower case letters as in the above example.
|
2016-12-10 21:05:58 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
### Complex-typed Values
|
2014-07-28 19:43:00 +02:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
When variable values are provided in a variable definitions file, the usual
|
|
|
|
syntax can be used to assign complex-typed values, like lists and maps.
|
2016-03-02 00:08:57 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
Some special rules apply to the `-var` command line option and to environment
|
|
|
|
variables: to allow string values to be set conveniently, by default values
|
|
|
|
assigned in these ways are interpreted as literal strings, and thus do not
|
|
|
|
need to be themselves quoted:
|
2016-10-17 19:35:39 +02:00
|
|
|
|
2014-07-28 19:43:00 +02:00
|
|
|
```
|
2018-05-06 04:37:47 +02:00
|
|
|
$ export TF_VAR_image_id=ami-abc123
|
2014-07-28 19:43:00 +02:00
|
|
|
```
|
2016-03-02 00:08:57 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
However, if a variable in the root module is declared as being of a complex
|
|
|
|
type (list, set, map, object, or tuple), Terraform will instead attempt to
|
|
|
|
parse it using the same syntax used within variable definitions files,
|
|
|
|
which requires cafeful attention to the string escaping rules in your
|
|
|
|
shell:
|
2016-03-02 00:08:57 +01:00
|
|
|
|
|
|
|
```
|
2018-05-06 04:37:47 +02:00
|
|
|
$ export TF_VAR_availability_zone_names='["us-west-1b","us-west-1d"]'
|
|
|
|
```
|
|
|
|
|
|
|
|
For readability, and to avoid the need to worry about shell escaping, we
|
|
|
|
recommend always setting complex variable values via varable definitions files.
|
2016-03-02 00:08:57 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
### Variable Definition Precedence
|
2017-03-08 05:09:48 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
The above mechanisms for defining variable values can be used together in
|
|
|
|
any combination. If the same variable is assigned multiple values, the
|
|
|
|
processing order is as follows, with the later items in this list taking
|
|
|
|
precedence over the earlier:
|
2017-12-05 20:27:31 +01:00
|
|
|
|
2018-05-06 04:37:47 +02:00
|
|
|
* Environment Variables
|
|
|
|
* The `terraform.tfvars` file, if present.
|
|
|
|
* The `terraform.tfvars.json` file, if present.
|
|
|
|
* Any `-var` and `-var-file` arguments on the command line, in the order they
|
|
|
|
are provided.
|