Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a specific pattern for a variable value

I am new to Terraform and I was wondering how to filter tags with it, I mean from the name tag "email" we must require the user to enter a specific format "[email protected]" in the value tag input.

like image 440
hiskice Avatar asked Jun 16 '26 18:06

hiskice


1 Answers

I think one of the recent experimental features of terraform should help you with that:

variable "email" {
  type = string

  validation {
    condition = can(regex("^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", var.email))
    error_message = "ERROR: Not a valid email"
  }
}

https://www.terraform.io/docs/configuration/functions/can.html

this feature requires this:

terraform {
  experiments = [variable_validation]
}

https://www.terraform.io/docs/configuration/variables.html#custom-validation-rules

like image 82
4c74356b41 Avatar answered Jun 21 '26 07:06

4c74356b41