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