-
Notifications
You must be signed in to change notification settings - Fork 18
/
organizational_units.tf
164 lines (158 loc) · 5.24 KB
/
organizational_units.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
160
161
162
163
164
locals {
level_1_ou_arguments = [
for ou in var.organization.units :
{
name : ou.name,
key : ou.key,
}
]
level_2_ou_arguments = flatten([
for level_1_ou in var.organization.units :
[
for level_2_ou in level_1_ou.units :
{
name : level_2_ou.name,
key : level_2_ou.key,
parent : level_1_ou.key
}
]
])
level_3_ou_arguments = flatten([
for level_1_ou in var.organization.units :
[
for level_2_ou in level_1_ou.units :
[
for level_3_ou in level_2_ou.units :
{
name : level_3_ou.name,
key : level_3_ou.key,
parent : level_2_ou.key
}
]
]
])
level_4_ou_arguments = flatten([
for level_1_ou in var.organization.units :
[
for level_2_ou in level_1_ou.units :
[
for level_3_ou in level_2_ou.units :
[
for level_4_ou in level_3_ou.units :
{
name : level_4_ou.name,
key : level_4_ou.key,
parent : level_3_ou.key
}
]
]
]
])
level_5_ou_arguments = flatten([
for level_1_ou in var.organization.units :
[
for level_2_ou in level_1_ou.units :
[
for level_3_ou in level_2_ou.units :
[
for level_4_ou in level_3_ou.units :
[
for level_5_ou in level_4_ou.units :
{
name : level_5_ou.name,
key : level_5_ou.key,
parent : level_4_ou.key
}
]
]
]
]
])
}
resource "aws_organizations_organizational_unit" "level_1_ous" {
for_each = {for record in local.level_1_ou_arguments : record.key => record}
name = each.value.name
parent_id = aws_organizations_organization.organization.roots[0].id
}
resource "aws_organizations_organizational_unit" "level_2_ous" {
for_each = {for record in local.level_2_ou_arguments : record.key => record}
name = each.value.name
parent_id = aws_organizations_organizational_unit.level_1_ous[each.value.parent].id
}
resource "aws_organizations_organizational_unit" "level_3_ous" {
for_each = {for record in local.level_3_ou_arguments : record.key => record}
name = each.value.name
parent_id = aws_organizations_organizational_unit.level_2_ous[each.value.parent].id
}
resource "aws_organizations_organizational_unit" "level_4_ous" {
for_each = {for record in local.level_4_ou_arguments : record.key => record}
name = each.value.name
parent_id = aws_organizations_organizational_unit.level_3_ous[each.value.parent].id
}
resource "aws_organizations_organizational_unit" "level_5_ous" {
for_each = {for record in local.level_5_ou_arguments : record.key => record}
name = each.value.name
parent_id = aws_organizations_organizational_unit.level_4_ous[each.value.parent].id
}
locals {
level_1_ou_attributes = [
for ou in local.level_1_ou_arguments :
{
key = ou.key
id = aws_organizations_organizational_unit.level_1_ous[ou.key].id,
arn = aws_organizations_organizational_unit.level_1_ous[ou.key].arn,
parent_id = aws_organizations_organizational_unit.level_1_ous[ou.key].parent_id,
name = aws_organizations_organizational_unit.level_1_ous[ou.key].name,
}
]
level_2_ou_attributes = [
for ou in local.level_2_ou_arguments :
{
key = ou.key
id = aws_organizations_organizational_unit.level_2_ous[ou.key].id,
arn = aws_organizations_organizational_unit.level_2_ous[ou.key].arn,
parent_id = aws_organizations_organizational_unit.level_2_ous[ou.key].parent_id,
name = aws_organizations_organizational_unit.level_2_ous[ou.key].name,
}
]
level_3_ou_attributes = [
for ou in local.level_3_ou_arguments :
{
key = ou.key
id = aws_organizations_organizational_unit.level_3_ous[ou.key].id,
arn = aws_organizations_organizational_unit.level_3_ous[ou.key].arn,
parent_id = aws_organizations_organizational_unit.level_3_ous[ou.key].parent_id,
name = aws_organizations_organizational_unit.level_3_ous[ou.key].name,
}
]
level_4_ou_attributes = [
for ou in local.level_4_ou_arguments :
{
key = ou.key
id = aws_organizations_organizational_unit.level_4_ous[ou.key].id,
arn = aws_organizations_organizational_unit.level_4_ous[ou.key].arn,
parent_id = aws_organizations_organizational_unit.level_4_ous[ou.key].parent_id,
name = aws_organizations_organizational_unit.level_4_ous[ou.key].name,
}
]
level_5_ou_attributes = [
for ou in local.level_5_ou_arguments :
{
key = ou.key
id = aws_organizations_organizational_unit.level_5_ous[ou.key].id,
arn = aws_organizations_organizational_unit.level_5_ous[ou.key].arn,
parent_id = aws_organizations_organizational_unit.level_5_ous[ou.key].parent_id,
name = aws_organizations_organizational_unit.level_5_ous[ou.key].name,
}
]
all_ou_attributes = {
for ou in concat(
local.level_1_ou_attributes,
local.level_2_ou_attributes,
local.level_3_ou_attributes,
local.level_4_ou_attributes,
local.level_5_ou_attributes
) :
ou.key => ou
}
}