-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Allow optional attribute values to have dynamic default values derived from objects elsewhere in the same module #30750
Comments
Thanks for the report! It will be considered as we take the next steps with this experimental feature. |
This is by far the most natural way to specify a default value for something that one explicitly marks as being optional, I hope it gets implemented :) I would like to note that even if some other way to specify default values is implemented, this second parameter to |
Hi all, we recently released the Terraform v1.3 alpha, which includes the ability to mark object type attributes as optional and set default values. I'm following up with issues that have provided feedback on the previous experiment, and wanted to invite you all to try the alpha and provide any feedback on this updated design. You can learn more/add comments here. Thank you so much in advance, and hope you like the feature! |
I was sooo excited to finally use this new feature, but to my dismay I discovered that if you have e.g. I'm not even sure what words to use to describe how impractical and unexpected this is, and foremost how much it basically nullifies the entire point of having this default parameter to To elaborate, in my example I have (or intended to have) this:
As you can see, the idea is to provide a default value for e.g. flavor and image when it's not specified explicitly for an instance, and the value for this should naturally not be hardcoded here in the object structure definition, but come from a separate variable so that it can be injected into the module. That said, of course the default parameter should be possible to define using a local variable, and if possible a data source as well. Can we please have this fixed, so that this feature is actually usable? It would let us not have to use Finally, sorry I didn't write this in the forum - I don't have an account there and don't believe in having to register accounts everywhere. Also, sorry if I should have pointed out the need for supporting variables et al earlier - it didn't even cross my mind that this default parameter would not support them :-) |
I would not expect defaults to allow dynamic values just like you can't use a dynamic value for You will need to solve by setting the default to |
Thanks, but it's not much different from what I'm doing now, which is |
Hello, It seems like this issue has effectively now transformed into a request for allowing default values for input variables to be dynamic based on other values present in the same module, and so I'm going to re-title it that way so that we don't keep getting tempted to close this as complete with the new functionality in Terraform v1.3.0. |
Related to: #31154 |
The way that Hashicorp enabled the Without |
I second @robzr.
Becomes a bit limiting and requires the local + coalesce/lookup pattern, for big objects, isn't great. Nonetheless, the feature looks mature, just missing this cherry on top 👍 |
I think this request requires essentially the same internal architecture change as #25609 and so it may be worthwhile to address both of them at the same time if it seems like "the rest is easy" once the graph shape changes are in. These are also potentially related depending on whether we intend to allow referring to anything other than other input variables from the same module: Input variable validation and type conversion are both happening in the same graph node, so essentially the capabilities between them are limited in the same way. Default values do have the additional concern that allowing them to be dynamic will make it impossible to show the exact default values in statically-generated module documentation, such as in a module registry. That'll be a tradeoff to consider when it comes to designing this. |
#31823 is somewhat related as it would at least provide a simpler way to set dynamic defaults. |
I think it is reasonable for the second argument to optional to have to be static (although it would be really nice if the default for both variables and the optional type could at least reference other variables). However, there should really be an equivalent to the defaults function that was removed. Perhaps bring the old function back, or have a merge function that doesn't overwrite with nulls, or have a function for removing null values from an object or map. |
Hi all, I want to be explicit that from the perspective of the stable Terraform language the The next step for this issue is therefore to design a suitable new language feature which aims to solve some of the same problems as the As I mentioned in an earlier comment above, it's also desirable for default values to be friendly to static documentation, and so that is another concern we'd want to keep in mind while evaluating possible new designs to address this request. We don't yet have an improved design to discuss. This issue could be an appropriate place to share and discuss ideas to solve the problem, but we typically want to wait at least one release after an experiment becomes stable to learn from real experiences using it before trying to design new features that build on top of what just shipped, and so for the moment from my perspective this request is in an information-gathering phase but not an active design and development phase. #31823 is indeed one possible candidate, although since it already has its own issue I would suggest that we discuss the details of that one over there and then, if that looks promising, we can figure out how to best to consolidate these discussions together later. Another possibility is that we first implement a facility for provider plugins to contribute additional functions to Terraform, and then the community can use provider plugins to experiment with a variety of different approaches that need not be constrained by the burden of Terraform's own backward compatibility promises. If one such answer becomes clearly prominent as a "winner" in the community then we might consider incorporating it as a builtin. (Note that it is already possible to try function ideas in providers using data sources which only perform local computation, though of course the syntax is far less convenient. But it could be a reasonable way to prototype in the meantime, without waiting for the ability for providers to contribute normal functions.) Thanks! |
I'm sorry, I probably didn't make my intent clear enough in my comment. I was fully aware that the optional feature was experimental, and am not mad that the finalized version wasn't exactly the same as the experiment. Nor do I consider it a regression, since the feature was experimental. I am, in fact, very happy that this feature has been stabilized, and think that the design works very well in a lot of cases. And I would rather have a stable optional feature now that could be improved in the future than an experimental feature that is a little more powerful. The one case where I think it could be improved is if you need a dynamic default, which was served a little better with the |
As much as I acknowledge that Deliberately stripped config (squint to see the Spring Config):
With defaults we could do
Not pretty but still achieves the goal without merge/coalesce hell and |
Use-cases
the
optional()
notation accepts a default value. The current default (null
) can be tricky to work with and can lead to unexpected behavior.Proposal
Similar to the lookup function
(lookup(map, key, default)
, the second argument in option could take adefault
value.This would elegantly solve the optional value without requiring ternaries to handle the
null
default.Current Terraform Version
References
The text was updated successfully, but these errors were encountered: