mirror of https://github.com/hashicorp/terraform
lang: stabilise templatestring func experiment (#35224)
* lang: stabilise templatestring func experiment * command/jsonfunction: marshal templatestring * docs: add templatestringpull/35261/head
parent
b5d6b13b3b
commit
1e2d4a2ecc
@ -0,0 +1,67 @@
|
||||
---
|
||||
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. Most use cases require only 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` renders 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.
|
||||
Loading…
Reference in new issue