---
layout: "template"
page_title: "Template: template_dir"
sidebar_current: "docs-template-resource-dir"
description: |-
  Renders templates from a directory.
---

# template_dir

Renders templates from a directory.

## Example Usage
```hcl
data "template_directory" "init" {
  source_dir      = "${path.cwd}/templates"
  destination_dir = "${path.cwd}/templates.generated"
  
  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}
```

## Argument Reference

The following arguments are supported:

* `source_path` - (Required) Path to the directory where the files to template reside.

* `destination_path` - (Required) Path to the directory where the templated files will be written.

* `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.

NOTE: Any required parent directories are created automatically. Additionally, any external modification to either the files in the source or destination directories will trigger the resource to be re-created.

## 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
outputs, etc.) you'll have to expose them via `vars` as shown below:

```hcl
resource "template_dir" "init" {
  # ...

  vars {
    foo  = "${var.foo}"
    attr = "${aws_instance.foo.private_ip}"
  }
}
```
Add a 'dir' resource to template entire directories
provider/template: Add a 'dir' resource to template entire directories

When TerraForm is used to configure and deploy infrastructure
applications that require dozens templated files, such as Kubernetes, it
becomes extremely burdensome to template them individually: each of them
requires a data source block as well as an upload/export (file
provisioner, AWS S3, ...).

Instead, this commit introduces a mean to template an entire folder of
files (recursively), that can then be treated as a whole by any provider
or provisioner that support directory inputs (such as the
file provisioner, the archive provider, ...).

This does not intend to make TerraForm a full-fledged templating system
as the templating grammar and capabilities are left unchanged. This only
aims at improving the user-experience of the existing templating
provider by significantly reducing the overhead when several files are
to be generated - without forcing the users to rely on external tools
when these templates stay simple and that their generation in TerraForm
is justified.
