DEV Community

ankitgadling
ankitgadling

Posted on

Day 7 – Type Constraints in Terraform

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"
}
Enter fullscreen mode Exit fullscreen mode

Number

Numeric values integers as well as floats.
Example:

variable "age" {
  type = number
  default = 25
}
Enter fullscreen mode Exit fullscreen mode

Bool

Boolean value (true/false).
Example:

variable "is_enabled" {
  type = bool
  default = true
}
Enter fullscreen mode Exit fullscreen mode

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)
}
Enter fullscreen mode Exit fullscreen mode

Set

Its a collection of unique values, also of the same type, but order does not matter.

variable "unique_tags" {
  type = set(string)
}
Enter fullscreen mode Exit fullscreen mode

Tuple

Its an ordered collection of values of different types.

variable "my_tuple" {
  type = tuple([string, number, bool])
}
Enter fullscreen mode Exit fullscreen mode

Map

Its a set of string keys with values of the same type.

variable "region_mapping" {
  type = map(string)
}
Enter fullscreen mode Exit fullscreen mode

Object

Its a collection of attributes with different types, similar to a struct.

variable "server_config" {
  type = object({
    name = string
    cpu  = number
    prod = bool
  })
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

Null

It represents the absence of a value and is often used to trigger default behavior.

Example:

variable "instance_type" {
  default = null
}
Enter fullscreen mode Exit fullscreen mode

Terraform will use the default from the provider/resource when null is passed.


Type Constraints in Terraform


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"
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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"
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

for more information checkout
https://github.com/ankitgadling/terraformwithaws/tree/main/day_7

Top comments (0)