Template
The Template resource allows the processing of templates, outputing the result as a file.
Templating uses the Handlebars language which is Mustache template language can be found at the following location:.
resource "template" "name" {
...
}
Template Functions
The template resource provides custom functions that can be used inside your templates as shown in the example below.
resource "template" "consul_config" {
source = <<-EOF
file_content = "{{ file "./myfile.txt" }}"
quote = {{quote something}}
trim = {{quote (trim with_whitespace)}}
EOF
destination = "./consul_config/consul.hcl"
}
quote [string]
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 [string]
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"
Attributes
Source source
required
type: string
Local path to the template source file
Destination destination
required
type: string
The destination to write the processed template to.
Variables variables
type: map[string]any
Variables to use with the template, variables are available to be used within the template using the `go template` syntax.
Given the above variables, these could be used within a template with the following convention.
Computed Attributes
These attributes are computed when the config is parsed and applied, and are
therefore only known at parsetime or runtime.
The full ID of the resource e.g. `resource.type.name`.
This is computed from the full resource path:
Meta Type meta.type
string
The type of the resource.
This taken from the type label of the resource definition.
Meta Name meta.name
string
The name of the resource.
This taken from the name label of the resource definition.
Examples
Template using HereDoc
resource "template" "consul_config" {
source = <<-EOF
data_dir = "{{data_dir}}"
log_level = "DEBUG"
datacenter = "dc1"
primary_datacenter = "dc1"
server = true
bootstrap_expect = 1
ui = true
bind_addr = "0.0.0.0"
client_addr = "0.0.0.0"
advertise_addr = "10.6.0.200"
ports {
grpc = 8502
}
connect {
enabled = true
}
EOF
destination = "./consul_config/consul.hcl"
variables = {
data_dir = "/tmp"
}
}
data_dir = "/tmp"
log_level = "DEBUG"
datacenter = "dc1"
primary_datacenter = "dc1"
server = true
bootstrap_expect = 1
ui = true
bind_addr = "0.0.0.0"
client_addr = "0.0.0.0"
advertise_addr = "10.6.0.200"
ports {
grpc = 8502
}
connect {
enabled = true
}
External Files
resource "template "consul_config" {
source = file("./mytemplate.hcl")
destination = "./consul_config/consul.hcl"
variables = {
data_dir = "/tmp"
}
}
container "consul" {
depends_on = ["template.consul_config"]
image {
name = "consul:${variable.consul_version}"
}
command = ["consul", "agent", "-config-file=/config/consul.hcl"]
volume {
source = resource.template.consul_config.destination
destination = "/config/consul.hcl"
}
}
Inline variables
resource "template" "consul_config" {
source = <<-EOF
data_dir = "${data("test")}"
log_level = "DEBUG"
datacenter = "${variable.datacenter}"
server = ${variable.server}
EOF
destination = "./consul_config/consul.hcl"
}