Take the following example:
Hi ${foo}, ${bar}
variable "var1" {
default = "Yoda"
}
resource "template_file" "default" {
template = "${file("template.tpl")}"
vars {
foo = "${var.var1}"
bar = "${var1}"
}
}
and run
$ terraform apply
Error applying plan:
1 error(s) occurred:
* template_file.default: failed to render : 1:6: unknown variable accessed: foo
Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
I'm not sure whether this is an issue related to template_file resource or core or HCL parser, I found the error message in ./config/lang/ so I categorised this as core.
I also just ran into this, and it confused me for a little bit until I spotted the error. Would be great to have a less misleading error message here if possible.
Here's a related issue:
variable foo { default = "blaz" }
resource template_file t {
template = "${var.foo}${bar}"
}
$ terraform apply
template_file.t: Creating...
rendered: "" => "<computed>"
template: "" => "${var.foo}${bar}"
Error applying plan:
1 error(s) occurred:
* template_file.t: failed to render : 1:3: unknown variable accessed: var.foo
Instead of complaining about the bar real error, the error message complains about var.foo.
I tracked this down enough to figure out that the error is coming from the unknown variable accessed in check_identifier.go, not one of the other 4 instances of that error.
My vague theory here, not based on much, is that the "initial" evaluation of the template fails because bar is unknown, but instead of that being an error the full literal template gets passed through to the the template_file plugin... which now fails because it doesn't know about var.foo which is only available in the initial config evaluation. Something related to UnknownVariableValue...
Basically, this error message seems to mean that there are multiple interpolations directly in the template body, and some of them work at first and others don't. I can see why you'd want to support "all of the interpolations in the body are of the template's declared vars", and I can see why you'd want to support "do interpolations directly in the template body" (most notably to make file() work)... but maybe there's a way to make it a clearer error to combine them?
I have this problem currently....
I just spent 3 hours debugging an error in my call to module. Just as this explained I left off the var. on a variable assignment and the error was on the template file not on the call to module
+100 to fix this
I was also like @itsguy, but spent way more time than I choose to admit before googling to come across this issue.
I think #9698 will clear this up, though perhaps not by addressing the root cause... It will make Terraform complain about var1 in Radek's example, which was what was expected here, but I don't know if the weird error reporting here is the same problem or if this fix will just mask it.
Fixed in 0.8
Yup just spent a while figuring it out, if you're here STOP, look at your template, you've named the variable "${var.variable}" instead of "${variable}"
And that would also fail if the line using ${var.X} is commented out inside the template, just saying
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
I just spent 3 hours debugging an error in my call to module. Just as this explained I left off the var. on a variable assignment and the error was on the template file not on the call to module
+100 to fix this