2017-04-14 01:48:51 +02:00
|
|
|
---
|
|
|
|
layout: "template"
|
|
|
|
page_title: "Template: template_dir"
|
|
|
|
sidebar_current: "docs-template-resource-dir"
|
|
|
|
description: |-
|
2017-04-25 19:46:55 +02:00
|
|
|
Renders a directory of templates.
|
2017-04-14 01:48:51 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# template_dir
|
|
|
|
|
2017-04-25 19:46:55 +02:00
|
|
|
Renders a directory containing templates into a separate directory of
|
|
|
|
corresponding rendered files.
|
|
|
|
|
|
|
|
`template_dir` is similar to [`template_file`](../d/file.html) but it walks
|
|
|
|
a given source directory and treats every file it encounters as a template,
|
|
|
|
rendering it to a corresponding file in the destination directory.
|
|
|
|
|
|
|
|
~> **Note** When working with local files, Terraform will detect the resource
|
|
|
|
as having been deleted each time a configuration is applied on a new machine
|
|
|
|
where the destination dir is not present and will generate a diff to create
|
|
|
|
it. This may cause "noise" in diffs in environments where configurations are
|
|
|
|
routinely applied by many different users or within automation systems.
|
2017-04-14 01:48:51 +02:00
|
|
|
|
|
|
|
## Example Usage
|
2017-04-25 19:46:55 +02:00
|
|
|
|
|
|
|
The following example shows how one might use this resource to produce a
|
|
|
|
directory of configuration files to upload to a compute instance, using
|
|
|
|
Amazon EC2 as a placeholder.
|
|
|
|
|
2017-04-14 01:48:51 +02:00
|
|
|
```hcl
|
2017-04-25 19:46:55 +02:00
|
|
|
resource "template_dir" "config" {
|
|
|
|
source_dir = "${path.module}/instance_config_templates"
|
|
|
|
destination_dir = "${path.cwd}/instance_config"
|
2017-04-14 01:48:51 +02:00
|
|
|
|
|
|
|
vars {
|
2017-04-25 19:46:55 +02:00
|
|
|
consul_addr = "${var.consul_addr}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_instance" "server" {
|
|
|
|
ami = "${var.server_ami}"
|
|
|
|
instance_type = "t2.micro"
|
|
|
|
|
|
|
|
connection {
|
|
|
|
# ...connection configuration...
|
|
|
|
}
|
|
|
|
|
|
|
|
provisioner "file" {
|
|
|
|
# Referencing the template_dir resource ensures that it will be
|
|
|
|
# created or updated before this aws_instance resource is provisioned.
|
|
|
|
source = "${template_dir.config.destination_dir}"
|
|
|
|
destination = "/etc/myapp"
|
2017-04-14 01:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 19:46:55 +02:00
|
|
|
|
|
|
|
variable "consul_addr" {}
|
|
|
|
|
|
|
|
variable "server_ami" {}
|
2017-04-14 01:48:51 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## Argument Reference
|
|
|
|
|
|
|
|
The following arguments are supported:
|
|
|
|
|
2017-04-25 19:46:55 +02:00
|
|
|
* `source_dir` - (Required) Path to the directory where the files to template reside.
|
2017-04-14 01:48:51 +02:00
|
|
|
|
2017-04-25 19:46:55 +02:00
|
|
|
* `destination_dir` - (Required) Path to the directory where the templated files will be written.
|
2017-04-14 01:48:51 +02:00
|
|
|
|
|
|
|
* `vars` - (Optional) Variables for interpolation within the template. Note
|
|
|
|
that variables must all be primitives. Direct references to lists or maps
|
|
|
|
will cause a validation error.
|
|
|
|
|
2017-04-25 19:46:55 +02:00
|
|
|
Any required parent directories of `destination_dir` will be created
|
|
|
|
automatically, and any pre-existing file or directory at that location will
|
|
|
|
be deleted before template rendering begins.
|
|
|
|
|
|
|
|
After rendering this resource remembers the content of both the source and
|
|
|
|
destination directories in the Terraform state, and will plan to recreate the
|
|
|
|
output directory if any changes are detected during the plan phase.
|
|
|
|
|
|
|
|
Note that it is _not_ safe to use the `file` interpolation function to read
|
|
|
|
files create by this resource, since that function can be evaluated before the
|
|
|
|
destination directory has been created or updated. It *is* safe to use the
|
|
|
|
generated files with resources that directly take filenames as arguments,
|
|
|
|
as long as the path is constructed using the `destination_dir` attribute
|
|
|
|
to create a dependency relationship with the `template_dir` resource.
|
2017-04-14 01:48:51 +02:00
|
|
|
|
|
|
|
## Template Syntax
|
|
|
|
|
|
|
|
The syntax of the template files is the same as
|
|
|
|
[standard interpolation syntax](/docs/configuration/interpolation.html),
|
|
|
|
but you only have access to the variables defined in the `vars` section.
|
|
|
|
|
|
|
|
To access interpolations that are normally available to Terraform
|
|
|
|
configuration (such as other variables, resource attributes, module
|
2017-04-25 19:46:55 +02:00
|
|
|
outputs, etc.) you can expose them via `vars` as shown below:
|
2017-04-14 01:48:51 +02:00
|
|
|
|
|
|
|
```hcl
|
|
|
|
resource "template_dir" "init" {
|
|
|
|
# ...
|
|
|
|
|
|
|
|
vars {
|
|
|
|
foo = "${var.foo}"
|
|
|
|
attr = "${aws_instance.foo.private_ip}"
|
|
|
|
}
|
|
|
|
}
|
2017-04-25 19:46:55 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
## Attributes
|
|
|
|
|
|
|
|
This resource exports the following attributes:
|
|
|
|
|
|
|
|
* `destination_dir` - The destination directory given in configuration.
|
|
|
|
Interpolate this attribute into other resource configurations to create
|
|
|
|
a dependency to ensure that the destination directory is populated before
|
|
|
|
another resource attempts to read it.
|