-
Notifications
You must be signed in to change notification settings - Fork 3
/
cdk_organization.py
499 lines (447 loc) · 17.6 KB
/
cdk_organization.py
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
################################################################################
# SAN99TIAGODEMO AWS ORGANIZATION (INFRASTRUCTURE AS CODE CDK SOLUTION)
# NOTE 1: I am using a CDK-based approach that is not maintained by AWS (yet):
# --> https://github.com/pepperize/cdk-organizations
# NOTE 2: As multiple accounts are being created, there are some "CDK" node
# ... dependencies. See example at the end on the source repo (link above)
################################################################################
# Built-in imports
import os
import json
# External imports
from aws_cdk import (
Stack,
CfnOutput,
RemovalPolicy,
)
from constructs import Construct
from pepperize_cdk_organizations import (
Account,
FeatureSet,
Organization,
OrganizationalUnit,
Policy,
PolicyType,
)
class OrganizationStack(Stack):
"""
Class to create the infrastructure for AWS Organizations.
"""
def __init__(
self, scope: Construct, construct_id: str, deployment_environment: str, **kwargs
) -> None:
"""
:param scope (Construct): Parent of this stack, usually an ``App`` or a ``Stage``, but could be any construct.
:param construct_id (str): The construct ID of this stack.
:param main_resources_name (str): The main solution name being deployed.
:param deployment_environment (str): Value that represents the deployment environment. For example: "dev" or "prod".
"""
super().__init__(scope, construct_id, **kwargs)
self.construct_id = construct_id
self.deployment_environment = deployment_environment
# AWS Organization creation, services configuration and SCPs
self.create_root_organization()
self.configure_organization_services()
self.configure_service_control_policies()
# Create "sandbox" OU with inner OUs and accounts inside
self.create_ou_sandbox()
self.create_accounts_inside_ou_sandbox()
# Create "infrastructure" OU with inner OUs and accounts inside
self.create_ou_infrastructure()
self.create_accounts_inside_ou_infrastructure()
# Create "workloads" OU with inner OUs and accounts inside
self.create_ou_workloads()
self.create_ou_finance()
self.create_accounts_inside_ou_finance()
self.create_ou_marketing()
self.create_accounts_inside_ou_marketing()
# Create "policy_staging_tests" OU with inner OUs and accounts inside
self.create_ou_policy_staging_tests()
self.create_accounts_inside_ou_policy_staging_tests()
# !IMPORTANT: this is mandatory for adding CDK dependencies for each account
self.add_cdk_accounts_dependencies() # DO NOT REMOVE!
# Create CloudFormation outputs
self.generate_cloudformation_outputs()
def create_root_organization(self):
"""
Method that creates the AWS Organization (root).
"""
self.organization = Organization(
self,
"RootOrganization",
feature_set=FeatureSet.ALL,
)
def configure_organization_services(self):
"""
Method that configures the AWS Organization with the desired enabled
services and policies (enable the desired ones).
"""
# Note 1: for more details on services, see full list at:
# --> https://docs.aws.amazon.com/organizations/latest/userguide/orgs_integrate_services_list.html
# Note 2: for more details on policies, see full list at:
# --> https://docs.aws.amazon.com/organizations/latest/userguide/orgs_manage_policies.html
# Enable service for AWS IAM Identity Center (AWS Single Sign-On Dashboard)
self.organization.enable_aws_service_access("sso.amazonaws.com")
# Enable service for Resource Access Manager (RAM)
self.organization.enable_aws_service_access("ram.amazonaws.com")
# Enable Service Control Policies (SCPs)
self.organization.enable_policy_type(PolicyType.SERVICE_CONTROL_POLICY)
def configure_service_control_policies(self):
"""
Method that configures the AWS Organization with the desired Service
Control Policies (SCPs) at organization level.
"""
# Get OS path to SCPs files and load them (for easier read/write of SCPs)
path_scp_prevent_leaving_org = os.path.join(
os.path.dirname(__file__),
"scp_prevent_leaving_org.json",
)
with open(path_scp_prevent_leaving_org, "r") as file:
scp_prevent_leaving_org = json.load(file)
path_scp_allow_specific_regions = os.path.join(
os.path.dirname(__file__),
"scp_allow_specific_regions.json",
)
with open(path_scp_allow_specific_regions, "r") as file:
scp_allow_specific_regions = json.load(file)
# SCP for preventing accounts of leaving organization
self.policy_deny_leave_org = Policy(
self,
"PolicyDenyLeave",
content=json.dumps(scp_prevent_leaving_org),
policy_name="PreventLeavingOrganization",
policy_type=PolicyType.SERVICE_CONTROL_POLICY,
description="SCP to prevent accounts from leaving the organization",
)
self.organization.attach_policy(self.policy_deny_leave_org)
# SCP for only allow access to specific regions in AWS (deny others)
self.policy_allow_specific_regions = Policy(
self,
"PolicyAllowSpecificRegions",
content=json.dumps(scp_allow_specific_regions),
policy_name="AllowSpecificRegions",
policy_type=PolicyType.SERVICE_CONTROL_POLICY,
description="SCP to only allow access to specific AWS Regions",
)
self.organization.attach_policy(self.policy_allow_specific_regions)
def create_ou_sandbox(self):
"""
Method that creates inner Organizational Units (OUs) inside the AWS
Organization for "Sandbox".
"""
self.top_level_ou_sandbox = OrganizationalUnit(
self,
"SandboxOU",
parent=self.organization.root,
organizational_unit_name="sandbox",
)
def create_accounts_inside_ou_sandbox(self):
"""
Method that creates AWS Accounts inside the Organizational Units (OUs)
for "Sandbox".
"""
self.account_sandbox_1 = Account(
self,
"SandboxAccount1",
account_name="san99tiago-sandbox-1",
email="[email protected]",
parent=self.top_level_ou_sandbox,
role_name="OrganizationAccountAccessRole",
)
# # DELETED TO AVOID LIMIT QUOTA OF 10 ACCOUNTS IN DEMO
# self.account_sandbox_2 = Account(
# self,
# "SandboxAccount2",
# account_name="san99tiago-sandbox-2",
# email="[email protected]",
# parent=self.top_level_ou_sandbox,
# role_name="OrganizationAccountAccessRole",
# )
def create_ou_infrastructure(self):
"""
Method that creates inner Organizational Units (OUs) inside the AWS
Organization for "Infrastructure".
"""
self.top_level_ou_infrastructure = OrganizationalUnit(
self,
"OUInfrastructure",
parent=self.organization.root,
organizational_unit_name="infrastructure",
)
self.ou_infrastructure_non_prod = OrganizationalUnit(
self,
"OUInfrastructureNonProd",
parent=self.top_level_ou_infrastructure,
organizational_unit_name="non-prod",
)
self.ou_infrastructure_prod = OrganizationalUnit(
self,
"OUInfrastructureProd",
parent=self.top_level_ou_infrastructure,
organizational_unit_name="prod",
)
def create_accounts_inside_ou_infrastructure(self):
"""
Method that creates AWS Accounts inside the Organizational Units (OUs)
for "Infrastructure".
"""
self.account_shared_services_non_prod = Account(
self,
"AccountSharedServicesNonProd",
account_name="shared-services-non-prod",
email="[email protected]",
parent=self.ou_infrastructure_non_prod,
role_name="OrganizationAccountAccessRole",
)
self.account_shared_services_prod = Account(
self,
"AccountSharedServicesProd",
account_name="shared-services-prod",
email="[email protected]",
parent=self.ou_infrastructure_prod,
role_name="OrganizationAccountAccessRole",
)
def create_ou_workloads(self):
"""
Method that creates inner Organizational Units (OUs) inside the AWS
Organization for "Workloads".
"""
self.top_level_ou_workloads = OrganizationalUnit(
self,
"OUWorkloads",
parent=self.organization.root,
organizational_unit_name="workloads",
)
def create_ou_finance(self):
"""
Method that creates inner Organizational Units (OUs) inside the AWS
Organization for "Finance".
"""
self.ou_finance = OrganizationalUnit(
self,
"OUFinance",
parent=self.top_level_ou_workloads,
organizational_unit_name="finance",
)
self.ou_finance_non_prod = OrganizationalUnit(
self,
"OUFinanceNonProd",
parent=self.ou_finance,
organizational_unit_name="non-prod",
)
self.ou_finance_prod = OrganizationalUnit(
self,
"OUFinanceProd",
parent=self.ou_finance,
organizational_unit_name="prod",
)
def create_accounts_inside_ou_finance(self):
"""
Method that creates AWS Accounts inside the Organizational Units (OUs)
for "Finance".
"""
self.account_finance_dev = Account(
self,
"AccountFinanceDev",
account_name="finance-dev",
email="[email protected]",
parent=self.ou_finance_non_prod,
role_name="OrganizationAccountAccessRole",
)
self.account_finance_qa = Account(
self,
"AccountFinanceQA",
account_name="finance-qa",
email="[email protected]",
parent=self.ou_finance_non_prod,
role_name="OrganizationAccountAccessRole",
)
self.account_finance_prod = Account(
self,
"AccountFinanceProd",
account_name="finance-prod",
email="[email protected]",
parent=self.ou_finance_prod,
role_name="OrganizationAccountAccessRole",
)
def create_ou_marketing(self):
"""
Method that creates inner Organizational Units (OUs) inside the AWS
Organization for "Marketing".
"""
self.ou_marketing = OrganizationalUnit(
self,
"OUMarketing",
parent=self.top_level_ou_workloads,
organizational_unit_name="marketing",
)
self.ou_marketing_non_prod = OrganizationalUnit(
self,
"OUMarketingNonProd",
parent=self.ou_marketing,
organizational_unit_name="non-prod",
)
self.ou_marketing_prod = OrganizationalUnit(
self,
"OUMarketingProd",
parent=self.ou_marketing,
organizational_unit_name="prod",
)
def create_accounts_inside_ou_marketing(self):
"""
Method that creates AWS Accounts inside the Organizational Units (OUs)
for "Marketing".
"""
self.account_marketing_dev = Account(
self,
"AccountMarketingDev",
account_name="marketing-dev",
email="[email protected]",
parent=self.ou_marketing_non_prod,
role_name="OrganizationAccountAccessRole",
removal_policy=RemovalPolicy.RETAIN,
import_on_duplicate=True,
)
self.account_marketing_prod = Account(
self,
"AccountMarketingProd",
account_name="marketing-prod",
email="[email protected]",
parent=self.ou_marketing_prod,
role_name="OrganizationAccountAccessRole",
)
def create_ou_policy_staging_tests(self):
"""
Method that creates inner Organizational Units (OUs) inside the AWS
Organization for "PolicyStagingTests".
"""
self.top_level_ou_policy_staging_tests = OrganizationalUnit(
self,
"OUPolicyStagingTests",
parent=self.organization.root,
organizational_unit_name="policy-staging-tests",
)
def create_accounts_inside_ou_policy_staging_tests(self):
"""
Method that creates AWS Accounts inside the Organizational Units (OUs)
for "PolicyStagingTests".
"""
self.account_policy_staging_tests = Account(
self,
"AccountPolicyStagingTests",
account_name="policy-staging-tests",
email="[email protected]",
parent=self.top_level_ou_policy_staging_tests,
role_name="OrganizationAccountAccessRole",
)
def add_cdk_accounts_dependencies(self):
"""
IMPORTANT METHOD to add CDK dependencies for the AWS Accounts that are
being created (to avoid 2 accounts creation simultaneously, which is
not supported by AWS). This is because of AWS Organizations limitation.
"""
# ! IMPORTANT: We MUST add these dependencies, as AWS Organizations only support
# ... one account creation "IN_PROGRESS". We add CDK dependency to solve issue
# ... and wait for the previous one to finish, to continue with the next...
self.account_shared_services_non_prod.node.add_dependency(
self.account_sandbox_1
)
self.account_shared_services_prod.node.add_dependency(
self.account_shared_services_non_prod
)
self.account_finance_dev.node.add_dependency(self.account_shared_services_prod)
self.account_finance_qa.node.add_dependency(self.account_finance_dev)
self.account_finance_prod.node.add_dependency(self.account_finance_qa)
self.account_marketing_dev.node.add_dependency(self.account_finance_prod)
self.account_marketing_prod.node.add_dependency(self.account_marketing_dev)
self.account_policy_staging_tests.node.add_dependency(
self.account_marketing_prod
)
def generate_cloudformation_outputs(self):
"""
Method to add the relevant CloudFormation outputs.
"""
CfnOutput(
self,
"DeploymentEnvironment",
value=self.deployment_environment,
description="Deployment environment",
)
CfnOutput(
self,
"OrganizationId",
value=self.organization.organization_id,
description="ID of AWS Organization",
)
CfnOutput(
self,
"RootId",
value=self.organization.root.identifier(),
description="ID of AWS Organization Root OU",
)
CfnOutput(
self,
"ManagementAccountId",
value=self.organization.management_account_id,
description="ID of Management Account",
)
CfnOutput(
self,
"ManagementAccountEmail",
value=self.organization.management_account_email,
description="Email of the Management Account",
)
CfnOutput(
self,
"AccountSandbox1Id",
value=self.account_sandbox_1.account_id,
description="ID of SandboxAccount1 Account",
)
CfnOutput(
self,
"AccountSharedServicesNonProdId",
value=self.account_shared_services_non_prod.account_id,
description="ID of AccountSharedServicesNonProd Account",
)
CfnOutput(
self,
"AccountSharedServicesProdId",
value=self.account_shared_services_prod.account_id,
description="ID of AccountSharedServicesProd Account",
)
CfnOutput(
self,
"AccountFinanceDevId",
value=self.account_finance_dev.account_id,
description="ID of AccountFinanceDev Account",
)
CfnOutput(
self,
"AccountFinanceQAId",
value=self.account_finance_qa.account_id,
description="ID of AccountFinanceQA Account",
)
CfnOutput(
self,
"AccountFinanceProdId",
value=self.account_finance_prod.account_id,
description="ID of AccountFinanceProd Account",
)
CfnOutput(
self,
"AccountMarketingDevId",
value=self.account_marketing_dev.account_id,
description="ID of AccountMarketingDev Account",
)
CfnOutput(
self,
"AccountMarketingProdId",
value=self.account_marketing_prod.account_id,
description="ID of AccountMarketingProd Account",
)
CfnOutput(
self,
"AccountPolicyStagingTestsId",
value=self.account_policy_staging_tests.account_id,
description="ID of AccountPolicyStagingTests Account",
)