github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/matchkeys.mdx (about) 1 --- 2 page_title: matchkeys - Functions - Configuration Language 3 description: |- 4 The matchkeys function takes a subset of elements from one list by matching 5 corresponding indexes in another list. 6 --- 7 8 # `matchkeys` Function 9 10 `matchkeys` constructs a new list by taking a subset of elements from one 11 list whose indexes match the corresponding indexes of values in another 12 list. 13 14 ```hcl 15 matchkeys(valueslist, keyslist, searchset) 16 ``` 17 18 `matchkeys` identifies the indexes in `keyslist` that are equal to elements of 19 `searchset`, and then constructs a new list by taking those same indexes from 20 `valueslist`. Both `valueslist` and `keyslist` must be the same length. 21 22 The ordering of the values in `valueslist` is preserved in the result. 23 24 ## Examples 25 26 ``` 27 > matchkeys(["i-123", "i-abc", "i-def"], ["us-west", "us-east", "us-east"], ["us-east"]) 28 [ 29 "i-abc", 30 "i-def", 31 ] 32 ``` 33 34 If the result ordering is not significant, you can achieve a similar result 35 using a `for` expression with a map: 36 37 ``` 38 > [for i, z in {"i-123"="us-west","i-abc"="us-east","i-def"="us-east"}: i if z == "us-east"] 39 [ 40 "i-def", 41 "i-abc", 42 ] 43 ``` 44 45 If the keys and values of interest are attributes of objects in a list of 46 objects then you can also achieve a similar result using a `for` expression 47 with that list: 48 49 ``` 50 > [for x in [{id="i-123",zone="us-west"},{id="i-abc",zone="us-east"}]: x.id if x.zone == "us-east"] 51 [ 52 "i-abc", 53 ] 54 ``` 55 56 For example, the previous form can be used with the list of resource instances 57 produced by a `resource` block with the `count` meta-attribute set, to filter 58 the instances by matching one of the resource attributes: 59 60 ``` 61 > [for x in aws_instance.example: x.id if x.availability_zone == "us-east-1a"] 62 [ 63 "i-abc123", 64 "i-def456", 65 ] 66 ``` 67 68 Since the signature of `matchkeys` is complicated and not immediately clear to 69 the reader when used in configuration, prefer to use `for` expressions where 70 possible to maximize readability.