» template_file

The template_file data source renders a template from a template string, which is usually loaded from an external file.

» Example Usage

data "template_file" "init" {
  template = "${file("${path.module}/init.tpl")}"
  vars = {
    consul_address = "${aws_instance.consul.private_ip}"

Inside init.tpl you can include the value of consul_address. For example:


echo "CONSUL_ADDRESS = ${consul_address}" > /tmp/iplist

Although in principle template_file can be used with an inline template string, we don't recommend this approach because it requires awkward escaping. Instead, just use template syntax directly in the configuration. For example:

  user_data = <<-EOT
    echo "CONSUL_ADDRESS = ${aws_instance.consul.private_ip}" > /tmp/iplist

» Argument Reference

The following arguments are supported:

  • template - (Required) The contents of the template, as a string using Terraform template syntax. Use the file function to load the template source from a separate file on disk.

  • 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.

Earlier versions of template_file accepted another argument filename as an alternative to template. This has now been removed. Use the template argument with the file function to get the same effect.

» Template Syntax

The template argument is processed as Terraform template syntax.

However, this provider has its own copy of the template engine embedded in it, separate from Terraform itself, and so which features are available are decided based on what Terraform version the provider was compiled against, and not on which Terraform version you are running.

For more consistent results, Terraform 0.12 has a built in function templatefile which serves the same purpose as this data source. Use that function instead if you are using Terraform 0.12 or later. Its template and expression capabilities will always match the version of Terraform you are using.

» Attributes Reference

The following attributes are exported:

  • template - See Argument Reference above.
  • vars - See Argument Reference above.
  • rendered - The final rendered template.