Justin Seiser
Justin Seiser

Reputation: 389

How to have a literal string of ${something} in a template data file

I have a template file, that is creating a fluentd file and inserting various variables. I am now trying to include this plugin which expects to find its own variables in the config file. The problem is that Terraform defines a variable in a template like ${variable} and this plugin expects to find its variables in the file as literal ${variable}

How can I tell terraform to not interpolate a ${} in a file, but to actually pass that entire string?

File Snippet:

<filter tomcat.logs>
  @type record_transformer
  <record>
    customer ${customer}
    environment ${environment}
    application ${application}
  </record>
</filter>

The above ${} are all variables I have defined for my template. I now need to add a section like this.

  <record>
    hostname      ${tagset_name}
    instance_id   ${instance_id}
    instance_type ${instance_type}
    az            ${availability_zone}
    private_ip    ${private_ip}
    vpc_id        ${vpc_id}
    ami_id        ${image_id}
    account_id    ${account_id}
  </record>

Where all of those are not variables but how it actually needs to look in the rendered template. I tried swapping them to be like $${account_id}, but that just ends up rendering account_id in the file.

data "template_file" "app" {
  template = "${file("templates/${var.application}.tpl")}"

  vars {
    customer               = "${var.customer}"
    environment            = "${var.environment}"
    application            = "${var.application}"
  }
}

Here is a breakdown of what is happening.

In the user data I have "instance_type $${instance_type}"  
The launch    configuration that is created for the instances, shows "instance_type    ${instance_type}"  
The actual file that is present on AWS shows    "instance_type"

Upvotes: 6

Views: 9631

Answers (2)

Javier Mont&#243;n
Javier Mont&#243;n

Reputation: 5696

In my case, having this issue inside a resource, the value is inside quotes and I don't need the \ to fix it.

somevalue = "$${variable}"

produces "${variable}" instead of "my_value"

Upvotes: 0

Justin Seiser
Justin Seiser

Reputation: 389

Finally figured this out. The answer from the marked duplicate question is incorrect for this instance.

template.tpl contains

cat <<EOT > /root/test.file
db.type=${db_type}
instance_type \$${instance_type}
EOT

Result

Error: Error refreshing state: 1 error(s) occurred:

* module.autoscaling_connect.data.template_file.app: 1 error(s) occurred:

* module.autoscaling_connect.data.template_file.app: data.template_file.app: failed to render : 27:16: unknown variable accessed: bogus_value

template.tpl contains

cat <<EOT > /root/test.file
db.type=${db_type}
instance_type \$${instance_type}
EOT

Results in a launch configuration containing

cat <<EOT > /root/test.file
db.type=mysql
instance_type \${instance_type}
EOT

Results in the File we created on the instance containing

db.type=mysql
instance_type ${instance_type}

In Short to end up with a ${something} in the file created from a terraform template file, you have to use \$${something} in the .tpl file.

Upvotes: 10

Related Questions