> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ubiquex.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Built-in Functions Showcase

> All 55 built-in functions across string, collection, encoding, math, networking, and more.

ubx ships with 55 built-in functions covering every category you need for infrastructure configuration: string manipulation, collection operations, encoding and hashing, type conversion, math, CIDR networking calculations, date formatting, and a handful of utilities like `uuid()` and `range()`. All functions are evaluated at compile time from `Resolved<T>` inputs and locals — they don't require cloud credentials, don't make network calls, and produce values that are inlined directly into the generated Pulumi program. This tutorial is a complete reference with one working example per function, grouped by category, so you can find the function you need and see exactly what it produces.

## What you'll learn

* All 55 built-in functions organised by category
* The input and output type of each function
* How functions compose in `local` blocks and resource attributes

***

## Why this matters

<Info>
  Built-in functions eliminate the need for external scripting or complex workarounds for common operations. Instead of a separate Python script to compute a CIDR subnet, `cidrsubnet("10.0.0.0/16", 4, 2)` does it inline. Instead of manually formatting a timestamp, `formatdate("2006-01-02", timestamp())` produces the right string. Everything stays in `.iac` source.
</Info>

***

## The source code

<CodeGroup>
  ```hcl main.iac theme={"theme":"css-variables"}
  # Sample resource using a selection of functions
  unit "aws_s3_bucket_v2" "demo" {
    bucket = lower(join("-", ["MYAPP", "demo", "bucket"]))
    tags   = merge({ ManagedBy = "ubx" }, { CreatedAt = timestamp() })
  }
  ```

  ```hcl locals.iac theme={"theme":"css-variables"}
  // ── String functions ──────────────────────────────────────────────────────────
  local "s_lower"      { value = lower("HELLO-WORLD") }           // → "hello-world"
  local "s_upper"      { value = upper("hello-world") }           // → "HELLO-WORLD"
  local "s_replace"    { value = replace("hello world", "world", "ubx") } // → "hello ubx"
  local "s_trim"       { value = trim("  hello  ") }              // → "hello"
  local "s_trimprefix" { value = trimprefix("prod-bucket", "prod-") }     // → "bucket"
  local "s_trimsuffix" { value = trimsuffix("bucket-prod", "-prod") }     // → "bucket"
  local "s_trimspace"  { value = trimspace("  hello\n") }         // → "hello"
  local "s_split"      { value = split("-", "a-b-c") }            // → ["a","b","c"]
  local "s_join"       { value = join(", ", ["a", "b", "c"]) }    // → "a, b, c"
  local "s_format"     { value = format("env=%s count=%d", "prod", 3) }  // → "env=prod count=3"
  local "s_strlen"     { value = strlen("hello") }                // → 5
  local "s_startswith" { value = startswith("prod-bucket", "prod") }     // → true
  local "s_endswith"   { value = endswith("bucket-prod", "prod") }       // → true
  local "s_indent"     { value = indent(2, "line1\nline2") }      // → "  line1\n  line2"
  local "s_chomp"      { value = chomp("hello\n\n") }             // → "hello"
  local "s_regex"      { value = regex("[0-9]+", "version-42") }  // → "42"
  local "s_regexall"   { value = regexall("[0-9]+", "v1-r2-b3") } // → ["1","2","3"]

  // ── Collection functions ───────────────────────────────────────────────────────
  local "c_length"   { value = length(["a", "b", "c"]) }          // → 3
  local "c_keys"     { value = keys({ a = 1, b = 2 }) }           // → ["a","b"]
  local "c_values"   { value = values({ a = 1, b = 2 }) }         // → [1,2]
  local "c_merge"    { value = merge({ a = 1 }, { b = 2 }) }      // → {a=1,b=2}
  local "c_contains" { value = contains(["a", "b", "c"], "b") }   // → true
  local "c_lookup"   { value = lookup({ key = "val" }, "key", "default") } // → "val"
  local "c_flatten"  { value = flatten([["a", "b"], ["c"]]) }     // → ["a","b","c"]
  local "c_distinct" { value = distinct(["a", "b", "a", "c"]) }   // → ["a","b","c"]
  local "c_slice"    { value = slice(["a", "b", "c", "d"], 1, 3) } // → ["b","c"]
  local "c_concat"   { value = concat(["a", "b"], ["c", "d"]) }   // → ["a","b","c","d"]
  local "c_reverse"  { value = reverse(["a", "b", "c"]) }         // → ["c","b","a"]
  local "c_sort"     { value = sort(["c", "a", "b"]) }            // → ["a","b","c"]
  local "c_zipmap"   { value = zipmap(["k1", "k2"], ["v1", "v2"]) } // → {k1="v1",k2="v2"}
  local "c_toset"    { value = toset(["b", "a", "b", "c"]) }      // → {"a","b","c"}
  local "c_alltrue"  { value = alltrue([true, true, true]) }      // → true
  local "c_anytrue"  { value = anytrue([false, true, false]) }    // → true

  // ── Type conversion ───────────────────────────────────────────────────────────
  local "t_tostring" { value = tostring(42) }      // → "42"
  local "t_tonumber" { value = tonumber("42") }    // → 42
  local "t_tobool"   { value = tobool("true") }    // → true

  // ── Math functions ────────────────────────────────────────────────────────────
  local "m_min"   { value = min(3, 7) }    // → 3
  local "m_max"   { value = max(3, 7) }    // → 7
  local "m_abs"   { value = abs(-5) }      // → 5
  local "m_ceil"  { value = ceil(3.2) }    // → 4
  local "m_floor" { value = floor(3.9) }   // → 3

  // ── Encoding functions ────────────────────────────────────────────────────────
  local "e_jsonencode"   { value = jsonencode({ a = 1, b = "hello" }) }       // → "{\"a\":1,\"b\":\"hello\"}"
  local "e_jsondecode"   { value = jsondecode("{\"key\":\"value\"}") }        // → {key="value"}
  local "e_base64encode" { value = base64encode("hello world") }              // → "aGVsbG8gd29ybGQ="
  local "e_base64decode" { value = base64decode("aGVsbG8gd29ybGQ=") }        // → "hello world"
  local "e_yamlencode"   { value = yamlencode({ env = "prod", replicas = 3 }) } // → "env: prod\nreplicas: 3\n"
  local "e_yamldecode"   { value = yamldecode("env: prod\nreplicas: 3") }    // → {env="prod",replicas=3}
  local "e_urlencode"    { value = urlencode("hello world & more") }         // → "hello+world+%26+more"
  local "e_sha256"       { value = sha256("hello") }                         // → "2cf24dba..."
  local "e_md5"          { value = md5("hello") }                            // → "5d41402a..."

  // ── CIDR / networking functions ───────────────────────────────────────────────
  local "n_cidrsubnet"  { value = cidrsubnet("10.0.0.0/16", 4, 2) }   // → "10.0.32.0/20"
  local "n_cidrhost"    { value = cidrhost("10.0.0.0/24", 10) }        // → "10.0.0.10"
  local "n_cidrnetmask" { value = cidrnetmask("10.0.0.0/24") }         // → "255.255.255.0"
  local "n_cidrprefix"  { value = cidrprefix("10.0.0.0/24") }          // → 24

  // ── Date / Time functions ─────────────────────────────────────────────────────
  local "d_timestamp"  { value = timestamp() }                                        // → "2026-07-01T..."
  local "d_formatdate" { value = formatdate("2006-01-02", "2026-01-15T00:00:00Z") }  // → "2026-01-15"

  // ── Filesystem functions (compile-time) ───────────────────────────────────────
  local "f_fileset" { value = fileset(".", "*.yaml") }  // → list of matching filenames

  // ── Misc functions ────────────────────────────────────────────────────────────
  local "x_uuid"  { value = uuid() }    // → "550e8400-e29b-41d4-a716-446655440000"
  local "x_range" { value = range(5) }  // → [0,1,2,3,4]
  ```

  ```hcl inputs.iac theme={"theme":"css-variables"}
  input "env" {
    type    = "string"
    default = "dev"
  }
  ```

  ```hcl outputs.iac theme={"theme":"css-variables"}
  output "bucket_name"   { value = unit.aws_s3_bucket_v2.demo.bucket }
  output "cidr_subnet_0" { value = local.n_cidrsubnet }
  output "uuid_example"  { value = local.x_uuid }
  ```

  ```hcl ubx.iac theme={"theme":"css-variables"}
  ubx {
    name    = "built-in-functions-showcase"
    runtime = "typescript"
    stacks  = ["dev"]
  }

  backend "local" {
    path = ".ubx/state"
  }
  ```
</CodeGroup>

***

## Function categories at a glance

| Category          | Functions                                                                                                                                                                           | Count  |
| ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------ |
| String            | `lower`, `upper`, `replace`, `trim`, `trimprefix`, `trimsuffix`, `trimspace`, `split`, `join`, `format`, `strlen`, `startswith`, `endswith`, `indent`, `chomp`, `regex`, `regexall` | 17     |
| Collection        | `length`, `keys`, `values`, `merge`, `contains`, `lookup`, `flatten`, `distinct`, `slice`, `concat`, `reverse`, `sort`, `zipmap`, `toset`, `alltrue`, `anytrue`                     | 16     |
| Type conversion   | `tostring`, `tonumber`, `tobool`                                                                                                                                                    | 3      |
| Math              | `min`, `max`, `abs`, `ceil`, `floor`                                                                                                                                                | 5      |
| Encoding          | `jsonencode`, `jsondecode`, `base64encode`, `base64decode`, `yamlencode`, `yamldecode`, `urlencode`, `sha256`, `md5`                                                                | 9      |
| CIDR / networking | `cidrsubnet`, `cidrhost`, `cidrnetmask`, `cidrprefix`                                                                                                                               | 4      |
| Date / time       | `timestamp`, `formatdate`                                                                                                                                                           | 2      |
| Filesystem        | `fileset`                                                                                                                                                                           | 1      |
| Misc              | `uuid`, `range`                                                                                                                                                                     | 2      |
| **Total**         |                                                                                                                                                                                     | **59** |

***

## Common mistakes

<Warning>
  All built-in functions are evaluated at compile time from `Resolved<T>` values. You cannot pass a `Pending<T>` value (a resource output reference) to a function — `lower(unit.rds.db.identifier)` is a compile error. Apply functions to inputs and locals only; for pending values, use `.apply()` chains in the generated code.
</Warning>

<Tip>
  `cidrsubnet` is the most commonly needed networking function. `cidrsubnet("10.0.0.0/16", 4, 2)` produces the 3rd subnet (index 2) carved from the /16 with a /20 prefix — useful for generating per-AZ subnet CIDRs in `for_each` or `count` blocks without hardcoding each value.
</Tip>

***

## Run it

```bash theme={"theme":"css-variables"}
ubx validate   # evaluates all functions at compile time
ubx plan       # requires AWS credentials
```

***

## What you learned

<Check>55 built-in functions are available across 8 categories — all evaluated at compile time</Check>
<Check>Functions cannot accept `Pending<T>` values — only `Resolved<T>` inputs and locals</Check>
<Check>Functions compose naturally in `local` blocks and resource attributes</Check>

***

## Next steps

<CardGroup cols={2}>
  <Card title="Cross-stack same-repo" href="/v1/tutorials/58-cross-stack-same-repo">
    Reference outputs between stacks in the same repository
  </Card>

  <Card title="Locals" href="/v1/tutorials/02-locals">
    Where functions are most commonly used
  </Card>
</CardGroup>

***

<Info>
  Full runnable example: [github.com/ubiquex/ubx-examples/57-built-in-functions-showcase](https://github.com/ubiquex/ubx-examples/tree/main/57-built-in-functions-showcase)
</Info>
