Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - How to restrict an input variable to a list of possible choices

Tags:

terraform

I have a variable that the user will input during run time. Lets say the variable name is region. However, I want the execution to be only successful if the user picks a value from one of the values defined in a list/ choices.

how can I restrict it so the user's selection has to match values that are considered acceptable in the variable definition?

like image 876
Sanmay Mishra Avatar asked Jul 24 '19 19:07

Sanmay Mishra


People also ask

How do you declare a list variable in Terraform?

Terraform variables can be defined within the infrastructure plan but are recommended to be stored in their own variables file. All files in your Terraform directory using the . tf file format will be automatically loaded during operations. Create a variables file, for example, variables.tf and open the file for edit.

How do you assign a value to a variable in Terraform?

To do so, simply set the environment variable in the format TF_VAR_<variable name> . The variable name part of the format is the same as the variables declared in the variables.tf file. For example, to set the ami variable run the below command to set its corresponding value.

How do you pass parameters in Terraform?

Using the -var Command-line Flag The -var flag is used to pass values for Input Variables to Terraform commands. This flag can be used with both of the Terraform plan and apply commands. The argument passed to the -var flag is a string surrounded by either single or double quotes.

What are three types of variables that exist within Terraform?

Lists, maps, and objects are the three most common complex variable types.

What types of input variables does terraform support?

Terraform supports several variable types for using with Input Variables. The most common is likely the string type but Terraform also supports a few more type options to choose from. Terraform supports three primitive types for Input Variables.

Why do I keep getting variable validation issues with terraform?

If you have worked before with Terraform by provisioning a cloud infrastructure and/or writing Terraform modules, you must have stumbled upon some variable validation issues. This could happen alot when you are trying to assign a value to a variable which is out of the valid variable values scope, or it’s the wrong variable type.

What is the difference between map and object values in TerraForm?

Important: In Terraform 0.12 and later, variables with map and object values behave the same way as other variables: the last value found overrides the previous values. This is a change from previous versions of Terraform, which would merge map values instead of overriding them.

What is the use of -Var-file in TerraForm?

-var-file flag enables multiple input variable values to be passed in by referencing a file that contains the values. The -var flag is used to pass values for Input Variables to Terraform commands. This flag can be used with both of the Terraform plan and apply commands.


1 Answers

Stumbled across this question.

Since v0.13.0 input validation has been possible directly via the input variables. Thus you can actually achieve this with a snippet such as below.

variable "test_variable" {
  type        = string
  description = "some test value"

  validation {
    condition     = contains(["item1", "item2", "item3"], var.test_variable)
    error_message = "Valid values for var: test_variable are (item1, item2, item3)"
  } 
}

Read more here - https://www.hashicorp.com/blog/custom-variable-validation-in-terraform-0-13

like image 111
David Avatar answered Oct 05 '22 06:10

David