How do `try()` and `can()` help with validation and optional values?
IntermediateConceptVariables, Locals, Outputs and Types
Answer
try(expr, fallback) returns the first expression that evaluates without error; can(expr) returns true or false. Combine with regex for validation.
condition = can(regex("^[a-z0-9-]{3,20}$", var.name))
value = try(var.config.timeout, 30)
More on Variables, Locals, Outputs and Types
How do you declare an input variable?BeginnerIn what order does Terraform load variable values?BeginnerHow do you define an output and what is it for?BeginnerWhat is the difference between locals and variables?BeginnerWhat are the primitive types in Terraform?BeginnerHow do list, set and map differ?Intermediate