github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/website/docs/language/functions/try.mdx (about) 1 --- 2 page_title: try - Functions - Configuration Language 3 description: |- 4 The try function tries to evaluate a sequence of expressions given as 5 arguments and returns the result of the first one that does not produce 6 any errors. 7 --- 8 9 # `try` Function 10 11 `try` evaluates all of its argument expressions in turn and returns the result 12 of the first one that does not produce any errors. 13 14 This is a special function that is able to catch errors produced when evaluating 15 its arguments, which is particularly useful when working with complex data 16 structures whose shape is not well-known at implementation time. 17 18 For example, if some data is retrieved from an external system in JSON or YAML 19 format and then decoded, the result may have attributes that are not guaranteed 20 to be set. We can use `try` to produce a normalized data structure which has 21 a predictable type that can therefore be used more conveniently elsewhere in 22 the configuration: 23 24 ```hcl 25 locals { 26 raw_value = yamldecode(file("${path.module}/example.yaml")) 27 normalized_value = { 28 name = tostring(try(local.raw_value.name, null)) 29 groups = try(local.raw_value.groups, []) 30 } 31 } 32 ``` 33 34 With the above local value expressions, configuration elsewhere in the module 35 can refer to `local.normalized_value` attributes without the need to repeatedly 36 check for and handle absent attributes that would otherwise produce errors. 37 38 We can also use `try` to deal with situations where a value might be provided 39 in two different forms, allowing us to normalize to the most general form: 40 41 ```hcl 42 variable "example" { 43 type = any 44 } 45 46 locals { 47 example = try( 48 [tostring(var.example)], 49 tolist(var.example), 50 ) 51 } 52 ``` 53 54 The above permits `var.example` to be either a list or a single string. If it's 55 a single string then it'll be normalized to a single-element list containing 56 that string, again allowing expressions elsewhere in the configuration to just 57 assume that `local.example` is always a list. 58 59 This second example contains two expressions that can both potentially fail. 60 For example, if `var.example` were set to `{}` then it could be converted to 61 neither a string nor a list. If `try` exhausts all of the given expressions 62 without any succeeding, it will return an error describing all of the problems 63 it encountered. 64 65 We strongly suggest using `try` only in special local values whose expressions 66 perform normalization, so that the error handling is confined to a single 67 location in the module and the rest of the module can just use straightforward 68 references to the normalized structure and thus be more readable for future 69 maintainers. 70 71 The `try` function can only catch and handle _dynamic_ errors resulting from 72 access to data that isn't known until runtime. It will not catch errors 73 relating to expressions that can be proven to be invalid for any input, such 74 as a malformed resource reference. 75 76 ~> **Warning:** The `try` function is intended only for concise testing of the 77 presence of and types of object attributes. Although it can technically accept 78 any sort of expression, we recommend using it only with simple attribute 79 references and type conversion functions as shown in the examples above. 80 Overuse of `try` to suppress errors will lead to a configuration that is hard 81 to understand and maintain. 82 83 ## Examples 84 85 ``` 86 > local.foo 87 { 88 "bar" = "baz" 89 } 90 > try(local.foo.bar, "fallback") 91 baz 92 > try(local.foo.boop, "fallback") 93 fallback 94 ``` 95 96 The `try` function will _not_ catch errors relating to constructs that are 97 provably invalid even before dynamic expression evaluation, such as a malformed 98 reference or a reference to a top-level object that has not been declared: 99 100 ``` 101 > try(local.nonexist, "fallback") 102 103 Error: Reference to undeclared local value 104 105 A local value with the name "nonexist" has not been declared. 106 ``` 107 108 ## Related Functions 109 110 * [`can`](/language/functions/can), which tries evaluating an expression and returns a 111 boolean value indicating whether it succeeded.