forked from blackbird-cloud/terraform-aws-organization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
271 lines (227 loc) · 10.9 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
resource "aws_organizations_organization" "default" {
aws_service_access_principals = var.aws_service_access_principals
enabled_policy_types = var.enabled_policy_types
feature_set = var.feature_set
}
resource "aws_organizations_organizational_unit" "level_one" {
for_each = {
for ou in var.organizational_units : ou.name => ou
}
name = each.value.name
parent_id = aws_organizations_organization.default.roots[0].id
tags = merge(var.tags, each.value.tags)
}
locals {
level_one_accounts = flatten([
for level_one_ou in var.organizational_units : [for account in try(level_one_ou.accounts, []) : merge(account, { parent_id : aws_organizations_organizational_unit.level_one[level_one_ou.name].id })]
])
level_two_accounts = flatten([
for level_two_ou in local.level_two_ous : [for account in try(level_two_ou.accounts, []) : merge(account, { parent_id : aws_organizations_organizational_unit.level_two[level_two_ou.key].id })]
])
level_three_accounts = flatten([
for level_three_ou in local.level_three_ous : [for account in try(level_three_ou.accounts, []) : merge(account, { parent_id : level_three_ou.parent_id })]
])
accounts = concat(local.level_one_accounts, local.level_two_accounts, local.level_three_accounts)
level_two_ous = flatten([
for level_one_ou in var.organizational_units : [
for level_two_ou in try(level_one_ou.organizational_units, []) : {
ou : level_two_ou
key : "${level_one_ou.name}-${level_two_ou.name}"
parent_id : aws_organizations_organizational_unit.level_one[level_one_ou.name].id
}
]
])
level_three_ous = flatten([
for level_one_ou in var.organizational_units : [
for level_two_ou in try(level_one_ou.organizational_units, []) : [
for level_three_ou in try(level_two_ou.organizational_units, []) : {
ou : level_three_ou
key : "${level_one_ou.name}-${level_two_ou.name}-${level_three_ou.name}"
parent_id : aws_organizations_organizational_unit.level_two["${level_one_ou.name}-${level_two_ou.name}"].id
}
]
]
])
ous = merge(aws_organizations_organizational_unit.level_one, aws_organizations_organizational_unit.level_two, aws_organizations_organizational_unit.level_three)
delegated_administrators = flatten([
for account in local.accounts : [
for service in account.delegated_administrator_services : { service_principal : service, account_name : account.name }
]
])
delegated_service_principals = flatten([
for account in local.accounts : [
for service in account.delegated_administrator_services : service
]
])
organizations_policy_attachments = flatten([
for name, policy in var.organizations_policies : [
for ou in policy.ous : {
ou = ou
policy_name = name
# target_id : ou == "root" ? aws_organizations_organization.default.roots[0].id : local.ous[ou].id
# policy_id : aws_organizations_policy.default[name].id
}
]
])
}
resource "aws_organizations_organizational_unit" "level_two" {
for_each = {
for ou in local.level_two_ous : ou.key => ou
}
name = each.value.ou.name
parent_id = each.value.parent_id
tags = merge(var.tags, each.value.ou.tags)
}
resource "aws_organizations_organizational_unit" "level_three" {
for_each = {
for ou in local.level_three_ous : ou.key => ou
}
name = each.value.ou.name
parent_id = each.value.parent_id
tags = merge(var.tags, each.value.ou.tags)
}
resource "aws_organizations_account" "default" {
for_each = {
for account in local.accounts : account.name => account
}
name = each.value.name
email = each.value.email
close_on_deletion = try(each.value.close_on_deletion, null)
iam_user_access_to_billing = try(each.value.iam_user_access_to_billing, null)
parent_id = each.value.parent_id
tags = merge(var.tags, try(each.value.tags, {}))
depends_on = [aws_organizations_organization.default]
}
resource "aws_organizations_delegated_administrator" "default" {
for_each = {
for account in local.delegated_administrators : account.service_principal => account
}
account_id = aws_organizations_account.default[each.value.account_name].id
service_principal = each.value.service_principal
}
resource "aws_organizations_policy" "default" {
for_each = var.organizations_policies
name = each.key
content = each.value.content
description = try(each.value.description, null)
skip_destroy = try(each.value.skip_destroy, null)
type = try(each.value.type, "SERVICE_CONTROL_POLICY")
tags = var.tags
depends_on = [aws_organizations_organization.default]
}
resource "aws_organizations_policy_attachment" "default" {
for_each = {
for attachment in local.organizations_policy_attachments : "${attachment.policy_name}-${attachment.ou}" => attachment
}
policy_id = aws_organizations_policy.default[each.value.policy_name].id
target_id = each.value.ou == "root" ? aws_organizations_organization.default.roots[0].id : local.ous[each.value.ou].id
depends_on = [aws_organizations_organization.default, aws_organizations_policy.default]
}
### Account Management
resource "aws_account_primary_contact" "root" {
address_line_1 = try(var.primary_contact.address_line_1, null)
address_line_2 = try(var.primary_contact.address_line_2, null)
address_line_3 = try(var.primary_contact.address_line_3, null)
city = try(var.primary_contact.city, null)
company_name = try(var.primary_contact.company_name, null)
country_code = try(var.primary_contact.country_code, null)
district_or_county = try(var.primary_contact.district_or_county, null)
full_name = try(var.primary_contact.full_name, null)
phone_number = try(var.primary_contact.phone_number, null)
postal_code = try(var.primary_contact.postal_code, null)
state_or_region = try(var.primary_contact.state_or_region, null)
website_url = try(var.primary_contact.website_url, null)
}
resource "aws_account_alternate_contact" "root_operations" {
alternate_contact_type = "OPERATIONS"
name = try(var.operations_contact.name, var.primary_contact.full_name)
title = var.operations_contact.title
email_address = var.operations_contact.email_address
phone_number = try(var.operations_contact.phone_number, var.primary_contact.phone_number)
}
resource "aws_account_alternate_contact" "root_billing" {
alternate_contact_type = "BILLING"
name = try(var.billing_contact.name, var.primary_contact.full_name)
title = var.billing_contact.title
email_address = var.billing_contact.email_address
phone_number = try(var.billing_contact.phone_number, var.primary_contact.phone_number)
}
resource "aws_account_alternate_contact" "root_security" {
alternate_contact_type = "SECURITY"
name = try(var.security_contact.name, var.primary_contact.full_name)
title = var.security_contact.title
email_address = var.security_contact.email_address
phone_number = try(var.security_contact.phone_number, var.primary_contact.phone_number)
}
### Child Account Management
resource "aws_account_primary_contact" "child" {
for_each = {
for account in local.accounts : account.name => account
}
account_id = aws_organizations_account.default[each.key].id
address_line_1 = var.primary_contact.address_line_1
address_line_2 = try(var.primary_contact.address_line_2, null)
address_line_3 = try(var.primary_contact.address_line_3, null)
city = var.primary_contact.city
company_name = try(var.primary_contact.company_name, null)
country_code = var.primary_contact.country_code
district_or_county = try(var.primary_contact.district_or_county, null)
full_name = var.primary_contact.full_name
phone_number = var.primary_contact.phone_number
postal_code = var.primary_contact.postal_code
state_or_region = try(var.primary_contact.state_or_region, null)
website_url = try(var.primary_contact.website_url, null)
}
resource "aws_account_alternate_contact" "child_operations" {
for_each = {
for account in local.accounts : account.name => account
}
account_id = aws_organizations_account.default[each.key].id
alternate_contact_type = "OPERATIONS"
name = try(var.operations_contact.name, var.primary_contact.full_name)
title = var.operations_contact.title
email_address = var.operations_contact.email_address
phone_number = try(var.operations_contact.phone_number, var.primary_contact.phone_number)
}
resource "aws_account_alternate_contact" "child_billing" {
for_each = {
for account in local.accounts : account.name => account
}
account_id = aws_organizations_account.default[each.key].id
alternate_contact_type = "BILLING"
name = try(var.billing_contact.name, var.primary_contact.full_name)
title = var.billing_contact.title
email_address = var.billing_contact.email_address
phone_number = try(var.billing_contact.phone_number, var.primary_contact.phone_number)
}
resource "aws_account_alternate_contact" "child_security" {
for_each = {
for account in local.accounts : account.name => account
}
account_id = aws_organizations_account.default[each.key].id
alternate_contact_type = "SECURITY"
name = try(var.security_contact.name, var.primary_contact.full_name)
title = var.security_contact.title
email_address = var.security_contact.email_address
phone_number = try(var.security_contact.phone_number, var.primary_contact.phone_number)
}
### Securityhub organization settings
resource "aws_securityhub_organization_admin_account" "default" {
count = contains(local.delegated_service_principals, "securityhub.amazonaws.com") ? 1 : 0
admin_account_id = aws_organizations_delegated_administrator.default["securityhub.amazonaws.com"].account_id
depends_on = [aws_organizations_organization.default]
}
### GuardDuty organization settings
resource "aws_guardduty_detector" "default" {
count = contains(local.delegated_service_principals, "guardduty.amazonaws.com") ? 1 : 0
}
resource "aws_guardduty_organization_admin_account" "default" {
count = contains(local.delegated_service_principals, "guardduty.amazonaws.com") ? 1 : 0
admin_account_id = aws_organizations_delegated_administrator.default["guardduty.amazonaws.com"].account_id
depends_on = [aws_organizations_organization.default, aws_guardduty_detector.default[0]]
}
### Inspector organization settings
resource "aws_inspector2_delegated_admin_account" "default" {
count = contains(local.delegated_service_principals, "inspector2.amazonaws.com") ? 1 : 0
account_id = aws_organizations_delegated_administrator.default["inspector2.amazonaws.com"].account_id
}