Your problem is still is a valid situation that many Terraform users might face. Therefore, I will provide a more up to date answer.
In order to restrict an input variable to a list of possible choices you can now do the following:
variable "create_mode" {
type = string
description = "Some description"
default = "Default"
validation {
condition = contains(["Default", "PointInTimeRestore", "Replica"], var.create_mode)
error_message = "Valid values for create_mode are (Default, PointInTimeRestore, Replica)"
}
}
In the example above I have the variable create_mode. The valid options are: "Default", "PointInTimeRestore", "Replica". So in order to restrict the input I'm adding the validation block which contains the condition and the error message to display in case somebody tries to use an invalid value for the variable.
Now if I try to initiatiate this variable to let's say create_mode = "Random" I will get the following error when I do terraform plan or terraform validate.
daniel:~$tfplan
╷
│ Error: Invalid value for variable
│
│ on main.tf line 91, in module "pg_fs_database":
│ 91: create_mode = "Random"
│ ├────────────────
│ │ var.create_mode is "Random"
│
│ Valid values for create_mode are (Default, PointInTimeRestore, Replica)
│
│ This was checked by the validation rule at ../postgresql-flexible-server/variables.tf:56,3-13.
Have a look here for more details.