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

Feature request: shorthand for setting variables #15784

Closed
boompig opened this issue Aug 10, 2017 · 5 comments
Closed

Feature request: shorthand for setting variables #15784

boompig opened this issue Aug 10, 2017 · 5 comments

Comments

@boompig
Copy link
Contributor

boompig commented Aug 10, 2017

Is it possible to have a shorthand for variables? Something like

variable "varname" = { "value" }

Typing

variable "varname" {
    default = "value"
}

is not only cumbersome, it means less code can fit on screen. This results in unnecessary long files.

@apparentlymart
Copy link
Contributor

Hi @boompig! Thanks for this suggestion.

The particular syntax you proposed here can't really fit within the constraints of the configuration format right now, but that doesn't mean we could try to find a different concise way to declare variables.

I'm curious though: do you find that your modules generally have a lot of variables? If so, I'm curious how you would characterize the usage of variables:

  • Are there a lot because currently Terraform makes it hard to pass complex structures as a single value, and so you're unpacking a bunch of resource attributes into separate variables?
  • Are you using variables with defaults as a way to factor out commonly-repeated strings, expecting that they won't be overridden by the calling module?
  • Something else?

The reason for this question is that we've been looking at some other language improvements that may help with the above in some different ways. With #15449 (which we should be able to land soon, after we get through some 0.10.0 fixes) we'll have a different way to deal with the second problem above. For the first, we're still in the early planning stage but we're planning to make it possible to pass complex object structures as variables, which I expect would result in certain modules requiring many fewer variables than before.

Of course neither of those things directly address the verbosity of individual variable blocks, but will ideally cause there to be fewer variable blocks, and thus perhaps make the verbosity of each not feel as heavy as it does today.

@boompig
Copy link
Contributor Author

boompig commented Aug 10, 2017

I find that factoring out those values which are likely to change in variables at the top of files is helpful. In practice this ends up being file paths mostly.

@in4mer
Copy link

in4mer commented Aug 14, 2017

Along these lines, I'd find it way, way better to be able to alias certain strings. At this point I'm quite tired of typing null_data_source. If we could come up with a way to alias strings to objects, that'd be nice. I'd much rather type .nds.

@apparentlymart
Copy link
Contributor

Hi all! Sorry for the long silence here.

In Terraform 0.10.3 we added the concept of local values as a way to factor out a particular expression and give it a name, separately from the concept of input variables.

With that feature in place, we now recommend using variable blocks only for values that are to be provided by the caller, and use the locals block for the situation described here where an expression is extracted so that it can be easily reused and changed in one place.

Local values have a more concise syntax because they don't have all of the other requirements that input variables need to deal with:

locals {
  template_file = "${path.module}/example.tmpl"
}

While this change hasn't changed the variable block syntax at all, it does tend to reduce the number of variable blocks in a configuration since there's no longer a need to declare input variables just to reuse their values in multiple locations. Therefore I'm going to close this issue, since the current syntax complexity is necessary for the intended use-case of variable blocks (defining what is expected from the caller) and there's now a more concise way to achieve the other emergent use-case of factoring out values.

Thanks for sharing this use-case, and sorry again I didn't find this issue while we were preparing the 0.10.3 release.

@ghost
Copy link

ghost commented Apr 4, 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 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants