OpenTofu provides a rich set of built-in functions, operators, and conditional expressions that enhance your infrastructure-as-code (IaC) workflows. This guide will show you how to leverage the interactive console and explore numeric, string, list, and map functions with practical examples.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

Launching the OpenTofu Console
To experiment with functions and interpolations in a live environment, start the interactive console:The OpenTofu console evaluates HCL expressions in real time. You can inspect variables, resources, and file contents without applying changes.
Exploring Functions in the Console
Given this configuration snippet:file(path)reads the file’s content.length(list)returns the number of elements (here,3).toset(list)converts a list to a set, removing duplicates.
Numeric Functions
Numeric functions allow you to compare values and perform rounding:| Function | Description | Example |
|---|---|---|
| max(…) | Returns the largest number | max(1,5,3) → 5 |
| min(…) | Returns the smallest number | min(1,5,3) → 1 |
| ceil(n) | Rounds up to the nearest integer | ceil(10.1) → 11 |
| floor(n) | Rounds down to the nearest integer | floor(10.9) → 10 |
Use the expansion operator
... to unpack a collection into individual arguments.String Functions
Manipulate and transform text with string functions:| Function | Description |
|---|---|
| split(sep, string) | Splits a string into a list of substrings |
| join(sep, list) | Joins a list into a single string |
| lower(string) | Converts all characters to lowercase |
| upper(string) | Converts all characters to uppercase |
| title(string) | Capitalizes the first letter of each word |
| substr(string, o, l) | Extracts a substring starting at offset o, length l |
Collection Functions
List Operations
| Function | Description |
|---|---|
| length(list) | Returns the number of items |
| index(list, value) | Finds the position of value |
| element(list, index) | Retrieves the element at index |
| contains(list, value) | Checks if value exists (case-sensitive) |
Map Operations
| Function | Description |
|---|---|
| keys(map) | Returns a list of all keys |
| values(map) | Returns a list of all values |
| lookup(map, key, default) | Retrieves map[key] or default if the key is absent |
Calling
lookup without a default value will error if the key does not exist.