Shorthand for optional resources #33750
Labels
duplicate
issue closed because another issue already tracks this problem
enhancement
new
new issue not yet triaged
Terraform Version
Use Cases
Clearer and more concise code that is DRY.
Attempted Solutions
Currently, we use a ternary operator, take this dummy example:
This is okay, but can become more difficult to read. Especially since now everywhere you reference this, you also need to take into account that it is parameterized.
Over time this can become more and more cumbersome and error prone, especially when refactoring.
Another option is to create reusable modules, but this can often become more and more unmaintainable for simpler cases as it adds complexity due to additional layers of indirection and is more verbose as you have to now also declare a bunch of variables and outputs to communicate with the module.
Proposal
In our codebase, we have lots of places where we want to allow enabling or disabling features based on flags and conditions.
For example, we have a bunch of infrastructure that stubs external integrations that we turn on during platform stress testing to prevent API abuse on the upstream integrations we do not depend on.
To do this, we need the ability to optionally create infrastructure based on conditions, ideally maintaining a clear and readable codebase.
One way that could possibly make this simple to implement is to allow an enabled or count attribute to take a boolean.
This doesn't help with the issue around noise for dereferencing the attributes of optional infrastructure though, unless we were to allow treating 0-or-1 arrays as either existing or not existing when declared this way.
Another way that might be nicer would be to provide a
check
-like block that could allow nesting multiple resources in bulk.By doing this, all resources within the block can be treated as if they always exist. Outside, referencing them when they do not exist would be an error.
Stuff like outputs could be wrapped in an optional block with the same condition to dereference them when they do not exist, or a try could be used. A ternary could also be used instead.
References
No response
The text was updated successfully, but these errors were encountered: