Variable Types: Primitives
What You'll Learn
You'll discover how Terraform's three primitive data types—string, number, and bool—let you define simple, single-value variables for your infrastructure configurations.
Understanding Primitive Types
In Terraform, primitive types are the simplest building blocks for variables—they hold a single, atomic value rather than a collection. Think of them like basic ingredients: flour, sugar, and salt. Each serves one clear purpose and can't be broken down further in your recipe.
The Three Primitives
String: Text values wrapped in quotes. Use strings for names, descriptions, regions, or any human-readable text.
variable "region" {
type = string
default = "us-east-1"
}
Number: Numeric values without quotes. Perfect for counts, sizes, port numbers, or any quantity.
variable "instance_count" {
type = number
default = 3
}
Bool: Boolean values representing true/false. Ideal for feature flags or enable/disable switches.
variable "enable_monitoring" {
type = bool
default = true
}
Why Specify Types?
Declaring type in your variable blocks provides validation—Terraform will reject invalid values before execution. If someone tries to pass "five" to your instance_count number variable, Terraform catches the error immediately.
When you don't specify a type, Terraform infers it from the default value, but explicit types make your code clearer and safer.
Key Takeaway: Terraform's primitive types—string, number, and bool—are single-value building blocks that make your variables type-safe and self-documenting, preventing configuration errors before infrastructure is provisioned.