Skip to navigation

Filesystem functions

File reads resolve relative to the directory of the source declaring the expression, including a child module. They never use the controller working directory or home. path.module identifies that root.

FunctionResult and rules
abspath(path)Normalized absolute path inside the root; safely missing descendants are allowed.
basename(path)Lexical last path component; does not read the filesystem.
dirname(path)Lexical parent path; does not read the filesystem.
file(path)Read a confined regular UTF-8 file.
filebase64(path)Read a confined regular file as bytes, then standard base64.
fileexists(path)True for a regular file, false only for safely missing files; directories and inspection errors fail.
fileset(root, pattern)Sorted set of relative regular filenames matching *, **, character classes, and brace alternatives.
templatestring(text, vars)Render evaluated text in isolated explicit vars scope; see templates.
templatefile(path, vars)Read and render a confined UTF-8 HCL template with explicit variables; see templates.

Read paths may be relative or absolute inside the declaring root. Parent traversal, URI paths, outside absolute paths, escaping or dangling symlinks, and nonregular reads fail. In-memory sources must supply an explicit root, and all source names in a compilation must be unique so declaring roots cannot be confused. fileset requires an existing base directory, refuses directory symlinks and symlink entries, and inspects at most 100,000 entries.

Local reads may precede global catalog validation because they supply desired values. Cloud resource acquisition waits for global validation. Rendered/read bytes used as file content are bundled; applying needs no source files. Lexical basename/dirname do not grant read access. File hashes are listed under hash and crypto. templatefile and templatestring are detailed under templates.

Example

locals {
files = {
absolute = abspath("assets/config.json")
base = basename("assets/config.json")
parent = dirname("assets/config.json")
text = file("assets/config.json")
bytes = filebase64("assets/config.json")
present = fileexists("assets/config.json")
matches = fileset("assets", "*.json")
}
}
output "files" {
type = string
value = jsonencode(local.files)
}

All calls obey the function limits and annotation rules.