Interview Prep/Terraform/Variables, Locals, Outputs and Types

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)
All Terraform questions Roadmap: Terraform

More on Variables, Locals, Outputs and Types