templatestring function reference
This topic provides reference information about the templatestring
function. The templatestring
function renders a string defined elsewhere in the module as a template using a set of variables.
Introduction
The primary use case for the templatestring
function is to render templates fetched as a single string from remote locations. The function enables advanced use cases where a string template expression is insufficient, such as when the template is available from a named object declared in the current module. To render a template from a file, use the templatefile
function.
Syntax
The templatestring
function takes an argument that references an object defined in the module and one or more arguments that specify variables to use for rendering the template:
In the following example, the function renders the string value located at data.aws_s3_object.example.body
as the template:
For information about the syntax you can use for the variables arguments, refer to Strings and Templates.
Example
The following example retrieves a template from S3 and dynamically renders it:
For more examples of how to use templates, refer to the documentation for the templatefile
function.
Dynamic template construction
You can write an expression that builds a template dynamically and then assigns it to a local value. You can then use a reference to that local value as the first argument to templatestring
.
Note that you should only dynamically construct templates in this way when no other alternative is feasible. This is because the result can be difficult to understand and maintain and susceptible to unexpected inputs. Built-in Terraform functions may interact with the local filesystem. As a result, the inputs may produce a template that includes data from arbitrary files on the system where Terraform is running.
Related Functions
templatefile
reads a file from disk and renders its content as a template.