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