»Templating Functions

Waypoint has a set of functions for templating strings, files, and directories with both predefined and custom variable values. These templates can use conditionals, loops, and more to dynamically generate content.

Waypoint has three templating functions documented below: templatefile, templatedir, and templatestring. All of these functions use the same templating syntax. The templates have access to all the same variables and functions that are available at the function call-site (where the function such as templatedir is called). Custom variables can also be provided to the template.

»Syntax

Template syntax uses HCL string templates. This supports conditionals, loops, function calls, etc.

»templatefile

templatefile(path[, vars...]) path
templatefile(path[, vars...]) path

templatefile takes a path to a file and optionally one or more maps of variables for the template, renders the template at path, and returns the path to the rendered template. The returned path is a copy of the original template in a temporary location. This function does not modify the original file.

Reminder: This function returns a file path, not the rendered contents of the file. If you want the contents of the rendered file, you can wrap this function with file.

This function is useful for parameters that expect file paths. The example below shows using this function with Docker to template the Dockerfile:

app "web" {
  build {
    use "docker" {
      dockerfile = templatefile("${path.app}/Dockerfile.tpl", {
        theme = "rainbow"
      })
    }
  }
}
app "web" {  build {    use "docker" {      dockerfile = templatefile("${path.app}/Dockerfile.tpl", {        theme = "rainbow"      })    }  }}

»templatedir

templatedir(path[, vars...]) path
templatedir(path[, vars...]) path

templatedir is similar to templatefile, except it takes a path to a directory, treats all files within that directory as a template, copies the results to a new temporary directory, and returns the temporary directory path.

This function will recursively render all files in path.

This function is useful for parameters that expect directory paths, such as specifying a directory of YAML files for Kubernetes.

»templatestring

templatestring(string[, vars...]) string
templatestring(string[, vars...]) string

templatestring renders a string as a template directly and returns the rendered contents.

Example, rendering a template file and returning the contents directly:

userdata = templatestring(file("${path.app}/userdata.tpl"))
userdata = templatestring(file("${path.app}/userdata.tpl"))