2014-07-28 19:43:00 +02:00
---
2020-08-15 03:51:06 +02:00
layout: "language"
2018-12-20 05:34:34 +01:00
page_title: "Input Variables - Configuration Language"
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
2019-01-17 01:30:43 +01:00
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
earlier, see
[0.11 Configuration Language: Input Variables ](../configuration-0-11/variables.html ).
2020-11-06 20:51:11 +01:00
> **Hands-on:** Try the [Customize Terraform Configuration with Variables](https://learn.hashicorp.com/tutorials/terraform/variables?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn.
2020-10-02 20:02:59 +02:00
2018-05-06 04:37:47 +02:00
Input variables serve as parameters for a Terraform module, allowing aspects
2018-12-11 01:14:33 +01:00
of the module to be customized without altering the module's own source code,
2018-05-06 04:37:47 +02:00
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
2018-12-11 01:14:33 +01:00
set their values using CLI options and environment variables.
2020-11-13 03:30:52 +01:00
When you declare them in [child modules ](/docs/configuration/blocks/modules/index.html ),
2018-12-11 01:14:33 +01:00
the calling module should pass values in the `module` block.
2017-11-09 00:49:32 +01:00
2020-09-01 22:28:30 +02:00
If you're familiar with traditional programming languages, it can be useful to
compare Terraform modules to function definitions:
- Input variables are like function arguments.
- [Output values ](./outputs.html ) are like function return values.
- [Local values ](./locals.html ) are like a function's temporary local variables.
2018-12-11 01:14:33 +01:00
-> **Note:** For brevity, input variables are often referred to as just
"variables" or "Terraform variables" when it is clear from context what sort of
variable is being discussed. Other kinds of variables in Terraform include
_environment variables_ (set by the shell where Terraform runs) and _expression
variables_ (used to indirectly represent a value in an
2020-11-13 03:30:52 +01:00
[expression ](/docs/configuration/expressions/index.html )).
2018-12-11 01:14:33 +01: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
}
2019-09-13 21:23:42 +02:00
variable "docker_ports" {
type = list(object({
internal = number
external = number
protocol = string
}))
default = [
{
internal = 8300
external = 8300
protocol = "tcp"
}
]
}
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
2018-12-11 01:14:33 +01:00
be unique among all variables in the same module. This name is used to
2018-05-06 04:37:47 +02:00
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
2018-12-11 01:14:33 +01:00
The name of a variable can be any valid [identifier ](./syntax.html#identifiers )
2020-09-30 17:21:08 +02:00
_except_ the following: `source` , `version` , `providers` , `count` , `for_each` , `lifecycle` , `depends_on` , `locals` .
2018-12-11 01:14:33 +01:00
These names are reserved for meta-arguments in
2020-11-13 03:30:52 +01:00
[module configuration blocks ](/docs/configuration/blocks/modules/syntax.html ), and cannot be
2018-12-11 01:14:33 +01:00
declared as variable names.
2016-10-24 11:05:25 +02:00
2020-09-30 17:21:08 +02:00
## Arguments
Terraform CLI defines the following optional arguments for variable declarations:
- [`default`][inpage-default] - A default value which then makes the variable optional.
- [`type`][inpage-type] - This argument specifies what value types are accepted for the variable.
- [`description`][inpage-description] - This specifies the input variable's documentation.
- [`validation`][inpage-validation] - A block to define validation rules, usually in addition to type constraints.
2020-10-01 19:12:25 +02:00
- [`sensitive`][inpage-sensitive] - Limits Terraform UI output when the variable is used in configuration.
2020-09-30 17:21:08 +02:00
### Default values
[inpage-default]: #default -values
2016-10-24 11:05:25 +02:00
2018-12-11 01:14:33 +01:00
The variable declaration can also include a `default` argument. If present,
2018-05-06 04:37:47 +02:00
the variable is considered to be _optional_ and the default value will be used
2018-12-11 01:14:33 +01:00
if no value is set when calling the module or running Terraform. The `default`
argument requires a literal value and cannot reference other objects in the
2018-05-06 04:37:47 +02:00
configuration.
2017-11-09 00:49:32 +01:00
2020-09-30 17:21:08 +02:00
### Type Constraints
2018-05-06 05:38:38 +02:00
2020-09-30 17:21:08 +02:00
[inpage-type]: #type -constraints
2016-10-24 11:05:25 +02:00
2018-12-11 01:14:33 +01:00
The `type` argument in a `variable` block allows you to restrict the
2020-11-13 03:30:52 +01:00
[type of value ](/docs/configuration/expressions/types.html ) that will be accepted as
2018-12-11 01:14:33 +01:00
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-12-11 01:14:33 +01:00
While type constraints are optional, we recommend specifying them; they
2020-10-24 03:05:19 +02:00
can serve as helpful reminders for users of the module, and they
2018-12-11 01:14:33 +01:00
allow 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
2018-12-11 01:14:33 +01:00
constructors. 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-12-11 01:14:33 +01:00
The type constructors allow you to specify complex types such as
2018-05-06 04:37:47 +02:00
collections:
2016-01-21 20:33:16 +01:00
2018-12-11 01:14:33 +01: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
2018-12-11 01:14:33 +01:00
more information on the meaning and behavior of these different types, as well
as detailed information about automatic conversion of complex types, see
[Type Constraints ](./types.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
2018-12-11 01:14:33 +01:00
value must be convertible to the specified type.
2016-10-31 19:11:18 +01:00
2020-09-30 17:21:08 +02:00
### Input Variable Documentation
[inpage-description]: #input -variable-documentation
2016-10-31 19:11:18 +01:00
2018-12-11 01:14:33 +01:00
Because the input variables of a module are part of its user interface, you can
briefly describe 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-12-11 01:14:33 +01:00
The description should concisely explain the purpose
2018-05-06 04:37:47 +02:00
of the variable and what kind of value is expected. This description string
2018-12-11 01:14:33 +01:00
might be included in documentation about the module, and so it should be written
2018-05-06 04:37:47 +02:00
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
2020-09-30 17:21:08 +02:00
### Custom Validation Rules
[inpage-validation]: #custom -validation-rules
2020-01-04 02:12:49 +01:00
2020-05-27 20:56:15 +02:00
-> This feature was introduced in Terraform CLI v0.13.0.
2020-01-04 02:12:49 +01:00
In addition to Type Constraints as described above, a module author can specify
arbitrary custom validation rules for a particular variable using a `validation`
block nested within the corresponding `variable` block:
```hcl
variable "image_id" {
type = string
description = "The id of the machine image (AMI) to use for the server."
validation {
condition = length(var.image_id) > 4 & & substr(var.image_id, 0, 4) == "ami-"
error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
}
}
```
The `condition` argument is an expression that must use the value of the
variable to return `true` if the value is valid, or `false` if it is invalid.
The expression can refer only to the variable that the condition applies to,
and _must not_ produce errors.
If the failure of an expression is the basis of the validation decision, use
[the `can` function ](./functions/can.html ) to detect such errors. For example:
```hcl
variable "image_id" {
type = string
description = "The id of the machine image (AMI) to use for the server."
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("^ami-", var.image_id))
error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
}
}
```
If `condition` evaluates to `false` , Terraform will produce an error message
that includes the sentences given in `error_message` . The error message string
should be at least one full sentence explaining the constraint that failed,
using a sentence structure similar to the above examples.
2020-10-01 19:09:52 +02:00
### Suppressing Values in CLI Output
2020-09-30 17:53:53 +02:00
2020-10-01 19:09:52 +02:00
[inpage-sensitive]: #suppressing -values-in-cli-output
2020-09-30 17:53:53 +02:00
2020-12-02 00:12:25 +01:00
-> This feature was introduced in Terraform v0.14.0.
> **Hands-on:** Try the [Protect Sensitive Input Variables](https://learn.hashicorp.com/tutorials/terraform/sensitive-variables?in=terraform/configuration-language&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial on HashiCorp Learn.
2020-09-30 17:53:53 +02:00
2020-10-01 19:09:52 +02:00
Setting a variable as `sensitive` prevents Terraform from showing its value in the `plan` or `apply` output, when that variable is used within a configuration.
Sensitive values are still recorded in the [state ](/docs/state/index.html ), and so will be visible to anyone who is able to access the state data. For more information, see [_Sensitive Data in State_ ](/docs/state/sensitive-data.html ).
A provider can define [an attribute as sensitive ](/docs/extend/best-practices/sensitive-state.html#using-the-sensitive-flag ), which prevents the value of that attribute from being displayed in logs or regular output. The `sensitive` argument on variables allows users to replicate this behavior for values in their configuration, by defining a variable as `sensitive` .
2020-09-30 17:53:53 +02:00
2020-10-01 19:16:15 +02:00
Define a variable as sensitive by setting the `sensitive` argument to `true` :
2020-09-30 17:53:53 +02:00
```
variable "user_information" {
type = object({
name = string
address = string
})
sensitive = true
}
resource "some_resource" "a" {
name = var.user_information.name
address = var.user_information.address
}
```
2020-10-01 19:16:15 +02:00
Using this variable throughout your configuration will obfuscate the value from display in `plan` or `apply` output:
2020-09-30 17:53:53 +02:00
```
Terraform will perform the following actions:
# some_resource.a will be created
+ resource "some_resource" "a" {
+ name = (sensitive)
+ address = (sensitive)
}
Plan: 1 to add, 0 to change, 0 to destroy.
```
2020-10-05 17:34:04 +02:00
In some cases where a sensitive variable is used in a nested block, the whole block can be redacted. This happens with resources which can have multiple blocks of the same type, where the values must be unique. This looks like:
```
# main.tf
resource "some_resource" "a" {
nested_block {
user_information = var.user_information # a sensitive variable
other_information = "not sensitive data"
}
}
# CLI output
Terraform will perform the following actions:
# some_resource.a will be updated in-place
~ resource "some_resource" "a" {
~ nested_block {
# At least one attribute in this block is (or was) sensitive,
# so its contents will not be displayed.
}
}
```
2020-09-30 17:53:53 +02:00
#### Cases where Terraform may disclose a sensitive variable
2020-10-01 19:19:14 +02:00
A `sensitive` variable is a configuration-centered concept, and values are sent to providers without any obfuscation. A provider error could disclose a value if that value is included in the error message. For example, a provider might return the following error even if "foo" is a sensitive value: `"Invalid value 'foo' for field"`
2020-09-30 17:53:53 +02:00
If a resource attribute is used as, or part of, the provider-defined resource id, an `apply` will disclose the value. In the example below, the `prefix` attribute has been set to a sensitive variable, but then that value ("jae") is later disclosed as part of the resource id:
```
# random_pet.animal will be created
+ resource "random_pet" "animal" {
+ id = (known after apply)
+ length = 2
+ prefix = (sensitive)
+ separator = "-"
}
Plan: 1 to add, 0 to change, 0 to destroy.
...
random_pet.animal: Creating...
random_pet.animal: Creation complete after 0s [id=jae-known-mongoose]
```
2020-09-30 17:21:08 +02:00
## Using Input Variable Values
Within the module that declared a variable, its value can be accessed from
2020-11-13 03:30:52 +01:00
within [expressions ](/docs/configuration/expressions/index.html ) as `var.<NAME>` ,
2020-09-30 17:21:08 +02:00
where `<NAME>` matches the label given in the declaration block:
-> **Note:** Input variables are _created_ by a `variable` block, but you
_reference_ them as attributes on an object named `var` .
```hcl
resource "aws_instance" "example" {
instance_type = "t2.micro"
ami = var.image_id
}
```
The value assigned to a variable can only be accessed in expressions within
the module where it was declared.
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
2019-08-07 00:28:03 +02:00
* [In a Terraform Cloud workspace ](/docs/cloud/workspaces/variables.html ).
2018-12-11 01:14:33 +01:00
* Individually, with the `-var` command line option.
* In variable definitions (`.tfvars`) files, either specified on the command line
or automatically loaded.
* As 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
2020-11-13 03:30:52 +01:00
[_Modules_ ](/docs/configuration/blocks/modules/index.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
2019-09-10 16:29:37 +02:00
To specify individual variables on the command line, use the `-var` option
2018-12-11 01:14:33 +01:00
when running 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"
2019-04-01 13:51:17 +02:00
terraform apply -var='image_id_list=["ami-abc123","ami-def456"]'
terraform apply -var='image_id_map={"us-east-1":"ami-abc123","us-east-2":"ami-def456"}'
2016-07-12 00:37:51 +02:00
```
2018-12-11 01:14:33 +01:00
The `-var` option can be used any number of times in a single command.
2020-12-17 00:10:49 +01:00
< a id = "variable-files" > < / a >
2018-12-11 01:14:33 +01:00
### Variable Definitions (`.tfvars`) 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
2018-12-11 01:14:33 +01:00
a _variable definitions file_ (with a filename ending in either `.tfvars`
or `.tfvars.json` ) and then specify that file on the command line with
`-var-file` :
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
```
2019-08-07 00:28:03 +02:00
-> **Note:** This is how Terraform Cloud passes
[workspace variables ](/docs/cloud/workspaces/variables.html ) to Terraform.
2018-12-11 01:14:33 +01: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
2018-12-11 01:14:33 +01:00
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` .
2018-12-11 01:14:33 +01:00
* Any files with names ending in `.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
2020-11-13 03:30:52 +01:00
When variable values are provided in a variable definitions file, you can use
Terraform's usual syntax for
[literal expressions ](/docs/configuration/expressions/types.html#literal-expressions )
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
2018-12-11 01:14:33 +01:00
variables. For convenience, Terraform defaults to interpreting `-var` and
environment variable values as literal strings, which do not need to be 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-12-11 01:14:33 +01:00
However, if a root module variable uses a [type constraint ](#type-constraints )
to require a complex value (list, set, map, object, or tuple), Terraform will
instead attempt to parse its value using the same syntax used within variable
2019-04-30 09:06:01 +02:00
definitions files, which requires careful attention to the string escaping rules
2018-12-11 01:14:33 +01:00
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
2019-05-21 21:55:44 +02:00
recommend always setting complex variable values via variable 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-12-11 01:14:33 +01:00
The above mechanisms for setting variables can be used together in any
combination. If the same variable is assigned multiple values, Terraform uses
2019-08-28 21:34:22 +02:00
the _last_ value it finds, overriding any previous values. Note that the same
variable cannot be assigned multiple values within a single source.
2018-12-11 01:14:33 +01:00
Terraform loads variables in the following order, with later sources taking
precedence over earlier ones:
2017-12-05 20:27:31 +01:00
2018-12-11 01:14:33 +01:00
* Environment variables
2018-05-06 04:37:47 +02:00
* The `terraform.tfvars` file, if present.
* The `terraform.tfvars.json` file, if present.
2018-12-11 01:14:33 +01:00
* Any `*.auto.tfvars` or `*.auto.tfvars.json` files, processed in lexical order
of their filenames.
* Any `-var` and `-var-file` options on the command line, in the order they
2019-08-07 00:28:03 +02:00
are provided. (This includes variables set by a Terraform Cloud
2018-12-11 01:14:33 +01:00
workspace.)
~> **Important:** In Terraform 0.12 and later, variables with map and object
values behave the same way as other variables: the last value found overrides
the previous values. This is a change from previous versions of Terraform, which
would _merge_ map values instead of overriding them.