Functions

Configuration files can use built-in functions which are interpolated at runtime. For example the env(<environment-variable-name>) function could be used to return the value of an environment variable.

env

The env function can be used to interpolate a system set environment variable inside your configuration.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    path = env("PATH")
  }
}

len

The len function returns the length of a string or an array.

variable "myarray" {
  value = [1,2,3]
}

variable "mystring" {
  value = "string"
}

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    array_length = len(variable.myarray) // 3
    string_length = len(variable.mystring) // 6
  }
}

home

The home function returns the location of the current users HOME folder.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    home = home()
  }
}

file

The file function returns the contents of a file at the given path.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    contents = file("myfile.txt")
  }
}

dir

The dir function returns the directory of the file containing the current resource.

resource "template" "mine" {
  source = "${dir()}/mysource.txt"
  destination = "./mydestination.txt"
}

trim

The trim function removes whitespace from the given string.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestionation.txt"

  variables = {
    contents = trim(file("myfile.txt"))
  }
}

docker_ip

The docker_ip function returns the ip address of the configured docker host.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestination.txt"

  variables = {
    url = "${docker_ip()}:8080"
  }
}

docker_host

The docker_host function returns the socket or TCP address for the current Docker host.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "./mydestination.txt"

  variables = {
    DOCKER_HOST = docker_host()
  }
}

data

The data function creates a temporary folder that exists until sandbox is destroyed. Data folders are created with the permissions 755.

First use of data creates the folder, subsequent uses return the path.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "${data("temp")}/file.txt"

  variables = {
    DOCKER_HOST = docker_host
  }
}

data_with_permissions

The data_with_permissions function creates a temporary folder that exists until the sandbox is destroyed. Unlike data, data_with_permissions allows you to specify the directory permissions as a parameter.

First use of data_with_permissions creates the folder, subsequent uses return the path.

resource "template" "mine" {
  source = "./mysource.txt"
  destination = "${data_with_permissions("temp", 644)}/file.txt"

  variables = {
    DOCKER_HOST = docker_host
  }
}

system

When "os" is passed in as a parameter it returns the OS of the system (darwin, linux, windows, etc.), and when "arch" is passed in as a parameter it returns the architecture of the system (amd64, arm64, etc.).

resource "copy" "cli" {
  source = "https://github.com/instruqt/cli/releases/download/2209-ff87c62/instruqt-${system("os")}-${system("arch")}.zip"
  destination = "./cli"
  permissions = "0755"
}

template_file

Returns the rendered contents of a template file at the given path with the given input variables.

Templates can leverage the Handlebars templating language, more details on Handlebars can be found at https://handlebarsjs.com/

#given a file "./mytemplate.tmpl" with the contents "hello {{name}}"

mytype "test" {
  // my_file = "foobar"
  my_file = template_file("./mytemplate.tmpl", {
    name = "world"
  })
}

Template helpers

The template_file function provides helpers that can be used inside your templates as shown in the examples below.

quote

Returns the original string wrapped in quotations, quote can be used with the Go template pipe modifier.

// given the string 'abc'

quote "abc" // would return the value '"abc"'

trim

Removes whitespace such as carrige returns and spaces from the begining and the end of the string, can be used with the Go template pipe modifier.

// given the string ' abc '

trim " abc " // would return the value 'abc'

Last updated