internal/terraform: Accept only ephemeral values in ephemeral outputs #35672
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This enforces a more explicit configuration where ephemeral values have to be marked as such unless they are already ephemeral before being used in ephemeral context.
For example, the original prototype allowed this:
This PR disallows that.
A new
ephemeral()
function may introduce an escape hatch though, to account for cases where ephemerality cannot be directly declared, such as in local values.The downside is that it also enables some use cases which we probably shouldn't be enabling, for example:
Ideally, variables should just be made ephemeral before referencing them. It is possible however that some users will want to reuse the same value in ephemeral and non-ephemeral context. I'm not sure how common that may be and how reasonable such a use case actually is, given the risk it introduces.
Problem
Mixing ephemeral and non-ephemeral values is more or less a problem we can solve by tracking ephemerality alongside the values.
However, the problem with turning something non-ephemeral into ephemeral is that it's difficult to tell if the value was already used in a context where it will be (or already was) persisted.
For example:
I'm not sure if we can/should be detecting such cases. I do believe however that forcing the use of the
ephemeral()
function will at least make people think that they're doing something unusual. This is in contrast to implying ephemerality just by reference.Possible Limitations
Given the above context, we could restrict the use of the
ephemeral()
function to just local values and constants, and/or have constants imply ephemerality when mixed with ephemeral values in the same expression.We could also implement some warning diagnostics for all the known/suspected bad use cases.
We could also hold on introducing the escape hatch initially until users come up with use cases.
I'm open to other ideas!
Tests
Tests can be added as part of #35647