templatestring Function
Note: The templatestring
function is intended for advanced use cases. Most use cases require only a string template expression. To render a template from a file, use the templatefile
function.
templatestring
renders a template using a supplied set of template variables.
templatefile(ref, vars)
The first parameter must be a simple reference to string value containing the template: for example, data.aws_s3_object.example.body
or local.inline_template
.
It is not valid to supply the template expression directly as the first argument:
# The following is not allowedtemplatestring("Hello, $${name}", { name = var.name})
Instead of the above, you should instead use a string template expression:
"Hello, ${var.name}"
The templatestring
function is needed only when the template is available as a named object in the current module.
The template syntax is the same as for
string templates
in the main Terraform language, including interpolation sequences delimited with
${
... }
.
Strings in the Terraform language are sequences of Unicode characters, so this function will interpret the file contents as UTF-8 encoded text and return the resulting Unicode characters. If the template contains invalid UTF-8 sequences then this function will produce an error.
Example
The following example retrieves a template from S3 and dynamically renders it:
data "aws_s3_object" "example" { bucket = "example-example" key = "example.tmpl"}output "example" { value = templatestring(data.aws_s3_object.example.body, { name = var.name })}
For more examples of how to use templates, please see the documentation for the templatefile
function.
Related Functions
templatefile
reads a file from disk and renders its content as a template.