Skip to main content

Built-In Functions

Imagine you’re running your Lego city with Terraform. Sometimes you need to rename streets, count houses, or group neighborhoods. Instead of doing this manually, Terraform gives you built‑in functions which are ready‑made helpers that transform strings, numbers, lists, and maps. These functions are the everyday tools that make your configurations dynamic, flexible, and less repetitive.


Key Concepts

1. String Functions

join() → Combine list into a string.hcl

output "subnets" {
  value = join(", ", ["10.0.1.0/24", "10.0.2.0/24"])
}
``` → `10.0.1.0/24, 10.0.2.0/24`  

replace() → Replace substrings.

output "new_name" {
  value = replace("dev-server", "dev", "prod")
}
``` → `prod-server`  

upper() / lower() → Change case of strings.

output "env_upper" {
  value = upper("dev")
}
``` → `DEV`  

2. Numeric Functions

  • ceil() / floor() → Round numbers up or down.

abs() → Absolute value.

output "abs_val" {
  value = abs(-10)
}
``` → `10`  

max() / min() → Find largest or smallest number.

output "max_size" {
  value = max(2, 4, 6)
}
``` → `6`  

3. Collection Functions

concat() → Merge lists.

output "all_subnets" {
  value = concat(var.public_subnets, var.private_subnets)
}

lookup() → Get value from a map with a default.hcl

output "region" {
  value = lookup(var.settings, "region", "us-east-1")
}

element() → Pick item by index.

output "first_subnet" {
  value = element(var.subnets, 0)
}

length() → Count elements.

output "count_subnets" {
  value = length(var.subnets)
}

4. Conditional Functions

coalesce() → Return first non‑null value.

output "db_url" {
  value = coalesce(var.db_url, "default-db.local")
}

Ternary operator (? :) → Inline condition.

output "env_type" {
  value = var.is_prod ? "production" : "development"
}

Hands‑On Lab / Demo

Lab: Combining Functions for Dynamic Outputs

  1. Run terraform apply → Output:
    DEV has 2 subnets: 10.0.1.0/24, 10.0.2.0/24

Use functions:

output "summary" {
  value = upper(var.env) + " has " + tostring(length(var.subnets)) + " subnets: " + join(", ", var.subnets)
}

Define variables:

variable "subnets" {
  default = ["10.0.1.0/24", "10.0.2.0/24"]
}
variable "env" {
  default = "dev"
}

Pro Tips & Best Practices

  • Use functions to avoid hardcoding values.
  • Combine functions for powerful transformations.
  • Keep expressions readable - avoid overly complex one‑liners.
  • Test functions with terraform console before applying.
  • Document function usage in module README.

Summary & Cheatsheet

  • String functions: upper(), lower(), replace(), join().
  • Numeric functions: max(), min(), abs(), ceil(), floor().
  • Collection functions: length(), element(), lookup(), concat().
  • Conditional functions: ternary (? :), coalesce().
Quick mnemonic: Strings, Numbers, Collections, Conditions = SNCC

The Hackers Notebook

Common built‑in functions are the bread and butter of Terraform scripting. They let you manipulate strings, numbers, lists, and maps with ease, making your configurations flexible and reusable. By mastering these, you unlock the ability to write cleaner, smarter Terraform code.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025