Search⌘ K

Type Constraints - Simple Types

Understand how to apply simple type constraints to Terraform variables, including string, number, and bool types. Learn how these constraints help ensure variable values are valid, improve code readability, and guide correct usage within your Terraform projects.

So far, we have been setting variables and not specified the type. This means that whatever type you pass to a variable will be the type that the variable assumes. A type constraint allows you to be specific about the type of a variable. There are three simple types:

  • string
  • number
  • bool

Project example

To specify a type constraint, use the type property when defining a variable. Let’s see an example of using the three simple types:

C++
variable "a" {
type = string
default = "foo"
}
variable "b" {
type = bool
default = true
}
variable "c" {
type = number
default = 123
}
output "a" {
value = var.a
}
output "b" {
value = var.b
}
output "c" {
value = var.c
}

In this project, we are defining three variables a, b, and c. We are using the type parameter to define:

  • a as a string,
  • b as a bool, and
  • c as a number.

We are using defaults to set ...