--- layout: "docs" page_title: "Configuring Input Variables" sidebar_current: "docs-config-variables" description: |- Input variables are parameters for Terraform modules. This page covers configuration syntax for variables. --- # Input Variable Configuration 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). This page assumes you're familiar with the [configuration syntax](/docs/configuration/syntax.html) already. ## Example Input variables can be defined as follows: ```hcl variable "key" { type = "string" } variable "images" { type = "map" default = { us-east-1 = "image-1234" us-west-2 = "image-4567" } } variable "zones" { default = ["us-east-1a", "us-east-1b"] } ``` ## Description The `variable` block configures a single input variable for a Terraform module. Each block declares a single variable. 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. Within the block body (between `{ }`) is configuration for the variable, which accepts the following arguments: - `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 `default`. If no `default` is provided, the type is assumed to be `string`. - `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. - `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. 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. 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. ### Strings String values are simple and represent a basic key to value mapping where the key is the variable name. An example is: ```hcl variable "key" { type = "string" default = "value" } ``` A multi-line string value can be provided using heredoc syntax. ```hcl variable "long_key" { type = "string" default = < **Note**: Variable files are evaluated in the order in which they are specified on the command line. If a particular variable is defined in more than one variable file, the last value specified is effective. ### Variable Merging When multiple values are provided for the same input variable, map values are merged while all other values are overriden by the last definition. For example, if you define a variable twice on the command line: ```shell $ terraform apply -var foo=bar -var foo=baz ``` Then the value of `foo` will be `baz`, since it was the last definition seen. However, for maps, the values are merged: ```shell $ terraform apply -var 'foo={quux="bar"}' -var 'foo={bar="baz"}' ``` The resulting value of `foo` will be: ```shell { quux = "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 Both these files have the variable `baz` defined: _foo.tfvars_ ```hcl baz = "foo" ``` _bar.tfvars_ ```hcl baz = "bar" ``` When they are passed in the following order: ```shell $ terraform apply -var-file=foo.tfvars -var-file=bar.tfvars ``` The result will be that `baz` will contain the value `bar` because `bar.tfvars` has the last definition loaded. Definition files passed using the `-var-file` flag will always be evaluated after those in the working directory.