-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
page_title: templatestring - Functions - Configuration Language | ||
description: |- | ||
The templatestring function takes a string from elsewhere in the module and renders its content as a template using a supplied set of template variables. | ||
--- | ||
|
||
# `templatestring` Function | ||
|
||
-> **Note:** The `templatestring` function is intended for advanced use cases, and is not the same as a [string template expression](/terraform/language/expressions/strings#string-templates). To render a template from a file, use the [`templatefile` function](/terraform/language/functions/templatefile). | ||
|
||
`templatestring` reads the file at the given path and renders its content | ||
as a template using a supplied set of template variables. | ||
|
||
```hcl | ||
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: | ||
|
||
```hcl | ||
# The following is not allowed | ||
templatestring("Hello, $${name}", { | ||
name = var.name | ||
}) | ||
``` | ||
|
||
Instead of the above, you should instead use a string template expression: | ||
|
||
```hcl | ||
"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](/terraform/language/expressions/strings#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: | ||
|
||
```hcl | ||
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`](/terraform/language/functions/templatefile#Examples) function. | ||
|
||
## Related Functions | ||
|
||
* [`templatefile`](/terraform/language/functions/templatefile) reads a file from disk and renders its content as a template. |