Today, I learned about Terraform type constraints and why they are essential for writing safe, predictable, and maintainable infrastructure code.
Terraform variables are not just about passing values. With type constraints, variables become contracts that define what kind of data is allowed.
Topics Covered
-
Primitive types:
string,number,bool -
Collection types:
list,set,map -
Structural types:
tuple,object - Type validation and constraints
- Defining complex variable structures
Why Type Constraints Matter
Without type constraints, Terraform treats variables as loosely typed. This can lead to:
- Runtime errors during
terraform apply - Unexpected values passed to resources
Hard-to-debug infrastructure issues
Using type constraints gives you:Early validation at
terraform planSelf-documenting variables
Safer and predictable infrastructure code
Primitive Type
Number
Supports both integers and floating-point values.
example
variable "instance_count" {
type = number
default = 1
}
Specifying the type as number restricts the variable to numeric values, matching Terraform’s expectations for count
String
A string is used for text-based values such as names, regions, or identifiers.
Example
variable "region" {
type = string
default = "us-east-1"
}
String values must be enclosed in double quotes and can contain spaces.
Boolean
A boolean represents true or false. It is commonly used for feature toggles.
example
variable "monitoring_enabled" {
type = bool
default = true
}
Complex Types in Terraform
List
A list is an ordered collection of values of the same type
variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b"]
}
Set
A set is similar to a list, but:
- Values must be unique
- Order is not guaranteed
Commonly used for Security-groups;
variable "allowed_ports" {
type = set(number)
default = [22, 80, 443]
}
Map
A key-value structure frequently used for tagging AWS resources.
variable "tag" {
type = map(string)
default = {
Environment = "Dev"
Name = "Dev-EC2-instance"
}
Structural Types(Advanced)
Object
- Object is a collection of named attributes with different data types, defined with keys.
- Example object variable config with three attributes:
variable "config" {
type = object({
region = string
monitoring = bool
instance_count = number
})
default = {
region = "us-east-1"
monitoring = true
instance_count = 1
}
}
Tuple
- Tuple enables grouping multiple values with different data types in a fixed sequence.
- Example tuple variable with three elements (number, string, number):
variable "ingress_values" {
type = tuple([number, string, number])
default = [443, "TCP", 443]
}
Best practice I learnt when using type contraints;
- Use primitives for simple values.
- Use complex types to group multiple related values, especially when types vary.
- Understand indexing rules: lists and tuples are indexed; sets are not.
- Use objects for structured data with named fields of different types.
- Use maps for homogeneous key-value pairs.
Top comments (0)