As your modules grow in complexity, so does the need for strong type safety. Type constraints help you catch errors early, enforce structure, and write more predictable, maintainable Terraform code.
Why Type Constraints Matter
- Fewer runtime errors
- Easier debugging
- Cleaner module interfaces
- More reliable, reusable infrastructure code
Primitive (Basic Types)
String
- Used for plain text values.
variable "env" {
type = string
}
Number
- Represents integers or floating-point numbers.
variable "instance_count" {
type = number
}
Bool
Boolean values: true or false.
variable "enabled" {
type = bool
}
Complex (Collection Types)
list(type)
An ordered collection of elements of the same type.
variable "allowed_regions" {
type = list(string)
}
set(type)
An unordered, unique collection.
variable "security_groups" {
type = set(string)
}
map(type)
An unordered, unique collection.
variable "tags" {
type = map(string)
}
tuple([type1, type2, ...])
An ordered list where each element has a different type.
variable "config_tuple" {
type = tuple([string, number, bool])
}
object({ key = type })
A structured data object with named attributes, same as JSON
variable "network_config" {
type = object({
cidr_block = string
subnets = list(string)
public = bool
})
}
Top comments (0)