forked from aws-solutions/aws-control-tower-customizations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customizations-for-aws-control-tower.template
3067 lines (3012 loc) · 117 KB
/
customizations-for-aws-control-tower.template
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
AWSTemplateFormatVersion: '2010-09-09'
Description: '(SO0089) - customizations-for-aws-control-tower Solution. Version: v2.5.2'
Parameters:
PipelineApprovalStage:
Description: Do you want to add a manual approval stage to the Custom Control Tower Configuration Pipeline?
AllowedValues:
- 'Yes'
- 'No'
Default: 'No'
Type: String
PipelineApprovalEmail:
Description: (Not required if Pipeline Approval Stage = 'No') Email for notifying that the CustomControlTower pipeline is waiting for an Approval
Type: String
CodePipelineSource:
Description: Which AWS CodePipeline source provider do you want to select?
AllowedValues:
- 'Amazon S3'
- 'AWS CodeCommit'
Default: 'Amazon S3'
Type: String
CodeCommitRepositoryName:
Description: Name of the CodeCommit repository that contains custom Control Tower configuration. The suffix .git is prohibited.
Default: custom-control-tower-configuration
Type: String
AllowedPattern: ^[\w\.-]+
CodeCommitBranchName:
Description: Name of the branch in CodeCommit repository that contains custom Control Tower configuration.
Default: main
Type: String
ExistingRepository:
Description: Are you using an existing CodeCommit repository that already contains custom Control Tower configuration?
Default: 'No'
Type: String
AllowedValues:
- 'Yes'
- 'No'
RegionConcurrencyType:
Description: Select the the concurrency type of deploying StackSets operations in Regions.
Default: 'PARALLEL'
Type: String
AllowedValues:
- 'PARALLEL'
- 'SEQUENTIAL'
MaxConcurrentPercentage:
Description: The maximum percentage of accounts in which to perform this operation at one time.
Default: 100
Type: String
FailureTolerancePercentage:
Description: The percentage of accounts, per Region, for which this stack operation can fail before AWS CloudFormation stops the operation in that Region.
Default: 10
Type: String
EnforceSuccessfulStackInstances:
Description: By default, CfCT's deployment pipeline defers to Stack Sets to report failures based on the combination of concurrency and fault tolerance you choose. Setting this parameter to true will consider a Stack Set deployment that contains failed stack instance deployments to be a failure in the deployment pipeline, regardless of fault tolerance you specify. This allows for you to specify 100% concurrency, but stop the pipeline post-deployment if stack instances fail to deploy.
Default: false
Type: String
AllowedValues:
- true
- false
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Pipeline Configuration
Parameters:
- PipelineApprovalStage
- PipelineApprovalEmail
- CodePipelineSource
- Label:
default: AWS CodeCommit Setup (Applicable if 'AWS CodeCommit' was selected as the CodePipeline Source)
Parameters:
- ExistingRepository
- CodeCommitRepositoryName
- CodeCommitBranchName
- Label:
default: AWS CloudFormation StackSets Configuration
Parameters:
- RegionConcurrencyType
- MaxConcurrentPercentage
- FailureTolerancePercentage
ParameterLabels:
PipelineApprovalStage:
default: Pipeline Approval Stage
PipelineApprovalEmail:
default: Pipeline Approval Email Address
CodePipelineSource:
default: AWS CodePipeline Source
ExistingRepository:
default: Existing CodeCommit Repository?
CodeCommitRepositoryName:
default: CodeCommit Repository Name
CodeCommitBranchName:
default: CodeCommit Branch Name
RegionConcurrencyType:
default: Region Concurrency Type
MaxConcurrentPercentage:
default: Max Concurrent Percentage
FailureTolerancePercentage:
default: Failure Tolerance Percentage
Mappings:
BucketConfiguration:
SourceBucketName:
Name: control-tower-cfct-assets-prod
SourceKeyName:
Name: customizations-for-aws-control-tower/v2.5.2/custom-control-tower-configuration.zip
CustomControlTowerPipelineS3TriggerKey:
Name: custom-control-tower-configuration.zip
CustomControlTowerPipelineS3NonTriggerKey:
Name: _custom-control-tower-configuration.zip
CodePipelineSource:
CodeCommit:
RepoName: /Customizations-for-aws-control-tower/CodeCommitRepoName
BranchName: /Customizations-for-aws-control-tower/CodeCommitBranchName
KMS:
Alias:
Name: CustomControlTowerKMSKey
Solution:
Metrics:
SendAnonymousData: 'Yes'
SolutionID: 'SO0089'
MetricsURL: 'https://metrics.awssolutionsbuilder.com/generic'
Data:
AddonTemplate: 'https://s3.amazonaws.com/control-tower-cfct-assets-prod/customizations-for-aws-control-tower/v2.5.2/custom-control-tower-initiation.template'
AWSControlTower:
ExecutionRole:
Name: "AWSControlTowerExecution"
LambdaFunction:
Logging:
Level: 'info'
FindReplace:
Values:
NoneType: 'null'
BoolType: 'yes,no,Yes,No,True,False,true,false' # no spaces are allowed in this string, comma is the only allowed delimiter
AutoBuild:
CustomControlTower:
Flag: 'No'
ControlTowerBaselineConfigStackset:
Info:
Name: 'AWSControlTowerBP-BASELINE-CONFIG'
Conditions:
IsPipelineApprovalStageCondition: !Equals [!Ref PipelineApprovalStage, 'Yes']
IsBuildCustomControlTowerCondition: !Equals [!FindInMap [AutoBuild, CustomControlTower, Flag], 'Yes']
IsCodeCommitPipelineSource: !Equals [!Ref CodePipelineSource, 'AWS CodeCommit']
IsExistingRepository: !Equals [!Ref ExistingRepository, 'Yes']
IsNewCodeCommitRepository: !And [!Not [!Condition IsExistingRepository], !Condition IsCodeCommitPipelineSource]
Resources:
PipelineApprovalTopic:
Type: AWS::SNS::Topic
Condition: IsPipelineApprovalStageCondition
Properties:
KmsMasterKeyId: alias/aws/sns
Subscription:
- Endpoint: !Ref PipelineApprovalEmail
Protocol: email
CustomControlTowerPipelineS3Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
BucketName: !Sub custom-control-tower-configuration-${AWS::AccountId}-${AWS::Region}
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
VersioningConfiguration:
Status: Enabled
LoggingConfiguration:
DestinationBucketName: !Ref CustomControlTowerS3AccessLogsBucket
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
CustomControlTowerPipelineS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CustomControlTowerPipelineS3Bucket
PolicyDocument:
Statement:
- Sid: DenyDeleteBucket
Effect: Deny
Principal: "*"
Action: s3:DeleteBucket
Resource: !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineS3Bucket}
CustomControlTowerPipelineArtifactS3Bucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
VersioningConfiguration:
Status: Enabled
LoggingConfiguration:
DestinationBucketName: !Ref CustomControlTowerS3AccessLogsBucket
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
CustomControlTowerPipelineArtifactS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CustomControlTowerPipelineArtifactS3Bucket
PolicyDocument:
Statement:
- Sid: DenyDeleteBucket
Effect: Deny
Principal: "*"
Action: s3:DeleteBucket
Resource: !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineArtifactS3Bucket}
# Create buckets using S3-SSE keys for default encryption
CustomControlTowerS3AccessLogsBucket:
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Type: AWS::S3::Bucket
Metadata:
cfn_nag:
rules_to_suppress:
- id: W35
reason: "This S3 bucket is used as the destination for 'CustomControlTowerPipelineS3Bucket' and 'CustomControlTowerPipelineArtifactS3Bucket'"
Properties:
AccessControl: LogDeliveryWrite
VersioningConfiguration:
Status: Enabled
BucketEncryption:
ServerSideEncryptionConfiguration:
- ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
PublicAccessBlockConfiguration:
BlockPublicAcls: True
BlockPublicPolicy: True
IgnorePublicAcls: True
RestrictPublicBuckets: True
CustomControlTowerS3AccessLogsBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CustomControlTowerS3AccessLogsBucket
PolicyDocument:
Statement:
- Sid: DenyDeleteBucket
Effect: Deny
Principal: "*"
Action: s3:DeleteBucket
Resource: !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerS3AccessLogsBucket}
CustomControlTowerCodeCommit:
Type: AWS::CodeCommit::Repository
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Condition: IsNewCodeCommitRepository
Properties:
RepositoryDescription: Configuration for Customizations for AWS Control Tower solution
RepositoryName: !Ref CodeCommitRepositoryName
Code:
S3:
Bucket: control-tower-cfct-assets-prod
Key: !Sub customizations-for-aws-control-tower/v2.5.2/custom-control-tower-configuration-${AWS::Region}.zip
# SSM Parameter to store the git repository name
CustomControlTowerRepoNameParameter:
Type: AWS::SSM::Parameter
Properties:
Name:
Fn::FindInMap:
- CodePipelineSource
- CodeCommit
- RepoName
Description: Contains the name of the CodeCommit repository
Type: String
Value: !Ref CodeCommitRepositoryName
# SSM Parameter to store the git repository branch name
CustomControlTowerBranchNameParameter:
Type: AWS::SSM::Parameter
Properties:
Name:
Fn::FindInMap:
- CodePipelineSource
- CodeCommit
- BranchName
Description: Contains the name of the CodeCommit repository branch
Type: String
Value: !Ref CodeCommitBranchName
CustomControlTowerCodePipelineRole:
Type: AWS::IAM::Role
Metadata:
cfn_nag:
rules_to_suppress:
- id: W28
reason: "The role name is defined to identify Custom Control Tower resources."
- id: W11
reason: "Allow Resource * for KMS/SSM API. KMS Service only support all resources. Key ID is generated by the service. SSM parameters are customer defined."
Properties:
RoleName: CustomControlTowerCodePipelineRole
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "codepipeline.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "Custom-Control-Tower-CodePipeline-Policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- s3:GetBucketVersioning
Resource:
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineArtifactS3Bucket}
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineS3Bucket}
- Effect: "Allow"
Action:
- s3:PutObject
- s3:GetObject
- s3:GetObjectVersion
Resource:
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineArtifactS3Bucket}/*
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineS3Bucket}/*
- Effect: Allow
Action:
- ssm:PutParameter
- ssm:GetParameter
- ssm:DeleteParameter
- ssm:GetParametersByPath
- ssm:DescribeParameters
Resource: '*'
- Effect: "Allow"
Action:
- "codebuild:BatchGetBuilds"
- "codebuild:StartBuild"
Resource:
- !Sub arn:${AWS::Partition}:codebuild:${AWS::Region}:${AWS::AccountId}:project/${CustomControlTowerCodeBuild}
- !Sub arn:${AWS::Partition}:codebuild:${AWS::Region}:${AWS::AccountId}:project/${SCPCodeBuild}
- !Sub arn:${AWS::Partition}:codebuild:${AWS::Region}:${AWS::AccountId}:project/${StackSetCodeBuild}
- Effect: "Allow"
Action:
- codecommit:GetBranch
- codecommit:GetCommit
- codecommit:UploadArchive
- codecommit:GetUploadArchiveStatus
- codecommit:CancelUploadArchive
Resource: "*"
- Effect: "Allow"
Action:
- lambda:ListFunctions
- lambda:ListVersionsByFunction
Resource: "*"
- !If
- IsPipelineApprovalStageCondition
- Effect: "Allow"
Action:
- "sns:Publish"
Resource: !Ref PipelineApprovalTopic
- !Ref AWS::NoValue
CustomControlTowerCodePipeline:
Type: AWS::CodePipeline::Pipeline
Properties:
Name: Custom-Control-Tower-CodePipeline
RoleArn: !GetAtt CustomControlTowerCodePipelineRole.Arn
ArtifactStore:
Location: !Ref CustomControlTowerPipelineArtifactS3Bucket
Type: S3
Stages:
- Name: Source
Actions:
- Name: Source
ActionTypeId:
!If
- IsCodeCommitPipelineSource
- Category: Source
Owner: AWS
Version: "1"
Provider: CodeCommit
- Category: Source
Owner: AWS
Version: "1"
Provider: S3
OutputArtifacts:
- Name: SourceApp
Configuration:
!If
- IsCodeCommitPipelineSource
- RepositoryName: !Ref CodeCommitRepositoryName
BranchName: !Ref CodeCommitBranchName
- S3Bucket: !Ref CustomControlTowerPipelineS3Bucket
S3ObjectKey: !FindInMap [BucketConfiguration, CustomControlTowerPipelineS3TriggerKey, Name]
RunOrder: 1
- Name: Build
Actions:
- Name: CodeBuild
InputArtifacts:
- Name: SourceApp
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
OutputArtifacts:
- Name: BuiltApp
Configuration:
ProjectName: !Ref CustomControlTowerCodeBuild
- !If
- IsPipelineApprovalStageCondition
- Name: Approval
Actions:
- Name: Approval
ActionTypeId:
Category: Approval
Owner: AWS
Version: "1"
Provider: Manual
RunOrder: 1
Configuration:
NotificationArn: !Ref PipelineApprovalTopic
- !Ref AWS::NoValue
- Name: ServiceControlPolicy
Actions:
- Name: CodeBuild
InputArtifacts:
- Name: BuiltApp
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
Configuration:
ProjectName: !Ref SCPCodeBuild
- Name: CloudformationResource
Actions:
- Name: CodeBuild
InputArtifacts:
- Name: BuiltApp
ActionTypeId:
Category: Build
Owner: AWS
Version: "1"
Provider: CodeBuild
Configuration:
ProjectName: !Ref StackSetCodeBuild
CustomControlTowerCodeBuildRole:
Type: "AWS::IAM::Role"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Allow Resource * for Cloudformation/SSM API: needs to support user defined cfn templates and ssm parameter names."
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "Custom-Control-Tower-CodeBuild-Policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
- Effect: "Allow"
Action:
- s3:PutObject
- s3:GetObjectVersion
- s3:DeleteObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineArtifactS3Bucket}/*
- Effect: Allow
Action:
- s3:GetObject
- cloudformation:ValidateTemplate
Resource: "*"
- Effect: "Allow"
Action:
- s3:GetObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::control-tower-cfct-assets-prod/*
- Effect: Allow
Action:
- ssm:GetParameter
- ssm:GetParametersByPath
Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: Allow
Action:
- ssm:DescribeParameters
Resource: '*' # The APIs above only support '*' resource.
CustomControlTowerCodeBuild:
Type: AWS::CodeBuild::Project
DependsOn: CustomControlTowerDeploymentLambda
Properties:
Name: Custom-Control-Tower-CodeBuild
ServiceRole: !GetAtt CustomControlTowerCodeBuildRole.Arn
EncryptionKey: !Sub
- alias/${KMSKeyName}
- {KMSKeyName: !FindInMap [KMS, Alias, Name]}
Source:
Type: CODEPIPELINE
BuildSpec: "version: 0.2\nphases:\n install:\n runtime-versions:\n python: 3.8\n ruby: 2.6\n commands:\n - export current=$(pwd)\n - if [ -f manifest.yaml ];then export current=$(pwd);else if [ -f custom-control-tower-configuration/manifest.yaml ]; then export current=$(pwd)/custom-control-tower-configuration; else echo 'manifest.yaml does not exist at the root level of custom-control-tower-configuration.zip or inside custom-control-tower-configuration folder, please check the ZIP file'; exit 1; fi; fi;\n - apt-get -q update 1> /dev/null\n - apt-get -q install zip wget python3-pip libyaml-dev -y 1>/dev/null\n - export LC_ALL='en_US.UTF-8'\n - locale-gen en_US en_US.UTF-8\n - dpkg-reconfigure locales --frontend noninteractive\n pre_build:\n commands:\n - cd $current\n - echo 'Download CustomControlTower Scripts'\n - aws s3 cp --quiet s3://control-tower-cfct-assets-prod/customizations-for-aws-control-tower/v2.5.2/custom-control-tower-scripts.zip $current\n - unzip -q -o $current/custom-control-tower-scripts.zip -d $current\n - cp codebuild_scripts/* .\n - bash install_stage_dependencies.sh $STAGE_NAME\n build:\n commands:\n - echo 'Starting build $(date) in $(pwd)'\n - echo 'bash execute_stage_scripts.sh $STAGE_NAME $LOG_LEVEL $WAIT_TIME $SM_ARN $ARTIFACT_BUCKET $KMS_KEY_ALIAS_NAME $BOOL_VALUES $NONE_TYPE_VALUES'\n - bash execute_stage_scripts.sh $STAGE_NAME $LOG_LEVEL $WAIT_TIME $SM_ARN $ARTIFACT_BUCKET $KMS_KEY_ALIAS_NAME $BOOL_VALUES $NONE_TYPE_VALUES \n - echo 'Running build scripts completed $(date)'\n post_build:\n commands:\n - echo 'Starting post build $(date) in $(pwd)'\n - echo 'build completed on $(date)'\n\nartifacts:\n files:\n - '**/*'\n\n"
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: "aws/codebuild/standard:5.0"
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: ARTIFACT_BUCKET
Value: !Ref CustomControlTowerPipelineArtifactS3Bucket
- Name: NONE_TYPE_VALUES
Value: !FindInMap [FindReplace, Values, NoneType]
- Name: BOOL_VALUES
Value: !FindInMap [FindReplace, Values, BoolType]
- Name: STAGE_NAME
Value: "build"
- Name: SM_ARN
Value: "NA"
- Name: LOG_LEVEL
Value: !FindInMap [LambdaFunction, Logging, Level]
- Name: WAIT_TIME
Value: "15"
- Name: KMS_KEY_ALIAS_NAME
Value: !FindInMap [KMS, Alias, Name]
- Name: SOLUTION_ID
Value: !FindInMap [ Solution, Metrics, SolutionID ]
- Name: SOLUTION_VERSION
Value: v2.5.2
Artifacts:
Name: !Sub ${CustomControlTowerPipelineArtifactS3Bucket}-Built
Type: CODEPIPELINE
SCPCodeBuildRole:
Type: "AWS::IAM::Role"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Allow * for Organizations APIs to list/describe/move user created child accounts in the AWS Organizations"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "Custom-Control-Tower-SCP-CodeBuild-Policy-Logs"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
- PolicyName: "Custom-Control-Tower-SCP-CodeBuild-Policy-S3"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- s3:GetObject
- s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineArtifactS3Bucket}/*
- Effect: "Allow"
Action:
- s3:GetObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::*/* # needed to support validation of remotely sourced templates feature. The host S3 bucket can be created by the customers or partners.
- PolicyName: "Custom-Control-Tower-SCP-CodeBuild-Policy-StepFunctions"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- states:ListExecutions
- states:StartExecution
- states:StopExecution
- states:DescribeStateMachine
Resource:
- !Ref ServiceControlPolicyMachine
- Effect: Allow
Action:
- states:DescribeStateMachineForExecution
- states:DescribeExecution
Resource:
- !Sub arn:${AWS::Partition}:states:${AWS::Region}:${AWS::AccountId}:execution:${ServiceControlPolicyMachine.Name}:*
- PolicyName: "Custom-Control-Tower-SCP-CodeBuild-Policy-Organizations"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- organizations:ListRoots
- organizations:ListOrganizationalUnitsForParent
- organizations:ListAccountsForParent
Resource: '*' # The APIs above only support '*' resource.
- PolicyName: "Custom-Control-Tower-SCP-CodeBuild-Policy-SSM"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ssm:GetParameter
- ssm:GetParametersByPath
Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: Allow
Action:
- ssm:DescribeParameters
Resource: '*' # The APIs above only support '*' resource.
SCPCodeBuild:
Type: AWS::CodeBuild::Project
DependsOn: CustomControlTowerDeploymentLambda
Properties:
Name: Custom-Control-Tower-SCP-CodeBuild
ServiceRole: !GetAtt SCPCodeBuildRole.Arn
EncryptionKey: !Sub
- alias/${KMSKeyName}
- {KMSKeyName: !FindInMap [KMS, Alias, Name]}
Source:
Type: CODEPIPELINE
BuildSpec: "version: 0.2\nphases:\n install:\n runtime-versions:\n python: 3.8\n ruby: 2.6\n commands:\n - export current=$(pwd)\n - if [ -f manifest.yaml ];then export current=$(pwd);else if [ -f custom-control-tower-configuration/manifest.yaml ]; then export current=$(pwd)/custom-control-tower-configuration; else echo 'manifest.yaml does not exist at the root level of custom-control-tower-configuration.zip or inside custom-control-tower-configuration folder, please check the ZIP file'; exit 1; fi; fi;\n - apt-get -q update 1> /dev/null\n - apt-get -q install zip wget python3-pip libyaml-dev -y 1> /dev/null \n pre_build:\n commands:\n - cd $current\n - echo 'Download CustomControlTower Scripts'\n - aws s3 cp --quiet s3://control-tower-cfct-assets-prod/customizations-for-aws-control-tower/v2.5.2/custom-control-tower-scripts.zip $current\n - unzip -q -o $current/custom-control-tower-scripts.zip -d $current\n - cp codebuild_scripts/* .\n - bash install_stage_dependencies.sh $STAGE_NAME\n build:\n commands:\n - echo 'Starting build $(date) in $(pwd)'\n - echo 'bash execute_stage_scripts.sh $STAGE_NAME $LOG_LEVEL $WAIT_TIME $SM_ARN $ARTIFACT_BUCKET $KMS_KEY_ALIAS_NAME $BOOL_VALUES $NONE_TYPE_VALUES'\n - bash execute_stage_scripts.sh $STAGE_NAME $LOG_LEVEL $WAIT_TIME $SM_ARN $ARTIFACT_BUCKET $KMS_KEY_ALIAS_NAME $BOOL_VALUES $NONE_TYPE_VALUES\n - echo 'Running build scripts completed $(date)'\n post_build:\n commands:\n - echo 'Starting post build $(date) in $(pwd)'\n - echo 'build completed on $(date)'\n\nartifacts:\n files:\n - '**/*'\n"
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: "aws/codebuild/standard:5.0"
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: SM_ARN
Value: !Ref ServiceControlPolicyMachine
- Name: LOG_LEVEL
Value: !FindInMap [LambdaFunction, Logging, Level]
- Name: WAIT_TIME
Value: "15"
- Name: STAGE_NAME
Value: "scp"
- Name: ARTIFACT_BUCKET
Value: !Ref CustomControlTowerPipelineArtifactS3Bucket
- Name: KMS_KEY_ALIAS_NAME
Value: !FindInMap [KMS, Alias, Name]
- Name: SOLUTION_ID
Value: !FindInMap [ Solution, Metrics, SolutionID ]
- Name: SOLUTION_VERSION
Value: v2.5.2
Artifacts:
Name: !Sub ${CustomControlTowerPipelineArtifactS3Bucket}-Built
Type: CODEPIPELINE
TimeoutInMinutes: 60
StackSetCodeBuildRole:
Type: "AWS::IAM::Role"
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Allow * for Organizations APIs to list/describe/move user created child accounts in the AWS Organizations"
- id: W11
reason: "Allow * for ec2 APIs because information like account, region, etc. are dynamically determined by custom configuration"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-Logs"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-S3"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- s3:GetObject
- s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineArtifactS3Bucket}/*
- Effect: "Allow"
Action:
- s3:GetObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::*/* # needed to support validation of remotely sourced templates feature. The host S3 bucket can be created by the customers or partners.
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-StepFunctions"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- states:ListExecutions
- states:StartExecution
- states:StopExecution
- states:DescribeStateMachine
Resource:
- !Ref StackSetStateMachine
- Effect: Allow
Action:
- states:DescribeStateMachineForExecution
- states:DescribeExecution
Resource:
- !Sub arn:${AWS::Partition}:states:${AWS::Region}:${AWS::AccountId}:execution:${StackSetStateMachine.Name}:*
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-Organizations"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- organizations:ListRoots
- organizations:ListOrganizationalUnitsForParent
- organizations:ListAccountsForParent
- organizations:ListAccounts
- organizations:DescribeOrganization
Resource: '*' # The APIs above only support '*' resource.
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-SSM"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ssm:GetParameter
- ssm:PutParameter
- ssm:GetParametersByPath
Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: Allow
Action:
- ssm:DescribeParameters
Resource: '*' # The APIs above only support '*' resource.
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-KMS"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- kms:Encrypt
- kms:Decrypt
- kms:ReEncryptFrom
- kms:ReEncryptTo
- kms:GenerateDataKey
- kms:GenerateDataKeyWithoutPlaintext
- kms:DescribeKey
Resource:
- !Sub arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/*
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-STS"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Resource: !Sub
- arn:${AWS::Partition}:iam::*:role/${CustomControlTowerExecutionRole}
- {CustomControlTowerExecutionRole: !FindInMap [AWSControlTower, ExecutionRole, Name]}
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-EC2"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- ec2:DescribeAvailabilityZones
Resource:
- '*' # The APIs above only support '*' resource.
- PolicyName: "Custom-Control-Tower-StackSet-CodeBuild-Policy-CloudFormation"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- cloudformation:DescribeStackSet
- cloudformation:ListStackSets
- cloudformation:ListStackInstances
- cloudformation:ListStackSetOperations
Resource:
- !Sub arn:${AWS::Partition}:cloudformation:${AWS::Region}:${AWS::AccountId}:stackset/*
StackSetCodeBuild:
Type: AWS::CodeBuild::Project
DependsOn: CustomControlTowerDeploymentLambda
Properties:
Name: Custom-Control-Tower-StackSet-CodeBuild
ServiceRole: !GetAtt StackSetCodeBuildRole.Arn
EncryptionKey: !Sub
- alias/${KMSKeyName}
- {KMSKeyName: !FindInMap [KMS, Alias, Name]}
Source:
Type: CODEPIPELINE
BuildSpec: "version: 0.2\nphases:\n install:\n runtime-versions:\n python: 3.8\n ruby: 2.6\n commands:\n - export current=$(pwd)\n - if [ -f manifest.yaml ];then export current=$(pwd);else if [ -f custom-control-tower-configuration/manifest.yaml ]; then export current=$(pwd)/custom-control-tower-configuration; else echo 'manifest.yaml does not exist at the root level of custom-control-tower-configuration.zip or inside custom-control-tower-configuration folder, please check the ZIP file'; exit 1; fi; fi;\n - apt-get -q update 1> /dev/null\n - apt-get -q install zip wget python3-pip libyaml-dev -y 1> /dev/null\n pre_build:\n commands:\n - cd $current\n - echo 'Download CustomControlTower Scripts'\n - aws s3 cp --quiet s3://control-tower-cfct-assets-prod/customizations-for-aws-control-tower/v2.5.2/custom-control-tower-scripts.zip $current\n - unzip -q -o $current/custom-control-tower-scripts.zip -d $current\n - cp codebuild_scripts/* .\n - bash install_stage_dependencies.sh $STAGE_NAME\n build:\n commands:\n - echo 'Starting build $(date) in $(pwd)'\n - echo 'bash execute_stage_scripts.sh $STAGE_NAME $LOG_LEVEL $WAIT_TIME $SM_ARN $ARTIFACT_BUCKET $KMS_KEY_ALIAS_NAME $BOOL_VALUES $NONE_TYPE_VALUES'\n - bash execute_stage_scripts.sh $STAGE_NAME $LOG_LEVEL $WAIT_TIME $SM_ARN $ARTIFACT_BUCKET $KMS_KEY_ALIAS_NAME $BOOL_VALUES $NONE_TYPE_VALUES\n - echo 'Running build scripts completed $(date)'\n post_build:\n commands:\n - echo 'Starting post build $(date) in $(pwd)'\n - echo 'build completed on $(date)'\n\nartifacts:\n files:\n - '**/*'\n"
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: "aws/codebuild/standard:5.0"
Type: LINUX_CONTAINER
EnvironmentVariables:
- Name: SM_ARN
Value: !Ref StackSetStateMachine
- Name: LOG_LEVEL
Value: !FindInMap [LambdaFunction, Logging, Level]
- Name: WAIT_TIME
Value: "15"
- Name: STAGE_NAME
Value: "stackset"
- Name: ARTIFACT_BUCKET
Value: !Ref CustomControlTowerPipelineArtifactS3Bucket
- Name: KMS_KEY_ALIAS_NAME
Value: !FindInMap [KMS, Alias, Name]
- Name: ENFORCE_SUCCESSFUL_STACK_INSTANCES
Value: !Ref EnforceSuccessfulStackInstances
- Name: EXECUTION_ROLE_NAME
Value: !FindInMap [AWSControlTower, ExecutionRole, Name]
- Name: SOLUTION_ID
Value: !FindInMap [Solution, Metrics, SolutionID]
- Name: SOLUTION_VERSION
Value: v2.5.2
- Name: METRICS_URL
Value: !FindInMap [Solution, Metrics, MetricsURL]
- Name: CONTROL_TOWER_BASELINE_CONFIG_STACKSET
Value: !FindInMap [ControlTowerBaselineConfigStackset, Info, Name]
Artifacts:
Name: !Sub ${CustomControlTowerPipelineArtifactS3Bucket}-Built
Type: CODEPIPELINE
TimeoutInMinutes: 480
CustomControlTowerDeploymentLambdaRole:
Type: AWS::IAM::Role
Metadata:
cfn_nag:
rules_to_suppress:
- id: W11
reason: "Allow Resource * for KMS API. KMS Service only support all resources. Key ID is generated by the service."
- id: W28
reason: "The role name is defined to identify Custom Control Tower resources."
Properties:
RoleName: CustomControlTowerDeploymentLambdaRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
- PolicyName: Custom-Control-Tower-DeploymentLambda-Logs
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource:
- !Sub arn:${AWS::Partition}:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*
- Effect: Allow
Action:
- xray:PutTraceSegments
- xray:PutTelemetryRecords
Resource: '*'
- PolicyName: Custom-Control-Tower-DeploymentLambda-KMS
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: "Allow"
Action:
- kms:DescribeKey
- kms:TagResource
- kms:PutKeyPolicy
- kms:GetKeyRotationStatus
- kms:EnableKeyRotation
Resource:
- !Sub arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/*
- Effect: "Allow"
Action:
- kms:CreateKey
- kms:ListAliases
Resource: "*"
- Effect: "Allow"
Action:
- kms:CreateAlias
Resource:
- !Sub arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:alias/*
- !Sub arn:${AWS::Partition}:kms:${AWS::Region}:${AWS::AccountId}:key/*
- PolicyName: Custom-Control-Tower-DeploymentLambda-S3
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: "Allow"
Action:
- s3:GetEncryptionConfiguration
- s3:PutEncryptionConfiguration
Resource:
- !GetAtt CustomControlTowerPipelineS3Bucket.Arn
- Effect: "Allow"
Action:
- s3:GetObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::control-tower-cfct-assets-prod/*
- Effect: "Allow"
Action:
- s3:GetObject
- s3:PutObject
Resource:
- !Sub arn:${AWS::Partition}:s3:::${CustomControlTowerPipelineS3Bucket}/*
- PolicyName: Custom-Control-Tower-DeploymentLambda-SSM
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- ssm:PutParameter
- ssm:GetParameter
- ssm:DeleteParameter
- ssm:GetParametersByPath
Resource: !Sub arn:${AWS::Partition}:ssm:${AWS::Region}:${AWS::AccountId}:parameter/*
- Effect: Allow
Action:
- ssm:DescribeParameters
Resource: '*' # The APIs above only support '*' resource.
CustomControlTowerDeploymentLambda:
Type: AWS::Lambda::Function
Metadata:
cfn_nag:
rules_to_suppress:
- id: W58
reason: "Permission for writing cloudwatch logs is defined in the lambda role"
- id: W89
reason: "This lambda function does not need access to VPC resources"
- id: W92
reason: "This use case does not need to set the ReservedConcurrentExecutions"