🎯 Goal: Move from expressions β†’ functions β€” reusable, built-in logic for safe, DRY configs.

βœ… You’ll master:


🧠 Why Functions Matter

❌ Without functions:

βœ… With functions:

β†’ Infrastructure that self-corrects invalid inputs

β†’ Truly DRY configs (no repeated logic)

β†’ Environment-aware resources (dev/staging/prod)

πŸ’‘ Key Insight:

Terraform has 100+ built-in functions β€” but no custom functions.

β†’ Use them like LEGO blocks β€” small, composable, powerful.


πŸ“¦ Function Cheat Sheet (Part 1)

Category Key Functions Use Case
String lower(), replace(), substr(), trim() Sanitize names, tags, IDs
Numeric max(), min(), abs() Cost tracking, capacity planning
Collection length(), concat(), merge() Combine lists/maps
Type Conversion toset(), tonumber(), tostring() Normalize inputs, dedupe
Lookup lookup(map, key, default) Env-specific configs

🎯 Golden Rule:

β€œChain functions like shell pipes: lower(replace(...)) β†’ clean, readable, safe.”


✏️ Hands-On: Functions in Action

πŸ”Ή Prerequisites (variables.tf)

variable "project_name" {
  type    = string
  default = "Project Alpha Resource!!"
}

variable "bucket_name_raw" {
  type    = string
  default = "Project Alpha Storage!! 123"
}

variable "allowed_ports_str" {
  type    = string
  default = "80,443,8080,3306"
}

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

variable "default_tags" {
  type = map(string)
  default = {
    ManagedBy = "Terraform"
    Company   = "TechCorp"
  }
}

variable "env_tags" {
  type = map(string)
  default = {
    Environment = "dev"
    CostCenter  = "engineering"
  }
}


1️⃣ String Functions β€” S3 Bucket Name Sanitization