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 resource failed to render with unknown variable accessed #10148

Closed
jcderose opened this issue Nov 15, 2016 · 7 comments
Closed
Labels
bug core waiting-response An issue/pull request is waiting for a response from the community

Comments

@jcderose
Copy link

jcderose commented Nov 15, 2016

Terraform Version

0.7.9

Affected Resource(s)

  • data.template_file

Terraform Configuration Files

template_file resource declaration:

data "template_file" "foo" {
  template = "${file("${path.module}/foo/environment.json")}"

  vars {
    name = "myname"
    # app1
    app1_foo = "${module.app1.foo}"
    app1_bar = "${module.app1.bar}"
    app1_baz = "${module.app1.baz}"

    # app2
    app2_foo = "${coalesce(module.app2.foo, module.app1.foo)}"
    app2_bar = "${coalesce(module.app2.bar, module.app1.bar)}"
    app2_baz = "${coalesce(module.app2.baz, module.app1.baz)}"
  }
}

template_file file (/path/foo/environment.json):

{
  "template_file_name": "${name}",
  "app1_foo": "${app1_foo}",
  "app1_bar": "${app1_bar}",
  "app1_baz": "${app1_baz}",
  "app2_foo": "${app2_foo}",
  "app2_bar": "${app2_bar}",
  "app2_baz": "${app2_baz}"
}

Debug Output

https://gist.github.com/jcderose/ded36006a6368346b32ad9ea77926b74

Panic Output

n/a

Expected Behavior

The template file should be populated with the interpolated values.

Actual Behavior

I get the now-infamous error message:

module.site.data.template_file.foo: Refreshing state...
Error applying plan:

1 error(s) occurred:

* data.template_file.foo: failed to render : 2:15: unknown variable accessed: name

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.

Steps to Reproduce

  1. terraform apply (terraform plan doesn't catch the error)

Important Factoids

I know this may be fixed with the upcoming 0.8 release (given the PR references I found below), but the linked issues and use cases don't quite seem to match my own.

And if nothing else, I'd love a temporary workaround.

References

@jcderose
Copy link
Author

@apparentlymart, you've chimed in on most of the threads surrounding this issue. Does a temporary workaround exist?

@mitchellh
Copy link
Contributor

I'm unable to reproduce this locally, but I have it feeling it may have to do with those modules, or it may already be fixed. @jcderose can you try with 0.7.11 with the -Xnew-apply flag?

@mitchellh mitchellh added the waiting-response An issue/pull request is waiting for a response from the community label Nov 16, 2016
@jcderose
Copy link
Author

That didn't resolve the issue. Here's my debug output, if it helps: https://gist.github.com/jcderose/d352206735196050fdd16cc852e24dfb

To clarify our use case: the app1 module is always called/run, but the app2 module may or may not be called, depending on the environment. So in cases where the app2 module is not called (and thus, the module.app2.* output variables don't exist), I want the app2_* variables (passed into the template_file resource) to default to the corresponding module.app1_* values instead.

Also to clarify, this code worked before I added the coalesce() functions to the template_file vars block. So I think the error has to do with the coalesce() functions being passed into the template.

@jcderose
Copy link
Author

Two interesting notes:

First, I tried replacing the coalesce() functions with other interpolation functions, to see if the issue is with the coalesce() function itself, but still received the error message.

Second, I re-ordered the variables in the template_file file (/path/foo/environment.json) to:

{
  "app1_foo": "${app1_foo}",
  "template_file_name": "${name}",
  "app1_bar": "${app1_bar}",
  "app1_baz": "${app1_baz}",
  "app2_foo": "${app2_foo}",
  "app2_bar": "${app2_bar}",
  "app2_baz": "${app2_baz}"
}

And now the error message complains about the app1_foo variable instead of the name variable:

module.site.data.template_file.foo: Refreshing state...
Error applying plan:

1 error(s) occurred:

* data.template_file.foo: failed to render : 2:22: unknown variable accessed: app1_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.

@jcderose
Copy link
Author

In troubleshooting this issue, I noticed the docs for the template_file resource say the vars block only supports primitives - so I assume I can't use the coalesce() function within the template_file vars block?

I've tried refactoring the coalesce() function to a separate null_data_source resource (and then passing the result to the template_file vars block) via suggestions in #4084 but haven't been able to get that to work either.

@jcderose
Copy link
Author

Okay, I resolved my issue: the output variable module.app2.foo references a resource that doesn't exist. Therefore, Terraform errors out anywhere module.app2.foo is referenced.

In my app2 module, I changed the output variable from:

output "foo" {
  value = "${aws_route53_record.db_master.fqdn}"
}

to:

output "foo" {
  value = "${join(",", aws_route53_record.db_master.*.fqdn)}"
}

This creates my intended effect of assigning an empty string to the output variable module.app2.foo if the aws_route53_record.db_master resource doesn't exist. Then in my template_file resource declaration I wrote:

data "template_file" "foo" {
  template = "${file("${path.module}/foo/environment.json")}"

  vars {
    ...
    app2_foo = "${coalesce(element(split(",", module.app2.foo), 0), module.app1.foo)}"
  }
}

This compares the sometimes-empty string of module.app1.db_fqdn against the non-empty string of module.app0.db_fqdn and assigns whichever first value is non-empty.

@ghost
Copy link

ghost commented Apr 12, 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 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug core waiting-response An issue/pull request is waiting for a response from the community
Projects
None yet
Development

No branches or pull requests

2 participants