-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Description
Current Terraform Version
Terraform v0.13.0-beta3
Use-cases
I want to be able to have conditional validation on variables - the condition being the value of other variables.
Currently, I'm trying to implement a load balancer (resource "aws_lb") in AWS - the load balancer resource can either be of type "application" or "network". Depending on the load balancer type, there are a number of required and optional variables that you can pass in. Terraform throws an error if you fail to pass a required variable, but, if you pass in an optional variable to the wrong load balancer type, there isn't much warning.
For the sake of example, an application load balancer can optionally take a list of security groups, but a network load balancer can't. If a developer passes in a list of security groups to an NLB, they may have specified the wrong value for load_balancer_type, or they may be passing variables into the wrong load balancer resource - in either case, I'd like to give a warning to the developer to save them having to go debugging.
Obviously, this isn't a request that's strictly for AWS load balancers - the pattern of "value X is required if value Y is Z" seems to be fairly common across Terraform plugins.
Attempted Solutions
I tried adding a validation block here:
variable "security_groups" {
description = "A list of security groups to attach to the load balancer. Only works with ALBs"
type = list(string)
validation {
condition = var.lb_type == "application"
error_message = "Security groups can only be used by Application Load Balancers."
}
}This is the error that I received:
Error: Invalid variable validation condition
on variables.tf line 36, in variable "security_groups":
36: condition = var.lb_type == "application"
The condition for variable "security_groups" must refer to var.security_groups
in order to test incoming values.Proposal
Just a few prefaces to my proposals:
- I haven't created a Terraform plugin before.
- I'm a huge fan of Terraform, and I apologise if it looks like I'm crapping over the addition of this feature.
- As @alexjurkiewicz mentioned in Variable validation should allow checking other variables #24374, implementing such a feature could allow for cyclic dependencies between variables - although, I think that it's at the developer's own risk - like how package systems in programming languages facilitate cyclic dependencies (but we still all recognise the benefit of having them).
- I think that the (bigger) potential issue is causing a significant growth in the dependency graph by creating a complex set of conditional validation rules - @apparentlymart even mentioned this in the comments on the validation block decoder.
- I'm a huge fan of Terraform, and I apologise if it looks like I'm crapping over the addition of this feature.
I think there are two potential approaches to this:
- Remove or alter the statement that checks that validation only refers to itself.
- Rewrite schema.Schema so that it isn't a choice between "optional" and "required" being true - this would be a major release change because it would fundamentally alter the workings of plugins.