github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/can.mdx (about)

     1  ---
     2  page_title: can - Functions - Configuration Language
     3  description: |-
     4    The can function tries to evaluate an expression given as an argument and
     5    indicates whether the evaluation succeeded.
     6  ---
     7  
     8  # `can` Function
     9  
    10  `can` evaluates the given expression and returns a boolean value indicating
    11  whether the expression produced a result without any errors.
    12  
    13  This is a special function that is able to catch errors produced when evaluating
    14  its argument. For most situations where you could use `can` it's better to use
    15  [`try`](/language/functions/try) instead, because it allows for more concise definition of
    16  fallback values for failing expressions.
    17  
    18  The primary purpose of `can` is to turn an error condition into a boolean
    19  validation result when writing
    20  [custom variable validation rules](/language/values/variables#custom-validation-rules).
    21  For example:
    22  
    23  ```
    24  variable "timestamp" {
    25    type        = string
    26  
    27    validation {
    28      # formatdate fails if the second argument is not a valid timestamp
    29      condition     = can(formatdate("", var.timestamp))
    30      error_message = "The timestamp argument requires a valid RFC 3339 timestamp."
    31    }
    32  }
    33  ```
    34  
    35  The `can` function can only catch and handle _dynamic_ errors resulting from
    36  access to data that isn't known until runtime. It will not catch errors
    37  relating to expressions that can be proven to be invalid for any input, such
    38  as a malformed resource reference.
    39  
    40  ~> **Warning:** The `can` function is intended only for simple tests in
    41  variable validation rules. Although it can technically accept any sort of
    42  expression and be used elsewhere in the configuration, we recommend against
    43  using it in other contexts. For error handling elsewhere in the configuration,
    44  prefer to use [`try`](/language/functions/try).
    45  
    46  ## Examples
    47  
    48  ```
    49  > local.foo
    50  {
    51    "bar" = "baz"
    52  }
    53  > can(local.foo.bar)
    54  true
    55  > can(local.foo.boop)
    56  false
    57  ```
    58  
    59  The `can` function will _not_ catch errors relating to constructs that are
    60  provably invalid even before dynamic expression evaluation, such as a malformed
    61  reference or a reference to a top-level object that has not been declared:
    62  
    63  ```
    64  > can(local.nonexist)
    65  
    66  Error: Reference to undeclared local value
    67  
    68  A local value with the name "nonexist" has not been declared.
    69  ```
    70  
    71  ## Related Functions
    72  
    73  * [`try`](/language/functions/try), which tries evaluating a sequence of expressions and
    74    returns the result of the first one that succeeds.