--- layout: "docs" page_title: "Configuring Variables" sidebar_current: "docs-config-variables" 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. --- # 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: ```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 configuration. Multiple variables blocks can be used to add multiple variables. The `name` given to the variable block is the name used to 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: - `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`. - `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. - `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. -> **Note**: Default values can be strings, lists, or maps. If a default is specified, it must match the declared type of the variable. ### 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 variable is defined in more than one variable file, the last value specified is effective. ### Variable Merging When variables are conflicting, map values are merged and all other values are overridden. Map values are always merged. For example, if you set 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 value 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. Definitions passed using the `-var-file` flag will always be evaluated after those in the working directory.