On day 5 we have seen the variables in action. Today we will be doing a deep dive into type constraints in Terraform. A Terraform variable has certain types: primitive, complex, and special. We will start with primitive first.
1. Primitive Types
The word itself explains this type. Primitive means the basic ones, which include string, number, and bool.
String
It can have any text value.
Example:
variable "name" {
type = string
default = "Terraform"
}
Number
Numeric values integers as well as floats.
Example:
variable "age" {
type = number
default = 25
}
Bool
Boolean value (true/false).
Example:
variable "is_enabled" {
type = bool
default = true
}
2. Complex Types
Complex types are types that allow you to group multiple values.
List
Its an ordered collection of values of the same type.
variable "instance_ids" {
type = list(string)
}
Set
Its a collection of unique values, also of the same type, but order does not matter.
variable "unique_tags" {
type = set(string)
}
Tuple
Its an ordered collection of values of different types.
variable "my_tuple" {
type = tuple([string, number, bool])
}
Map
Its a set of string keys with values of the same type.
variable "region_mapping" {
type = map(string)
}
Object
Its a collection of attributes with different types, similar to a struct.
variable "server_config" {
type = object({
name = string
cpu = number
prod = bool
})
}
3. Special Types
Any
Allows any data type Terraform will accept whatever is provided.
Useful for flexible modules. We should avoid using it.
variable "input" {
type = any
}
Null
It represents the absence of a value and is often used to trigger default behavior.
Example:
variable "instance_type" {
default = null
}
Terraform will use the default from the provider/resource when null is passed.
How These Types Help in Real Terraform Projects
Strings for Naming Resources
variable "environment" {
type = string
default = "dev"
}
resource "aws_s3_bucket" "example" {
bucket = "${var.environment}-app-data"
}
Change environment once → everything updates.
Numbers for Scaling Resources
variable "instance_count" {
type = number
default = 3
}
resource "aws_instance" "server" {
count = var.instance_count
ami = "ami-123"
instance_type = "t2.micro"
}
Increase the number → Terraform creates more servers.
Booleans for Feature Toggles
variable "monitoring_enabled" {
type = bool
default = true
}
resource "aws_instance" "server" {
monitoring = var.monitoring_enabled
}
Turn monitoring on/off with a single variable.
Lists for Repeating Resources
variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b"]
}
resource "aws_subnet" "subnet" {
count = length(var.availability_zones)
az = var.availability_zones[count.index]
cidr_block = "10.0.${count.index}.0/24"
}
Subnets automatically created across multiple AZs.
Maps for Centralized Tagging
variable "tags" {
type = map(string)
default = {
Environment = "dev"
Owner = "teamA"
}
}
resource "aws_vpc" "main" {
tags = var.tags
}
Manage all tags from one place.
Objects for Clean, Structured Config
variable "config" {
type = object({
region = string
monitoring = bool
instance_count = number
})
default = {
region = "us-east-1"
monitoring = true
instance_count = 2
}
}
provider "aws" {
region = var.config.region
}
for more information checkout
https://github.com/ankitgadling/terraformwithaws/tree/main/day_7

Top comments (0)