hashicorp / terraform-provider-template

Terraform template provider
https://www.terraform.io/docs/providers/template/
Mozilla Public License 2.0
130 stars 89 forks source link

Passing Lists as Template Variables #71

Open voroniys opened 4 years ago

voroniys commented 4 years ago

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Terraform v0.12.8

Affected Resource(s)

Please list the resources as a list, for example: template_file

Terraform Configuration Files

data "template_file" "kops_config" {
  template = file("${path.module}/config.tpl")
  vars     = {
    aws_account_id     = data.aws_caller_identity.current.account_id
    availability_zones = data.aws_availability_zones.available.names
  }
}

Debug Output

Error: Incorrect attribute value type

on kops_config.tf line 3, in data "template_file" "config": 3: vars = {

Inappropriate value for attribute "vars": element "availability_zones": string required.

Expected Behavior

Since in templates %{for } loops now possible I would expect there is a way to pass the list to template as variable

Actual Behavior

List is not accepted as string is required

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply

References

This is kind of re-openning of #40 since there was nothing done to fix this issue.

apparentlymart commented 4 years ago

Hi @voroniys,

Since you are using Terraform 0.12 you can use the templatefile built-in function instead of this data source. The data source continues to exist primarily to support Terraform 0.11 users and provides no significant advantages for 0.12 users. In fact, it has disadvantages like this one specifically because it is built around Terraform 0.11 limitations.

xM8WVqaG commented 4 years ago

@apparentlymart Although the OP's example suggests this functionality for the template_file data source - the templatefile function workaround isn't appropriate for workflows that have to use the template_dir resource that's also part of this provider.

Usecase: Populating a directory of yaml files for consumption in Kubernetes that currently have every IP address and image tag set as individual variables.