hashicorp/terraform

Documentation should clarify exactly where interpolations are allowed

pll opened this issue · 5 comments

pll commented

The docs do a great job of explaining how to use interpolations, but more clarification is needed on where they can be used. To whit:

Q: Can they be used in resource names?
i.e. resource "aws_instance" "${var.env}-${var.systype}" { ... }
A: Apparently not, since you end up with a resource of that literal string, not an interpolated value.

Q: Can they be used in a variable block?
i.e.: variable "foo" { default = "${var.bar}-${var.baz}" }
A: Apparently not, since it results in an error: * Variable 'foo': cannot contain interpolations

In short, the docs should mention where and where NOT to use interpolations.
(also, it would be REALLY cool to support those uses mentioned above, especially the latter, deriving new variables from existing ones, and/or performing functions on other variables (split, join, element, etc) to derive new ones).

Yep these would be good improvements to the docs. Want to take a stab at a pull request?

The source lives right in the repo.

I'd recommend against modeling your configs in such a way that makes you want dynamic Terraform resource names, and instead point you to modules. Modules give you a dynamic namespace for groups of resources and make the individual resource names less important. In fact, I commonly find myself naming individual resources "mod" to indicate they're just module-local. My module names, module variables, and module outputs are what I spend time naming to tell the story of the semantic wiring in my configs.

For variables, though, the use case definitely makes sense - it's just a matter of overcoming some implementation hurdles. You can track that feature request over in #2727.

pll commented

Sorry for the delayed response. I was happily on vacation and completely off-grid without so much as a cell-phone signal :)

I'll take a stab at pull request for the doc issues. As for dynamic resource names, I've come to a similar conclusion that our models are best to avoid that as well. I look forward to more dynamic variable interpolation though ;)

I'm going to go ahead and close this issue, but we'd still welcome the improved docs PR!

pll commented

Sorry, haven't had a chance to get to this.

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.