Variables
A variable is an input to a Terraform configuration or a Terraform module.
Variables are conventionally declared in a file named variables.tf1. Declare variables using variable blocks:
| |
A variable has a type to indicate what type of value Terraform can expect for this variable. There are three basic types:
stringbool(trueorfalse)number(used for both integers and decimal numbers, e.g.1,-1242, and3.1415)
There are also four composite types:
object()map()list()tuple()
If you need to pass sensitive data (e.g. passwords) into a Terraform configuration or module you should specify that the variable is sensitive:
| |
If you expect a value for a variable to fulfill certain conditions you can add one or more validation blocks to the variable:
| |
If the value passed to the region variable is not one of us or eu, Terraform will not proceed and will instead print the error_message.
You reference the value of a variable using the var.<name> syntax. An example of what this could look like:
| |
Best practices
- Add the
defaultargument to variables if a sensible default value exist. - Declare sensitive values using the
sensitive = trueargument. - Add the
descriptionargument with a descriptive value to tell users what this variable is.
This is not a requirement. You can place variables in any Terraform file inside of your Terraform configuration. However, you should follow the convention of using
variables.tfto not surprise and confuse other users of the Terraform configuration. ↩︎