This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
forked from infrablocks/terraform-aws-organization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizational_units.tf
78 lines (74 loc) · 2.68 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
locals {
level_1_ou_arguments = [
for ou in local.organizational_units : ou
]
level_2_ou_arguments = flatten([
for level_1_ou in local.organizational_units :
[for level_2_ou in level_1_ou.children :
{
name: level_2_ou.name,
parent: index(local.organizational_units, level_1_ou)
}
]
])
level_3_ou_arguments = flatten([
for level_1_ou in local.organizational_units :
[for level_2_ou in level_1_ou.children :
[for level_3_ou in level_2_ou.children :
{
name: level_3_ou.name,
parent: index(level_1_ou.children, level_2_ou)
}
]
]
])
}
resource "aws_organizations_organizational_unit" "level_1_ous" {
for_each = { for record in local.level_1_ou_arguments : record.name => 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.name => record }
name = each.value.name
parent_id = aws_organizations_organizational_unit.level_1_ous[local.level_1_ou_arguments[each.value.parent].name].id
}
resource "aws_organizations_organizational_unit" "level_3_ous" {
for_each = { for record in local.level_3_ou_arguments : record.name => record }
name = each.value.name
parent_id = aws_organizations_organizational_unit.level_2_ous[local.level_2_ou_arguments[each.value.parent].name].id
}
locals {
level_1_ou_attributes = [
for ou in local.level_1_ou_arguments :
{
id = aws_organizations_organizational_unit.level_1_ous[ou.name].id,
arn = aws_organizations_organizational_unit.level_1_ous[ou.name].arn,
parent_id = aws_organizations_organization.organization.roots[0].id,
name = ou.name,
}
]
level_2_ou_attributes = [
for ou in local.level_2_ou_arguments :
{
id = aws_organizations_organizational_unit.level_2_ous[ou.name].id,
arn = aws_organizations_organizational_unit.level_2_ous[ou.name].arn,
parent_id = aws_organizations_organizational_unit.level_1_ous[local.level_1_ou_arguments[ou.parent].name].id,
name = ou.name
}
]
level_3_ou_attributes = [
for ou in local.level_3_ou_arguments :
{
id = aws_organizations_organizational_unit.level_3_ous[ou.name].id,
arn = aws_organizations_organizational_unit.level_3_ous[ou.name].arn,
parent_id = aws_organizations_organizational_unit.level_2_ous[local.level_2_ou_arguments[ou.parent].name].id,
name = ou.name
}
]
all_ou_attributes = concat(
local.level_1_ou_attributes,
local.level_2_ou_attributes,
local.level_3_ou_attributes
)
}