hashicorp/terraform

question: why no interpolations in variables?

ketzacoatl opened this issue · 3 comments

When I try to define something like:

variable "extra_pillar" {
    description = "extra pillar that is shared across init templates for worker clusters"
    default = <<EOF
worker:
  redis: ${module.jobq-a.redis_url}
  image: ${var.worker_image}
  tag: ${var.worker_tag}
EOF
}

I get variable 'extra_pillar': cannot contain interpolations. I am surprised, in a way.

Obviously, one could get into circular deps with interpolation in variables, but then there are also times when it seems valid / sensible to leverage interpolation in variables. Is there a better way to achieve similar functionality?

jen20 commented

Hi @ketzacoatl. Currently we require variables to be known up front prior to interpolation - it would be a reasonably large change to adjust this.

In this particular case your best bet is to use the template_file resource, and you can then use the rendered argument to get the output:

resource "template_file" "extra_pillar" {
    template = <<EOF
worker:
  redis: ${redis_url}
  image: ${image}
  tag: ${tag}
EOF

    vars {
        redis_url = "${module.jobq-a.redis_url}"
        image = "${var.worker_image}"
        tag = "${var.worker_tag}"
    }
}

output "template" {
    value = "${template_file.extra_pillar.rendered}"
}

If this doesn't work for you, please feel free to reopen!

thanks @jen20, I will see how far I push the limits of template_file :)

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.