Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

template_file data source doesn't allow count meta-parameter #7919

Closed
gmiroshnykov opened this issue Aug 2, 2016 · 13 comments
Closed

template_file data source doesn't allow count meta-parameter #7919

gmiroshnykov opened this issue Aug 2, 2016 · 13 comments

Comments

@gmiroshnykov
Copy link

Terraform Version

Terraform v0.7.0

Affected Resource(s)

  • template_file

Terraform Configuration Files

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
}

Expected Behavior

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.

Actual Behavior

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

Steps to Reproduce

Simply run terraform plan with the above configuration file.

@jen20
Copy link
Contributor

jen20 commented Aug 3, 2016

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!

@vaygr
Copy link

vaygr commented Aug 3, 2016

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"
  }
}

@spkane
Copy link

spkane commented Aug 3, 2016

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

@osterman
Copy link

osterman commented Aug 3, 2016

Any clever workarounds? :)

@osterman
Copy link

osterman commented Aug 3, 2016

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)}" 

@slobodyanyuk
Copy link

@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 #2627
Thanks!

@RichardKnop
Copy link

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.

@sachincab
Copy link

@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: #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

@matthughes
Copy link

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

@apparentlymart
Copy link
Contributor

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!

@shatil
Copy link

shatil commented Aug 7, 2017

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

@apparentlymart
Copy link
Contributor

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

@ghost
Copy link

ghost commented Apr 7, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests