github.com/ves/terraform@v0.8.0-beta2/website/source/docs/providers/template/d/file.html.md (about) 1 --- 2 layout: "template" 3 page_title: "Template: template_file" 4 sidebar_current: "docs-template-datasource-file" 5 description: |- 6 Renders a template from a file. 7 --- 8 9 # template\_file 10 11 Renders a template from a file. 12 13 ## Example Usage 14 15 From a file: 16 17 ``` 18 data "template_file" "init" { 19 template = "${file("${path.module}/init.tpl")}" 20 21 vars { 22 consul_address = "${aws_instance.consul.private_ip}" 23 } 24 } 25 ``` 26 27 Inline: 28 29 ``` 30 data "template_file" "init" { 31 template = "$${consul_address}:1234" 32 33 vars { 34 consul_address = "${aws_instance.consul.private_ip}" 35 } 36 } 37 ``` 38 39 ## Argument Reference 40 41 The following arguments are supported: 42 43 * `template` - (Required) The contents of the template. These can be loaded 44 from a file on disk using the [`file()` interpolation 45 function](/docs/configuration/interpolation.html#file_path_). 46 47 * `vars` - (Optional) Variables for interpolation within the template. Note 48 that variables must all be primitives. Direct references to lists or maps 49 will cause a validation error. 50 51 The following arguments are maintained for backwards compatibility and may be 52 removed in a future version: 53 54 * `filename` - _Deprecated, please use `template` instead_. The filename for 55 the template. Use [path variables](/docs/configuration/interpolation.html#path-variables) to make 56 this path relative to different path roots. 57 58 ## Attributes Reference 59 60 The following attributes are exported: 61 62 * `template` - See Argument Reference above. 63 * `vars` - See Argument Reference above. 64 * `rendered` - The final rendered template. 65 66 ## Template Syntax 67 68 The syntax of the template files is the same as 69 [standard interpolation syntax](/docs/configuration/interpolation.html), 70 but you only have access to the variables defined in the `vars` section. 71 72 To access interpolations that are normally available to Terraform 73 configuration (such as other variables, resource attributes, module 74 outputs, etc.) you'll have to expose them via `vars` as shown below: 75 76 ``` 77 data "template_file" "init" { 78 # ... 79 80 vars { 81 foo = "${var.foo}" 82 attr = "${aws_instance.foo.private_ip}" 83 } 84 } 85 ``` 86 87 ## Inline Templates 88 89 Inline templates allow you to specify the template string inline without 90 loading a file. An example is shown below: 91 92 ``` 93 data "template_file" "init" { 94 template = "$${consul_address}:1234" 95 96 vars { 97 consul_address = "${aws_instance.consul.private_ip}" 98 } 99 } 100 ``` 101 102 -> **Important:** Template variables in an inline template (such as 103 `consul_address` above) must be escaped with a double-`$`. Unescaped 104 interpolations will be processed by Terraform normally prior to executing 105 the template. 106 107 An example of mixing escaped and non-escaped interpolations in a template: 108 109 ``` 110 variable "port" { default = 80 } 111 112 data "template_file" "init" { 113 template = "$${foo}:${var.port}" 114 115 vars { 116 foo = "${count.index}" 117 } 118 } 119 ``` 120 121 In the above example, the template is processed by Terraform first to 122 turn it into: `$${foo}:80`. After that, the template is processed as a 123 template to interpolate `foo`. 124 125 In general, you should use template variables in the `vars` block and try 126 not to mix interpolations. This keeps it understandable and has the benefit 127 that you don't have to change anything to switch your template to a file.