1

What does this mean:

Note: Inline templates must escape their interpolations (as seen by the double 
$ above). Unescaped interpolations will be processed before the template.

from https://www.terraform.io/docs/providers/template/index.html

The specific example is:

# Template for initial configuration bash script
data "template_file" "init" {
  template = "$${consul_address}:1234"

  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

1 Answers1

2

The ${} syntax is used by HCL for interpolation before the template rendering happens so if you were to just use:

# Template for initial configuration bash script
data "template_file" "init" {
  template = "${consul_address}:1234"

  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}

Terraform will attempt to find consul_address to template into the output instead of using the template variable of consul_address (which in turn is resolved to the private_ip output of the aws_instance.consul resource.

This is only an issue for inline templates and you don't need to do this for file based templates. For example this would be fine:

int.tpl

#!/bin/bash

echo ${consul_address} 

template.tf

# Template for initial configuration bash script
data "template_file" "init" {
  template = "${file("init.tpl")}"

  vars {
    consul_address = "${aws_instance.consul.private_ip}"
  }
}

Of course if you then also needed to use the ${} syntax literally in your output template then you would need to double escape with something like this:

#!/bin/bash

CONSUL_ADDRESS_VAR=${consul_address}
echo $${CONSUL_ADDRESS_VAR}

This would then be rendered as:

#!/bin/bash

CONSUL_ADDRESS_VAR=1.2.3.4
echo ${CONSUL_ADDRESS_VAR}
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177