Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
π 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:
- String
- Number
- Bool (Boolean)
- List
- Map
- Object
- 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
- Flexibility β Write dynamic and reusable Terraform code.
- Scalability β Easily manage complex environments.
- Maintainability β Organize configurations cleanly.
- Collaboration β Teams can work together effectively.
- 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.β