Terraform Built-in Functions

Search Terraform built-in functions by name, category and signature.

Interactive Terraform function reference covering string, collection, encoding, filesystem, numeric, network and crypto functions — each with its signature, a clear description and a worked example. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

How do I look up a value in a map with a fallback in Terraform?

Use lookup(map, key, default), which returns the value at the key or the default if the key is missing. For chaining several possible values, coalesce(a, b, c) returns the first non-null, non-empty argument.

Every Terraform function, searchable

Terraform’s configuration language (HCL) ships with a large library of built-in functions for transforming strings, collections, numbers, files, IP ranges and more. This reference lists each function with its signature, a description and a concrete example. Filter by category or search by name.

How it works

Terraform functions are pure: given the same inputs they always return the same output, and they run during plan/apply to compute values for locals, resource arguments and outputs. There are no user-defined functions — you compose the built-ins.

Functions are grouped by purpose. String functions (format, replace, substr) manipulate text. Collection functions (merge, flatten, lookup, distinct) reshape lists, sets and maps. Encoding functions (jsonencode, base64encode, yamlencode) serialise values for policies and APIs. Network functions (cidrsubnet, cidrhost) do subnet math. Filesystem functions (file, templatefile) read content from disk. The category filter in this tool mirrors these groups.

Read each entry as name(arguments) with the example showing a representative input and the resulting value. Combine functions freely, for example join(",", distinct(flatten(var.lists))).

Function categories and their most-used members

String functions

format(spec, args...) — behaves like printf; useful for building resource names from variables. For example format("app-%s-%s", var.env, var.region) returns "app-prod-eu-west-1".

replace(string, search, replace) — swaps substrings. replace("hello world", " ", "-") returns "hello-world".

trimspace, upper, lower, title — basic text normalisation, often used to standardise variable inputs before they become resource names.

Collection functions

merge(map1, map2, ...) — combines maps, with later maps overriding earlier keys. Common pattern for merging a default tag map with environment-specific tags.

flatten(list_of_lists) — turns nested lists into a single list. Pair with distinct to deduplicate.

toset(list) — converts a list to a set, removing duplicates, ready for for_each.

length(collection) — counts items in any list, set, or map.

Encoding functions

jsonencode(value) — converts any Terraform value to its JSON representation. Invaluable for writing IAM policy documents inline without a separate file.

base64encode(string) — required for EC2 user_data scripts and some API payloads.

urlencode(string) — escapes a string for safe inclusion in a URL.

Network functions

cidrsubnet(prefix, newbits, netnum) — carves subnets from a CIDR block. cidrsubnet("10.0.0.0/16", 8, 3) returns "10.0.3.0/24". Use with count.index to generate subnets across availability zones.

cidrhost(prefix, hostnum) — gives the IP of a specific host within a range. Useful for assigning static IPs inside a subnet.

Tips and examples

  • Provide safe defaults instead of failing a plan:
locals {
  region = coalesce(var.region, "eu-west-1")
  tag    = lookup(var.tags, "env", "dev")
}
  • Build subnets programmatically across availability zones:
cidrsubnet("10.0.0.0/16", 8, count.index)
  • Use try(...) to gracefully handle expressions that might error, and jsonencode to emit IAM or other JSON policy documents from native HCL objects.
  • Test any expression quickly in terraform console before wiring it into your configuration. The console accepts any valid HCL expression and prints the result immediately — far faster than a full plan cycle.