Differences between 'resource "template_file"' and 'data "template_file"'

1,680 views
Skip to first unread message

Matthew Thornton Long

unread,
Aug 25, 2016, 4:04:01 AM8/25/16
to Terraform
Hi,

I've been trying to use 'data template_file' declarations in the same way the 'resource template_file' documentation describes and I've run into a difference.  I was wondering if anyone had an explanation / workaround?

For example, with this block:

[resource|data] "template_file" "role" {
  template = "${file("${path.module}/mongodb.yml.tpl")}"
  count    = "${var.num_nodes}"

  vars {
    environment = "${var.account_name}"
    group       = "Infrastructure"
    primary     = "${lookup(var.primary_status, count.index)}"
  }
}

If this is a resource declaration (basically the "Using Templates with Count" example) then this works exactly as described (albeit with warnings):

$ terraform plan -target module.mongodb 
There are warnings and/or errors related to your configuration. Please
fix these before continuing.

Warnings:

  * template_file.role #2: using template_file as a resource is deprecated; consider using the data source instead
  * template_file.role #0: using template_file as a resource is deprecated; consider using the data source instead
  * template_file.role #1: using template_file as a resource is deprecated; consider using the data source instead

<SNIP>


If this is a "data" block then the following happens when I plan:

$ terraform plan -target module.mongodb 
There are warnings and/or errors related to your configuration. Please
fix these before continuing.

Errors:

  * data.template_file.role #0: : invalid or unknown key: count
  * data.template_file.role #1: : invalid or unknown key: count
  * data.template_file.role #2: : invalid or unknown key: count
<SNIP>
exit status 1

Is this expected?  Can we use counts with 'data template_file' elements the same way we did with resources?

Thanks,

Matt

David Adams

unread,
Aug 25, 2016, 7:47:25 AM8/25/16
to terrafo...@googlegroups.com
I just ran into this yesterday. If we can't use count then whole lot of use cases become broken. Same with template_cloudinit_config.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/3302cd1e-48bf-4cad-ab9e-76c8cbe09fc5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages