As of terraform 1.9 variables can now refer to other variables https://www.hashicorp.com/en/blog/terraform-1-9-enhances-input-variable-validations hence the code posted in the first message of this thread would work.