πŸ“˜ Terraform Variable Types Explained: Strings, Numbers, Bools, Lists, Maps, Objects, Tuples

When working with Terraform (an Infrastructure as Code tool), we often need to define variables. These variables make our Terraform configurations flexible, reusable, and dynamic.

But Terraform doesn’t just support one kind of variable β€” it has multiple types of variables. Each type has its own use case.

πŸ‘‰ Terraform Variable Types:

  1. String
  2. Number
  3. Bool (Boolean)
  4. List
  5. Map
  6. Object
  7. Tuple

By mastering these, you’ll write Terraform code that is scalable, maintainable, and reusable across environments.


πŸ”‘ 1. String Variables

A string is simply a sequence of characters (text).

Definition Example:

variable "region" {
type = string
default = "us-east-1"
}

Usage:

provider "aws" {
region = var.region
}

βœ… Example Programs

Example 1: String for AWS Region

variable "region" {
type = string
default = "us-east-1"
}
provider "aws" {
region = var.region
}

Example 2: String for Instance Type

variable "instance_type" {
type = string
default = "t2.micro"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}

Example 3: String for Environment Name

variable "environment" {
type = string
default = "dev"
}
output "env_name" {
value = "Environment: ${var.environment}"
}

πŸ”‘ 2. Number Variables

Numbers are used for integer or float values.

Definition Example:

variable "instance_count" {
type = number
default = 2
}

Usage:

resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}

βœ… Example Programs

Example 1: Number for Instance Count

variable "instance_count" {
type = number
default = 2
}
resource "aws_instance" "web" {
count = var.instance_count
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}

Example 2: Number for Storage Size

variable "storage_size" {
type = number
default = 20
}
resource "aws_ebs_volume" "volume" {
availability_zone = "us-east-1a"
size = var.storage_size
}

Example 3: Number for Auto Scaling Minimum

variable "min_instances" {
type = number
default = 1
}
output "autoscaling_min" {
value = "Minimum Instances: ${var.min_instances}"
}

πŸ”‘ 3. Boolean Variables (bool)

Boolean variables store true/false values.

Definition Example:

variable "enable_monitoring" {
type = bool
default = true
}

βœ… Example Programs

Example 1: Enable Monitoring

variable "enable_monitoring" {
type = bool
default = true
}
resource "aws_instance" "server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
monitoring = var.enable_monitoring
}

Example 2: Conditional Resource

variable "create_bucket" {
type = bool
default = true
}
resource "aws_s3_bucket" "example" {
count = var.create_bucket ? 1 : 0
bucket = "my-test-bucket"
}

Example 3: Feature Toggle

variable "enable_logs" {
type = bool
default = false
}
output "logging_status" {
value = var.enable_logs ? "Logging Enabled" : "Logging Disabled"
}

πŸ”‘ 4. List Variables

Lists store multiple values in order.

Definition Example:

variable "regions" {
type = list(string)
default = ["us-east-1", "us-west-2"]
}

βœ… Example Programs

Example 1: Multiple Regions

variable "regions" {
type = list(string)
default = ["us-east-1", "us-west-2"]
}
output "first_region" {
value = var.regions[0]
}

Example 2: List of Security Groups

variable "security_groups" {
type = list(string)
default = ["sg-12345", "sg-67890"]
}
resource "aws_instance" "server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
vpc_security_group_ids = var.security_groups
}

Example 3: Looping with List

variable "users" {
type = list(string)
default = ["alice", "bob", "charlie"]
}
output "user_list" {
value = [for user in var.users : upper(user)]
}

πŸ”‘ 5. Map Variables

Maps store key-value pairs.

Definition Example:

variable "instance_types" {
type = map(string)
default = {
dev = "t2.micro"
prod = "t3.large"
}
}

βœ… Example Programs

Example 1: Environment-Specific Instance Type

variable "instance_types" {
type = map(string)
default = {
dev = "t2.micro"
prod = "t3.large"
}
}
resource "aws_instance" "example" {
instance_type = var.instance_types["prod"]
ami = "ami-0c55b159cbfafe1f0"
}

Example 2: Tags Using Map

variable "tags" {
type = map(string)
default = {
Environment = "dev"
Owner = "team1"
}
}
resource "aws_s3_bucket" "bucket" {
bucket = "my-dev-bucket"
tags = var.tags
}

Example 3: AMI IDs by Region

variable "amis" {
type = map(string)
default = {
us-east-1 = "ami-0c55b159cbfafe1f0"
us-west-2 = "ami-0a887e401f7654935"
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = var.amis["us-east-1"]
instance_type = "t2.micro"
}

πŸ”‘ 6. Object Variables

Objects allow you to group related values.

Definition Example:

variable "app_config" {
type = object({
name = string
version = string
replicas = number
})
default = {
name = "myapp"
version = "1.0"
replicas = 2
}
}

βœ… Example Programs

Example 1: Application Config

output "app_name" {
value = var.app_config.name
}

Example 2: Database Config

variable "db_config" {
type = object({
engine = string
version = string
storage = number
})
default = {
engine = "mysql"
version = "8.0"
storage = 20
}
}
output "db_engine" {
value = var.db_config.engine
}

Example 3: Network Config

variable "network" {
type = object({
vpc_id = string
subnets = list(string)
})
default = {
vpc_id = "vpc-123456"
subnets = ["subnet-1", "subnet-2"]
}
}
output "subnets" {
value = var.network.subnets
}

πŸ”‘ 7. Tuple Variables

Tuples hold values of different types in a fixed order.

Definition Example:

variable "example_tuple" {
type = tuple([string, number, bool])
default = ["server1", 2, true]
}

βœ… Example Programs

Example 1: Simple Tuple

output "tuple_example" {
value = var.example_tuple
}

Example 2: Accessing Tuple Values

output "first_value" {
value = var.example_tuple[0]
}

Example 3: Tuple with Mixed Data

variable "mixed_tuple" {
type = tuple([string, number, bool])
default = ["db-server", 50, false]
}
output "server_info" {
value = "${var.mixed_tuple[0]} has ${var.mixed_tuple[1]}GB storage"
}

🧠 How to Remember Terraform Variable Types (Exam & Interview)

Trick: SNBLMOT (Say: β€œSnowBall-MOT”)

  • S β†’ String
  • N β†’ Number
  • B β†’ Bool
  • L β†’ List
  • M β†’ Map
  • O β†’ Object
  • T β†’ Tuple

🎯 Why It’s Important to Learn

  1. Flexibility – Write dynamic and reusable Terraform code.
  2. Scalability – Easily manage complex environments.
  3. Maintainability – Organize configurations cleanly.
  4. Collaboration – Teams can work together effectively.
  5. Best Practice – Industry standard for Infrastructure as Code.

πŸ† Conclusion

Terraform variable types are the foundation of reusable IaC code.

  • Use strings, numbers, bools for simple values.
  • Use lists, maps for collections.
  • Use objects and tuples for structured, complex configurations.

πŸ‘‰ In interviews, always mention: β€œTerraform variable types make code reusable, flexible, and environment-agnostic, enabling real-world automation at scale.”