Terraform v0.7.0
The new data source fails:
data "template_file" "example" {
template = "Hello World!"
count = 2
}
While the legacy resource works fine (although with a deprecation warning):
resource "template_file" "example" {
template = "Hello World!"
count = 2
}
Something similar to what legacy resource gives:
[...skipped...]
+ template_file.example.0
rendered: "<computed>"
template: "Hello World!"
+ template_file.example.1
rendered: "<computed>"
template: "Hello World!"
Plan: 2 to add, 0 to change, 0 to destroy.
There are warnings and/or errors related to your configuration. Please
fix these before continuing.
Errors:
* data.template_file.example #1: : invalid or unknown key: count
* data.template_file.example #0: : invalid or unknown key: count
Simply run terraform plan
with the above configuration file.
Hi @laggyluke - we're aware of the limitation of not being able to use count
with data sources, and will address this in an upcoming version. Thanks for opening an issue!
Yes, I'm facing the same issue while trying to convert this to data sources usage after 0.7.0 release and drop the deprecation warning:
resource "template_file" "config" {
count = "${length(keys(var.hosts))}"
template = "${file("${path.module}/config.tpl")}"
vars {
host = "${element(values(var.hosts), count.index)}"
}
}
resource "google_compute_instance" "server" {
count = "${length(keys(var.hosts))}"
...
provisioner "file" {
content = "${element(template_file.config.*.rendered, count.index)}"
destination = "/etc/default/server.conf"
}
}
@jen20 It would be useful to update the _Migrating to Data Sources_ section of the upgrade docs (https://www.terraform.io/upgrade-guides/0-7.html) with a note about this, so that folks don't upgrade all their files before realizing that they simply need to revert it, since this important functionality is missing.
Any clever workarounds? :)
I'm using the data template_file
provider for user_data
. Seems like I'm able to work around the problem by defining a macro like {count}
and doing a string replace on the .rendered
value.
e.g.
user_data = "${replace(data.template_file.kafka.rendered, "{count}", count.index)}"
@osterman Yep, looks like it's the only way to workaround this issue that should have been fixed in 0.7 but it's not https://github.com/hashicorp/terraform/issues/2627
Thanks!
There is no workaround for this in case you are doing something more than just identical template where you can just replace {count}. For example, generating self signed certificates for multiple nodes of a cluster with tls_cert_request
is broken.
For, now I will keep using the deprecated resources until it's fixed.
@RichardKnop Specially with tls_cert_request, when you start using deprecated resource type for count usage.
Apparently when you try to update your stack, it complain with below errors with count functionality for tls_cert_request.
Discussed here: https://github.com/hashicorp/terraform/issues/7821
Error refreshing state: 2 error(s) occurred:
* tls_cert_request.node-crs.1: no PEM block found in private_key_pem
* tls_cert_request.node-crs.0: no PEM block found in private_key_pem
Also, FYI but the docs definitely have examples where data.template_file use the count meta parameter. For example: https://www.terraform.io/docs/configuration/interpolation.html
Hi all! This was actually fixed in #8635, which was included in the Terraform 0.7.3 release. Sorry that this ticket ended up getting missed when posting updates!
@apparentlymart I'm using the latest Terraform (0.10.0), and I still can't reference rendered data by count.index
:
Error reading config for aws_s3_bucket[conf]: parse error at 1:41: expected "}" but found "."
With:
variable "environments" {
default = ["dev", "prod"]
description = "Environment to create resources in."
}
data "template_file" "policy" {
count = "${length(var.environments)}"
template = ""
}
resource "aws_s3_bucket" "conf" {
count = "${length(var.environments)}"
bucket = "conf-${var.environments[count.index]}"
policy = "${data.template_file.policy[count.index].rendered}"
}
If I use policy = "${element(data.template_file.policy.*.rendered, count.index)}"
instead, like @marcboudreau suggested, then terraform plan
works fine.
Hi @shatil,
At this time it doesn't work to use the [...]
index syntax to index into a set of resources, so using the splat syntax is the recommended approach. Still using the index syntax with the splat is better, though:
policy = "${data.template_file.policy.*.rendered[count.index]}"
Writing it this way allows Terraform to understand that only this specific index is needed, preventing overly-conservative behavior when the count is increased.
Supporting the more intuitive syntax you tried here is one of the things on our early list of configuration language improvements we plan to make in a future version. We'll have more to share about that soon.
(If you have any follow-up questions/feedback here, let's take it to a fresh issue, since this is not directly related to this original issue as filed and continuing the conversation here will create notification noise for the several people watching this one.)
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
@jen20 It would be useful to update the _Migrating to Data Sources_ section of the upgrade docs (https://www.terraform.io/upgrade-guides/0-7.html) with a note about this, so that folks don't upgrade all their files before realizing that they simply need to revert it, since this important functionality is missing.