Skip to content
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

Allow user to specify mutual exclusive variables #21860

Closed
pasalkarsachin1 opened this issue Jun 24, 2019 · 2 comments
Closed

Allow user to specify mutual exclusive variables #21860

pasalkarsachin1 opened this issue Jun 24, 2019 · 2 comments

Comments

@pasalkarsachin1
Copy link

Current Terraform Version

Terraform v0.11.11
+ provider.aws v2.16.0
+ provider.local v1.2.2
+ provider.null v2.1.2

Use-cases

We have use case in AWS lambda where we either want our user to provide the artifactory path for lambda code or his own code. There are 2 variables used as download artifactory needs local-exec mechanism where are local file needs archive zip way.

Right now, user can provide both options. We want options in variable to mar another variable as mutually exclusive

Attempted Solutions

N/A

Proposal

variable "local_file_content" { description = "If you have file present on owned tf, Mutually exclusive with download_location" type = "string" default = "" exclusive= ["download_location"] }

References

NA

@apparentlymart
Copy link
Contributor

Thanks for this feature request, and sorry for the long delay in responding to it.

#34955 has recently been merged for inclusion in the forthcoming Terraform v1.9 release, and will allow input variable validation rules to refer to other symbols (including other input variables) declared in the same module.

That means it'll now be possible to write a validation rule that checks to ensure that at least one of two variables is null:

variable "a" {
  type    = string
  default = null

  # Optional: if you also want to require that _at least one_ be set.
  validation {
    condition = (var.a != null || var.b != null)
    error_message = "Must set either 'a' or 'b'."
  }
}

variable "b" {
  type    = string
  default = null

  validation {
    condition = (var.a == null || var.b == null)
    error_message = "Cannot set both 'a' and 'b'."
  }
}

This does require making a seemingly-arbitrary decision about which of the two variables to hook the validation rule onto, which will then dictate which one Terraform indicates as being the problem in its error message. In this example I chose to report that at least one is required using a and that only one can be set using b, which is what I'd choose to do if I expected that a would be the more common choice than b, but of course this is a contrived example.

Thanks again!

Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants