github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/website/source/docs/providers/template/r/dir.html.md (about) 1 --- 2 layout: "template" 3 page_title: "Template: template_dir" 4 sidebar_current: "docs-template-resource-dir" 5 description: |- 6 Renders a directory of templates. 7 --- 8 9 # template_dir 10 11 Renders a directory containing templates into a separate directory of 12 corresponding rendered files. 13 14 `template_dir` is similar to [`template_file`](../d/file.html) but it walks 15 a given source directory and treats every file it encounters as a template, 16 rendering it to a corresponding file in the destination directory. 17 18 ~> **Note** When working with local files, Terraform will detect the resource 19 as having been deleted each time a configuration is applied on a new machine 20 where the destination dir is not present and will generate a diff to create 21 it. This may cause "noise" in diffs in environments where configurations are 22 routinely applied by many different users or within automation systems. 23 24 ## Example Usage 25 26 The following example shows how one might use this resource to produce a 27 directory of configuration files to upload to a compute instance, using 28 Amazon EC2 as a placeholder. 29 30 ```hcl 31 resource "template_dir" "config" { 32 source_dir = "${path.module}/instance_config_templates" 33 destination_dir = "${path.cwd}/instance_config" 34 35 vars { 36 consul_addr = "${var.consul_addr}" 37 } 38 } 39 40 resource "aws_instance" "server" { 41 ami = "${var.server_ami}" 42 instance_type = "t2.micro" 43 44 connection { 45 # ...connection configuration... 46 } 47 48 provisioner "file" { 49 # Referencing the template_dir resource ensures that it will be 50 # created or updated before this aws_instance resource is provisioned. 51 source = "${template_dir.config.destination_dir}" 52 destination = "/etc/myapp" 53 } 54 } 55 56 variable "consul_addr" {} 57 58 variable "server_ami" {} 59 ``` 60 61 ## Argument Reference 62 63 The following arguments are supported: 64 65 * `source_dir` - (Required) Path to the directory where the files to template reside. 66 67 * `destination_dir` - (Required) Path to the directory where the templated files will be written. 68 69 * `vars` - (Optional) Variables for interpolation within the template. Note 70 that variables must all be primitives. Direct references to lists or maps 71 will cause a validation error. 72 73 Any required parent directories of `destination_dir` will be created 74 automatically, and any pre-existing file or directory at that location will 75 be deleted before template rendering begins. 76 77 After rendering this resource remembers the content of both the source and 78 destination directories in the Terraform state, and will plan to recreate the 79 output directory if any changes are detected during the plan phase. 80 81 Note that it is _not_ safe to use the `file` interpolation function to read 82 files create by this resource, since that function can be evaluated before the 83 destination directory has been created or updated. It *is* safe to use the 84 generated files with resources that directly take filenames as arguments, 85 as long as the path is constructed using the `destination_dir` attribute 86 to create a dependency relationship with the `template_dir` resource. 87 88 ## Template Syntax 89 90 The syntax of the template files is the same as 91 [standard interpolation syntax](/docs/configuration/interpolation.html), 92 but you only have access to the variables defined in the `vars` section. 93 94 To access interpolations that are normally available to Terraform 95 configuration (such as other variables, resource attributes, module 96 outputs, etc.) you can expose them via `vars` as shown below: 97 98 ```hcl 99 resource "template_dir" "init" { 100 # ... 101 102 vars { 103 foo = "${var.foo}" 104 attr = "${aws_instance.foo.private_ip}" 105 } 106 } 107 ``` 108 109 ## Attributes 110 111 This resource exports the following attributes: 112 113 * `destination_dir` - The destination directory given in configuration. 114 Interpolate this attribute into other resource configurations to create 115 a dependency to ensure that the destination directory is populated before 116 another resource attempts to read it.