Filesystem Function

The Terraform Filesystem Function allows interaction with the file system during the execution of Terraform. This includes various methods to read and write files, and to manipulate the file system.

Types of Filesystem Functions

Terraform provides the following types of Filesystem functions:

1. abspath Function

The abspath function in Terraform takes a string containing a filesystem path and converts it into an absolute path. If the given input path does not specify an absolute path (i.e, it does not start with a root directory such as / or C:\) the abspath will perform a join with the current working directory to create the full absolute path. Syntax
abspath(string)
For example
terraform console
> abspath("terraform/root-modules")
"/Users/ajay.anbazhagan/terraform/root-modules"
> abspath("/root")
"/root"
> abspath(path.root)
"/Users/ajay.anbazhagan/terraform/root-modules"

2. dirname Function

The dirname function in Terraform takes a string containing a filesystem path and removes the last portion (the filename or last directory) of that path, returning the directory path. The syntax is:
dirname(string)
Behavior of dirname depends on host platform: While on Windows, backslash \ is used internally as path separator, on Unix-like systems this is forward slash /. Output of dirname is normalized, meaning any forward slashes in an input path, on Windows, would be replaced by backslashes.
For example
terraform console
> dirname("/home/ubuntu/docker/config.txt")
"/home/ubuntu/docker"
> dirname("/home/ubuntu/m2/directory")
"/home/ubuntu/m2"
If the input path is empty, it returns ".", representing the current working directory. For example
terraform console
> dirname("")
"."

3. basename Function

The basename in Terraform will take a string containing a filesystem path and remove all except the last portion from it. It returns the filename or respective last directory in the path. Syntax:
basename(string)
Behavior of basename depends on host platform: While on Windows, backslash \ is used internally as path separator, on Unix-like systems this is forward slash /.
For example
terraform console
> basename("/home/ubuntu/docker/config.txt")
"config.txt"
> basename("/home/ubuntu/m2/directory")
"directory"
If the input path is empty, it returns ".", representing the current working directory. For example
terraform console
> basename("")
"."

4. pathexpand Function

The pathexpand function takes a filesystem path that may start with a ~ (tilde) and replaces it with the current user's home directory path. If the path doesn't start with ~, it leaves the path unchanged. The syntax is
pathexpand(string)
Avoid using pathexpand in resource arguments, because it can cause problems if the configuration is run by multiple users or on different operating systems.
Depending on the OS of the host machine, Terraform uses different methods in order to find the "home directory" for the current user. On Unix systems, in order, Terraform will consult the following sources in order to attempt to find the home directory:
Unlike Unix systems, Windows does not have a traditional concept of a home directory. However, Terraform will use the following sources to determine a Windows equivalent directory, in order of preference:
For example
terraform console
> pathexpand("~/terraform")
"/home/ubuntu/terraform"

5. file Function

The file function reads the contents of a file at a specified path and returns it as a string. The syntax for using the file function is
file(path)
It reads the contents of the file as UTF-8 encoded text and returns the resulting Unicode characters. It will raise an error if the file contains invalid UTF-8 sequences.
For example
terraform console
> file("example.txt")
"Hello viewer"
This can only be used with files that existed on disk at the start of a Terraform run, and cannot be used if the file will be generated dynamically as part of the operation, since it does not participate in the dependency graph. If dynamic local files are needed, consider using the local_file data source instead, which respects resource dependencies.

6. fileexists Function

The fileexists function checks if a file exists at a specified path and returns true or false. The syntax is
fileexists(path)
This function is evaluated during configuration parsing, not at apply time, so it can only be used with files that are already existing on disk prior to Terraform taking any action.
For example
terraform console
> fileexists("example.txt")
false

7. filebase64 Function

The filebase64 function reads the contents of a file at a specified path and returns them as a base64-encoded string. The syntax is
filebase64(path)
This function can be used only with files that exist on disk at the start of a Terraform run. It does not participate in the dependency graph, and so it cannot be used by files generated dynamically as part of a Terraform operation.
For example
terraform console
> filebase64("example.txt")
"dGVycmFmb3JtCg=="

8. fileset Function

The fileset function returns a set of regular file names that match a specified path and pattern. The syntax is
fileset(path, pattern)
This function removes the path from resultant file names. This function can only be used with files that exist on disk at the start of a Terraform run. It does not participate in the dependency graph and hence cannot be used with files dynamically generated in the course of a Terraform running operation.
It supports various pattern matches like:
terraform console
> fileset("./examples-terraform", "*.txt")
toset([
    "file1.txt",
    "file2.txt",
    "file3.txt",
])
terraform console
> fileset(".","sub?.txt")
toset([
    "sub1.txt",
    "sub2.txt",
    "sub3.txt",
])
terraform console
> fileset(".","sub{1,2,3}.txt")
toset([
    "sub1.txt",
    "sub2.txt",
    "sub3.txt",
])
terraform console
> fileset(".","sub[a-z].txt")
toset([
    "suba.txt",
    "subb.txt",
    "subc.txt",
])
> fileset(".","sub[1-3].txt")
toset([
    "sub1.txt",
    "sub2.txt",
    "sub3.txt",
])
> fileset(".","sub[12].txt")
toset([
    "sub1.txt",
    "sub2.txt"
])
terraform console
> fileset(".","sub[^a-z].txt")
toset([])

9. templatefile Function

The templatefile function reads a file and renders its content as a template using a set of supplied variables. The syntax is
templatefile(path, vars)
Here's a breakdown of the syntax:
The syntax of the template is the same as string templates in Terraform, using ${...} for interpolation. The file must already exist on disk at the beginning of a Terraform run. Unfortunately it isn't possible to support dynamic files generated during part of a Terraform run.
For example
// config.tftpl
%{ if has_ha ~}
    HA Proxy: ${proxy_url}
%{ endif ~}
%{ if has_load_balancer ~}
    Load Balancer: ${load_balancer_url}
%{ endif ~}
local {
    region = "us-west-2"
    has_ha = true
    has_load_balancer = false
    proxy_url = "https://example.com/proxy"
    load_balancer_url = "https://example.com/load-balancer"
}
output "rendered_template" {
    value = templatefile("config.tftpl", {
        has_ha = local.has_ha
        has_load_balancer = local.has_load_balancer
        proxy_url = local.proxy_url
        load_balancer_url = local.load_balancer_url
    })
}
HA Proxy: https://example.com/proxy

Related Pages

Feedback

Was this page helpful?