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