i need to validate a variable in terraform. The content of the variable should be only 0-9, a-z and A-Z. Im tried it with following code:
variable "application_name" {
type = string
default = "foo"
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("([0-9A-Za-z])", var.application_name))
error_message = "For the application_name value only a-z, A-Z and 0-9 are allowed."
}
}
It doesen't work. When i set abcd- in the variable the validation returns true.
How can i fix the regex ?
Thanks for help ;)
@vgersh99 This doesn't work for me:
variable "application_name" {
type = string
default = "foo"
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("[^[:alnum:]]", var.application_name))
error_message = "For the application_name value only a-z, A-Z and 0-9 are allowed."
}
}
Here is the error:
$ terraform validate
Error: Invalid value for variable
on main.tf line 23, in module "ecs_cluster":
23: application_name = "frdlso"
For the application_name value only a-z, A-Z and 0-9 are allowed.
This was checked by the validation rule at
.terraform/modules/ecs_cluster/variables.tf:34,5-15
question from:
https://stackoverflow.com/questions/65905029/how-to-validate-a-character-set-in-terraform-variable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…