-
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
Standalone variable validations that can address multiple variables #32792
Comments
Thanks for this feature request, @DonBower! It seems like it's describing essentially the same use-case as #25609, albeit with a different proposed solution. Would you agree? If so, I'd prefer to merge this into the other issue just because otherwise it will split the ideas about how to address this and the voting on how many people have the problem over two different issues, making it harder to infer prioritization. In your particular case I think you could get an effect similar to what you described by using a locals {
flow_log_mutex_args = tomap({
eni_id = var.eni_id
subnet_id = var.subnet_id
transit_gateway_id = var.transit_gateway_id
transit_gateway_attachment_id = var.transit_gateway_attachment_id
})
flow_log_mutex_args_set = toset([
for k, v in local.flow_log_mutex_args : k
if v != null
])
}
resource "aws_flow_log" "example" {
eni_id = var.eni_id
subnet_id = var.subnet_id
transit_gateway_id = var.transit_gateway_id
transit_gateway_attachment_id = var.transit_gateway_attachment_id
# ...
lifecycle {
precondition {
condition = length(var.flow_log_mutex_args) == 1
error_message = "Must set exactly one of ${join(", ", keys(local.flow_log_mutex_args))}"
}
}
} (This example would be easier to write if there were a helper function for answering questions about how many null values there are in a map, etc, but of course that's a separate question from where to write the rule.) One notable difference between input variable validation and resource preconditions is that input variable validation is handled during the validate phase ( |
I absolutely agree, same issue, different proposed solution. Feel free to merge. |
I apparently previously forgot to merge this into #25609, so I'm belatedly doing it now. At the time I'm writing this there's an active language experiment for the other issue, so testing and feedback from that experiment would be very welcome! |
Terraform Version
Use Cases
Some input validations are based on multiple variables for example when two separate variables are mutually exclusive or inclusive. Other circumstanes might require an input for one variable if a different value has a specific value.
For a mutually exclusive example, let's look at the AWS resource aws_flow_log. In the Argument Reference, it states:
Note:
One of
eni_id
,subnet_id
,transit_gateway_id
,transit_gateway_attachment_id
, orvpc_id
must be specified.Currently, we cannot test for this condition with the current
validation
rule set.Providing a standalone
validation
resource, that is checked when all the variables are available to the called module (see apparentlymart's comment), would allow the user to program for such mutually exclusive conditions.Attempted Solutions
I didn't really try this, because I know it does not work:
Proposal
Provide for an standalone validation stanza:
References
The text was updated successfully, but these errors were encountered: