-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocals.tf
159 lines (129 loc) · 4.51 KB
/
locals.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
locals {
service_endpoints = {
ssm = [
"AccountA",
"AccountB",
],
ec2 = [
"AccountA",
"AccountB",
"AccountC"
],
logs = [
"AccountA",
"AccountB"
],
ec2messages = [
"AccountA",
"AccountC"
],
}
# Using nested for loops, create a list of objects pairing each service endpoint and account in its list
# The first for loop gives us the key (endpoint) and the nested for loop gives us the values within the list (accounts)
endpointaccounts_tuple = flatten([
for endpoint, accounts in local.service_endpoints : [
for account in accounts : {
endpoint = endpoint
account = account
}
]
])
# Convert the list tuple from local.endpointaccounts_tuple into a map of maps that can be used in a for_each loop (the below could also be used directly in the for_each )
# for_each = {
# for item in local.endpointaccounts_tuple :
# "${item.endpoint}_${item.account}" => item
# }
#
inputs_endpoint_map = {
for item in local.endpointaccounts_tuple :
"${item.endpoint}_${item.account}" => item
}
# Create a list of tuples with maps pairing each service endpoint and account in its list with a key made up of endpoint-account
# The first for loop gives us the key (endpoint) and the nested for loop gives us the values within the list (accounts)
endpointaccounts_tuple_maps = flatten([
for endpoint, accounts in local.service_endpoints : [
for account in accounts : {
"${endpoint}_${account}" = {
endpoint = endpoint
account = account
}
}
]
])
# Convert the list tuple from local.endpointaccounts_tuple_maps into a map of maps that can be used in a for_each loop (the below could also be used directly in the for_each )
# for_each = {
# for item in local.endpointaccounts_tuple :
# keys(item)[0] => values(item)[0]
# }
#
function_endpoint_map = { for item in local.endpointaccounts_tuple_maps:
keys(item)[0] => values(item)[0]
}
# Create a list of distinct (unique) accounts across all maps in local.service_endpoints
accounts = distinct(flatten([
for endpoint, accounts in local.service_endpoints : accounts
]))
# Search through the list and only return the pairs that contain AccountA
regex_search = [for pair in local.endpointaccounts_tuple : pair if can(regex("AccountA", pair.account))]
service_endpoint_list = [
"ssm",
"logs",
"ec2",
"ec2messages"
]
account_list = [
"AccountA",
"AccountB",
"AccountC",
"AccountD"
]
# Using built in Terraform function "setproduct" combine the 2 lists into all possible variations of endpoint -> account
endpointaccounts_product = setproduct(local.service_endpoint_list, local.account_list)
# Using built in Terraform function "zipmap" combine the 2 lists at their index 0->0, 1->1, 2->2 etc.
endpointaccounts_map = zipmap(local.service_endpoint_list, local.account_list)
# Using built in Terraform function "lookup" search a map for a string "ec2" and if not found tell us
endpointaccounts_lookup = lookup(local.endpointaccounts_map, "ec2", "not found")
# Using built in Terraform function "keys" extract all keys from the map
endpointaccounts_keys = keys(local.endpointaccounts_map)
# Using built in Terraform function "contains" this will return false as contains cannot search lists of lists
contains_search_false = contains(local.endpointaccounts_product, "AccountA")
# Using built in Terraform function "contains" this will return true as the list of lists has been flattened
contains_search_true = contains(flatten(local.endpointaccounts_product), "AccountA")
}
# The outputs below show how the data is structured or the returned values for each of the locals above
output "list_tuple" {
value = local.endpointaccounts_tuple
}
output "list_tuple_map" {
value = local.endpointaccounts_tuple_maps
}
output "maps_of_maps" {
value = local.function_endpoint_map
}
output "another_maps_of_maps" {
value = local.inputs_endpoint_map
}
output "unique_account_list" {
value = local.accounts
}
output "regex_found" {
value = local.regex_search
}
output "product_list" {
value = local.endpointaccounts_product
}
output "zipmap" {
value = local.endpointaccounts_map
}
output "lookups" {
value = local.endpointaccounts_lookup
}
output "keys" {
value = local.endpointaccounts_keys
}
output "contains_false" {
value = local.contains_search_false
}
output "contains_true" {
value = local.contains_search_true
}