Skip to main content
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.
The image contains the text "Functions, Operators, and Conditional Expressions" with a logo on the left. It also includes a copyright notice for KodeKloud.

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:
Try these commands in the console:
  • 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:
Use the expansion operator ... to unpack a collection into individual arguments.

String Functions

Manipulate and transform text with string functions:

Collection Functions

List Operations

Map Operations

Calling lookup without a default value will error if the key does not exist.

Summary

By mastering these built-in functions, operators, and expressions, you can write more concise, powerful, and maintainable OpenTofu configurations. Experiment in the console, integrate them into your modules, and streamline your IaC development.

References

Watch Video