-
Notifications
You must be signed in to change notification settings - Fork 13
/
template.yaml
1067 lines (1008 loc) · 34.4 KB
/
template.yaml
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
#
# Deployment Stack
# Creates the API Gateway, Lambda, all roles, etc and maps them together.
#
---
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: "CloudGenesis: Automated CloudFormation Management from Git Source"
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Main Configuration
Parameters:
- AccountIds
- AllowIAMCapabilities
- AllowAutoExpandCapability
- CFDeployerRoleName
- CFServiceRoleName
- SSMPath
- SemanticStackNaming
- ECRCodeBuildRepository
- Label:
default: Git Source
Parameters:
- GithubRepoOwner
- GithubRepoName
- GitHubToken
- WebhookSecretToken
- Label:
default: Slack & Email Configuration
Parameters:
- SlackChannel
- SlackWebHookUrl
- EmailContact
- Label:
default: Optional Configuration
Parameters:
- StackChangeSetPrefix
- ExternalNotificationSNSArn
- TrackingTagPrefix
- ExternalS3BucketPathArns
- S3FileEventHandlerReservedConcurrency
ParameterLabels:
AccountIds:
default: AWS Account ID's
AllowIAMCapabilities:
default: CloudFormation IAM Capabilities Enabled?
AllowAutoExpandCapability:
default: CloudFormation Auto Expand Capability Enabled?
SSMPath:
default: SSM Path Prefix
SlackChannel:
default: Slack Channel Name
SlackWebHookUrl:
default: Slack Web Hook URL
EmailContact:
default: Email Content (slack fallback)
ECRCodeBuildRepository:
default: ECR Repository Name for CodeBuild jobs
StackChangeSetPrefix:
default: Stack Change Set Prefix
SemanticStackNaming:
default: Force Semantic Stack Naming? (Recommended)
ExternalNotificationSNSArn:
default: External SNS Notification ARN
TrackingTagPrefix:
default: Tracking Tag Prefix
GithubRepoName:
default: GitHub Repository Name
GithubRepoOwner:
default: GitHub Repository Owner
GitHubToken:
default: GitHub Personal Access Token
WebhookSecretToken:
default: GitHub Webhook Auth Token
ExternalS3BucketPathArns:
default: Additional S3 Bucket ARN Paths for Templates
S3FileEventHandlerReservedConcurrency:
default: Concurrency for number of stacks commands sent at a time to CloudFormation.
Parameters:
AllowIAMCapabilities:
Description: "CloudFormation has an IAM safe guard built in. If this is false, no IAM resources can be created even if the policy arns allow it"
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
AllowAutoExpandCapability:
Description: "CloudFormation requires you to indicate if you want to allow it to Auto Expand macros "
Type: String
Default: "false"
AllowedValues:
- "true"
- "false"
CFDeployerRoleName:
Description: "The name of the role in each account that the deployer assumes for access to CloudFormation"
Type: String
CFServiceRoleName:
Description: "(Optional): The name of the CloudFormation service role in each account that is used by CF for managing the resources. Leave EMPTY if wish to use the CFDeployerRoleName's role to also manage the resources (no service role). This role must be passable by the CFDeployerRoleName and assumable by CloudFormation"
Type: String
Default: ""
StackChangeSetPrefix:
Description: "(Optional): The naming prefix applied to the the name of stack change sets. This is useful to lock down teams' automation deployers and roles to only be able to execute change sets that belong to them. If not set, the change set name will be: cf-deployer-automation. If it is set the value will be prepended to the following: ${yourvalue}-cf-deployer-automation"
Type: String
Default: ""
SemanticStackNaming:
Description: "Forces semantic stack naming, even when stacks provide their own StackName. This is highly recommended, otherwise two stacks that accidentally specify the same name will overwrite each other"
Type: String
Default: "true"
AllowedValues:
- "true"
- "false"
AccountIds:
Description: "The comma delimited list of account ID's that can this instance of CloudGenesis can automatically deploy to."
Type: CommaDelimitedList
ExternalNotificationSNSArn:
Description: "(Optional): The SNS arn to publish events to that contain a record of the change that is occurring by the automation. Leave empty if not used."
Type: String
Default: ""
SlackWebHookUrl:
Description: "Slack webhook URL; see https://example.slack.com/apps/"
Type: String
SlackChannel:
Description: "Slack Channel name to post to"
Type: String
EmailContact:
Description: "Email to send unhandled errors (when Slack's API is down)"
Type: String
SSMPath:
Type: String
Description: 'Base path to SSM secrets needed by deployer (e.g. "/cf-automation/${StackName}/*")'
TrackingTagPrefix:
Type: String
Description: "(Optional): Every stack launched has a tag automatically added to the stack so you what stack file the stack was created from. Use this prefix to prepend to that path other useful information such as what repo the stack came from or what deployer it came from. If you use multiple deployers and git repos, this is recommended to use this to identify which one."
Default: ""
ExternalS3BucketPathArns:
Description: "(Optional): A comma delimited list of S3 Bucket PATH ARNs to external buckets that can be used by stack templates. must be in this form: arn:aws:s3:::BUCKET_NAME/[optional-sub-path]*"
Type: CommaDelimitedList
Default: ""
ECRCodeBuildRepository:
Description: "The ECR repository name that holds the docker image used for the CodeBuild jobs ran on Git repo. ECR repo must be on the same account as CloudGenesis itself."
Type: String
GithubRepoName:
Description: The name of the repo (As seen on GitHub.com)
Type: String
GithubRepoOwner:
Description: The owner of the repo (As seen on Github.com)
Type: String
GitHubToken:
Type: String
NoEcho: True
Description: GitHub repository OAuth token - this is a GitHub Personal Access Token. All Git actions will appear as from the owner of this token.
WebhookSecretToken:
Description: A private token used to authenticate Webhook requests that CodePiepline receives. You generate this secret token and CodePipeline sends it to GitHub for GitHub to store and include on webhook requests.
NoEcho: True
Type: String
S3FileEventHandlerReservedConcurrency:
Description: The reserved concurrency for the S3FileEventHandler functions. Used to avoid cloud formation API throttling.
Type: Number
Default: 1
Conditions:
HasExternalNotificationSNS: !Not [!Equals [!Ref ExternalNotificationSNSArn, ""]]
HasCFServiceRole: !Not [!Equals [!Ref CFServiceRoleName, ""]]
HasStackChageSetPrefix: !Not [!Equals [!Ref StackChangeSetPrefix, ""]]
HasTrackingTagPrefix: !Not [!Equals [!Ref TrackingTagPrefix, ""]]
HasExternalS3BucketSupport: !Not [!Equals [!Join ["", !Ref ExternalS3BucketPathArns], ""]]
Resources:
S3CreateUpdateEventsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: S3 CreateUpdate Events"
S3DeleteEventsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: S3 Delete Events"
S3EventsSNSPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
Topics:
- !Ref S3CreateUpdateEventsTopic
- !Ref S3DeleteEventsTopic
PolicyDocument:
Id: S3TopicPolicy
Version: '2012-10-17'
Statement:
- Sid: "CreateEvents"
Effect: Allow
Principal:
AWS: "*"
Action: sns:Publish
Resource: !Ref S3CreateUpdateEventsTopic
Condition:
ArnLike:
aws:SourceArn: !Sub "arn:aws:s3:::${AWS::StackName}-cfstack-bucket"
- Sid: "DeleteEvents"
Effect: Allow
Principal:
AWS: "*"
Action: sns:Publish
Resource: !Ref S3DeleteEventsTopic
Condition:
ArnLike:
aws:SourceArn: !Sub "arn:aws:s3:::${AWS::StackName}-cfstack-bucket"
CloudFormationSyncBucket:
Type: AWS::S3::Bucket
DependsOn: S3EventsSNSPolicy
Properties:
BucketName: !Sub "${AWS::StackName}-cfstack-bucket"
VersioningConfiguration:
Status: Enabled
NotificationConfiguration:
TopicConfigurations:
- Event: "s3:ObjectCreated:Put"
Topic: !Ref S3CreateUpdateEventsTopic
Filter:
S3Key:
Rules:
- Name: "prefix"
Value: "stacks/"
- Name: "suffix"
Value: ".yaml"
- Event: "s3:ObjectCreated:Post"
Topic: !Ref S3CreateUpdateEventsTopic
Filter:
S3Key:
Rules:
- Name: "prefix"
Value: "stacks/"
- Name: "suffix"
Value: ".yaml"
- Event: "s3:ObjectCreated:CompleteMultipartUpload"
Topic: !Ref S3CreateUpdateEventsTopic
Filter:
S3Key:
Rules:
- Name: "prefix"
Value: "stacks/"
- Name: "suffix"
Value: ".yaml"
- Event: "s3:ObjectRemoved:DeleteMarkerCreated"
Topic: !Ref S3DeleteEventsTopic
Filter:
S3Key:
Rules:
- Name: "prefix"
Value: "stacks/"
- Name: "suffix"
Value: ".yaml"
CloudFormationSyncBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref CloudFormationSyncBucket
PolicyDocument:
Statement:
- Action:
- s3:ListBucket
- s3:ListBucketVersions
- s3:GetObject
- s3:GetObjectVersion
Effect: "Allow"
Principal:
AWS: !Ref AccountIds
Resource:
- !Sub "arn:aws:s3:::${AWS::StackName}-cfstack-bucket"
- !Sub "arn:aws:s3:::${AWS::StackName}-cfstack-bucket/*"
BuildStatusTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: Pipeline Status"
Subscription:
- Endpoint:
Fn::GetAtt:
- CodeBuildStatusFunction
- Arn
Protocol: lambda
S3FileEventsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: S3 File Events (Demuxed)"
DLQErrorsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: Errors"
Subscription:
- Endpoint:
Fn::GetAtt:
- SNSDLQErrorFunction
- Arn
Protocol: lambda
ErrorsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: Errors"
Subscription:
- Endpoint:
Fn::GetAtt:
- SNSErrorFunction
- Arn
Protocol: lambda
SlackNotificationErrorsTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: "CloudGenesis: Errors"
Subscription:
- Endpoint: !Ref EmailContact
Protocol: "email-json"
CloudFormationEventsTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: !Sub "${AWS::StackName}-CloudFormationEventsTopic"
Subscription:
- Endpoint:
Fn::GetAtt:
- CFNotifyFunction
- Arn
Protocol: lambda
CloudFormationEventsSNSPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
Topics:
- !Ref CloudFormationEventsTopic
PolicyDocument:
Id: S3TopicPolicy
Version: '2012-10-17'
Statement:
- Sid: "PublishEvents"
Effect: Allow
Principal:
AWS: !Ref AccountIds
Action: sns:Publish
Resource: !Ref CloudFormationEventsTopic
CodeBuildEventsSNSPolicy:
Type: AWS::SNS::TopicPolicy
Properties:
Topics:
- !Ref BuildStatusTopic
PolicyDocument:
Id: S3TopicPolicy
Version: '2012-10-17'
Statement:
- Sid: "PublishEvents"
Effect: Allow
Principal:
Service: "events.amazonaws.com"
Action: sns:Publish
Resource: !Ref BuildStatusTopic
#
# DEMUXER
#
LambdaDemuxerRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: "snsDLQAndErrors"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- sns:Publish
Resource:
- !Ref DLQErrorsTopic
- !Ref ErrorsTopic
- !Ref S3FileEventsTopic
- PolicyName: "xray-put-traces"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- xray:PutTraceSegments
- xray:PutTelemetryRecords
Resource: "*"
CreateUpdateEventDemuxer:
Type: AWS::Serverless::Function
Properties:
Runtime: java8
CodeUri: ./s3-event-demux/target/universal/s3-event-demux-1.0.zip
MemorySize: 512
Timeout: 300
Tracing: Active
DeadLetterQueue:
Type: SNS
TargetArn: !Ref DLQErrorsTopic
Environment:
Variables:
S3FILE_EVENTS_TOPIC_ARN: !Ref S3FileEventsTopic
SNS_ERROR_TOPIC_ARN: !Ref ErrorsTopic
Role: !GetAtt LambdaDemuxerRole.Arn
Handler: com.lifeway.cloudops.cloudformation.S3EventDemuxer::createUpdateHandler
Events:
CreateUpdateS3Event:
Type: SNS
Properties:
Topic: !Ref S3CreateUpdateEventsTopic
DeleteEventDemuxer:
Type: AWS::Serverless::Function
Properties:
Runtime: java8
CodeUri: ./s3-event-demux/target/universal/s3-event-demux-1.0.zip
MemorySize: 512
Timeout: 300
Tracing: Active
DeadLetterQueue:
Type: SNS
TargetArn: !Ref DLQErrorsTopic
Environment:
Variables:
S3FILE_EVENTS_TOPIC_ARN: !Ref S3FileEventsTopic
SNS_ERROR_TOPIC_ARN: !Ref ErrorsTopic
Role: !GetAtt LambdaDemuxerRole.Arn
Handler: com.lifeway.cloudops.cloudformation.S3EventDemuxer::deleteHandler
Events:
CreateUpdateS3Event:
Type: SNS
Properties:
Topic: !Ref S3DeleteEventsTopic
LambdaStackMgmtRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
Policies:
- PolicyName: "s3-ro-access"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:ListBucket
- s3:ListBucketVersions
- s3:GetObject
- s3:GetObjectVersion
Resource:
- !Sub "arn:aws:s3:::${AWS::StackName}-cfstack-bucket"
- !Sub "arn:aws:s3:::${AWS::StackName}-cfstack-bucket/*"
#Rules for each bucket that a CloudGenesis Deployer is also allowed to deploy templates from.
- !If
- HasExternalS3BucketSupport
- Effect: Allow
Action:
- s3:GetObject
- s3:GetObjectVersion
Resource: !Ref ExternalS3BucketPathArns
- !Ref "AWS::NoValue"
- PolicyName: "ssm-access"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- ssm:GetParameter
Resource: !Sub "arn:aws:ssm:*:*:parameter${SSMPath}*"
- PolicyName: "cross-account-cloudformation"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- sts:AssumeRole
Resource:
- !Sub "arn:aws:iam::*:role/${CFDeployerRoleName}"
- PolicyName: "snsDLQAndErrors"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- sns:Publish
Resource:
- !Ref ErrorsTopic
- PolicyName: "xray-put-traces"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- xray:PutTraceSegments
- xray:PutTelemetryRecords
Resource: "*"
- !If
- HasExternalNotificationSNS
- PolicyName: "external-sns-publish"
PolicyDocument:
Statement:
- Effect: Allow
Action:
- sns:Publish
Resource:
- !Ref ExternalNotificationSNSArn
- !Ref "AWS::NoValue"
S3FileEventHandler:
Type: AWS::Serverless::Function
Properties:
Runtime: java8
CodeUri: ./s3-event-handlers/target/universal/s3-event-handlers-1.0.zip
MemorySize: 1536
Timeout: 300
Tracing: Active
ReservedConcurrentExecutions: !Ref S3FileEventHandlerReservedConcurrency
DeadLetterQueue:
Type: SNS
TargetArn: !Ref ErrorsTopic # DLQ should be rarely thrown, msg won't be pretty formatted at this handler, but it will print
Environment:
Variables:
CF_EVENTS_TOPIC_NAME: !Sub "${AWS::StackName}-CloudFormationEventsTopic"
CLOUDGENESIS_ACCOUNT_ID: !Sub "${AWS::AccountId}"
IAM_CAPABILITIES_ENABLED: !Ref AllowIAMCapabilities
AUTO_EXPAND_CAPABILITY_ENABLED: !Ref AllowAutoExpandCapability
SEMANTIC_STACK_NAMING: !Ref SemanticStackNaming
IAM_ASSUME_ROLE_NAME: !Ref CFDeployerRoleName
SNS_ERROR_TOPIC_ARN: !Ref ErrorsTopic
IAM_CF_SERVICE_ROLE_NAME: !If [HasCFServiceRole, !Ref CFServiceRoleName, !Ref "AWS::NoValue"]
SNS_EXTERNAL_TOPIC_NOTIFY_ARN: !If [HasExternalNotificationSNS, !Ref ExternalNotificationSNSArn, !Ref "AWS::NoValue"]
CF_CHANGE_SET_NAME_PREFIX: !If [HasStackChageSetPrefix, !Ref StackChangeSetPrefix, !Ref "AWS::NoValue"]
TRACKING_TAG_PREFIX: !If [HasTrackingTagPrefix, !Ref TrackingTagPrefix, !Ref "AWS::NoValue"]
Role: !GetAtt LambdaStackMgmtRole.Arn
Handler: com.lifeway.cloudops.cloudformation.LambdaStackHandler::handler
Events:
DemuxedS3FileEvent:
Type: SNS
Properties:
Topic: !Ref S3FileEventsTopic
CFNotifyRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: CFNotifyPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: arn:aws:logs:*:*:*
- Effect: Allow
Action:
- sns:Publish
Resource:
- !Ref SlackNotificationErrorsTopic
- Effect: Allow
Action:
- cloudformation:DescribeStackResources
Resource: '*'
CFNotifyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "${AWS::StackName}-CFEventsNotification"
Description: Lambda function to post CF updates to Slack
Handler: lambda_notify.lambda_handler
Role: !GetAtt CFNotifyRole.Arn
CodeUri: ./cf-notify/cf-notify.zip
Runtime: python3.9
Timeout: 300
DeadLetterQueue:
Type: SNS
TargetArn: !Ref SlackNotificationErrorsTopic
Environment:
Variables:
WEBHOOK: !Ref SlackWebHookUrl
CHANNEL: !Ref SlackChannel
#
# This is painful, but the AWS Lambda Permission source arn pattern won't let you use wildcards. The arn has to be
# explicit - as a result, in order to allow SNS topics within the same account as the deployer to invoke CF Notify
# lambda, we have to grant an explicit perm to every possible region for the SNS topic that would be created in that
# region.
#
#US Regions
CFNotifyInvokePermissionUsEast1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:us-east-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionUsEast2:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:us-east-2:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionUsWest1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:us-west-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionUsWest2:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:us-west-2:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
#EU Regions
CFNotifyInvokePermissionEuWest1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:eu-west-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionEuWest2:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:eu-west-2:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionEuWest3:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:eu-west-3:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionEuCentral1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:eu-central-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
#AP Regions
CFNotifyInvokePermissionApSouth1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ap-south-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionApSouthEast1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ap-southeast-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionApSouthEast2:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ap-southeast-2:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionApNorthEast1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ap-northeast-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionApNorthEast2:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ap-northeast-2:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
CFNotifyInvokePermissionApNorthEast3:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ap-northeast-3:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
#South America Regions
CFNotifyInvokePermissionApSaEast1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:sa-east-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
#Canada Regions
CFNotifyInvokePermissionCaCentral1:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CFNotifyFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn: !Sub "arn:aws:sns:ca-central-1:${AWS::AccountId}:${AWS::StackName}-CloudFormationEventsTopic"
SNSDLQErrorFunction:
Type: AWS::Serverless::Function
Properties:
Description: Lambda function to post SNS DLQ errors to Slack
Handler: sns_error.dlq_lambda_handler
Role: !GetAtt CFNotifyRole.Arn
CodeUri: ./sns-notify/sns-error.zip
Runtime: python3.9
Timeout: 300
Environment:
Variables:
WEBHOOK: !Ref SlackWebHookUrl
CHANNEL: !Ref SlackChannel
DeadLetterQueue:
Type: SNS
TargetArn: !Ref SlackNotificationErrorsTopic
SNSDLQErrorInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: SNSDLQErrorFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn:
Ref: DLQErrorsTopic
SNSErrorFunction:
Type: AWS::Serverless::Function
Properties:
Description: Lambda function to post errors to Slack from SNS topic
Handler: sns_error.error_lambda_handler
Role: !GetAtt CFNotifyRole.Arn
CodeUri: ./sns-notify/sns-error.zip
Runtime: python3.9
Timeout: 300
Environment:
Variables:
WEBHOOK: !Ref SlackWebHookUrl
CHANNEL: !Ref SlackChannel
DeadLetterQueue:
Type: SNS
TargetArn: !Ref SlackNotificationErrorsTopic
SNSErrorInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: SNSErrorFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn:
Ref: ErrorsTopic
CodeBuildStatusFunction:
Type: AWS::Serverless::Function
Properties:
Description: Lambda function to post CodeBuild updates to Slack
Handler: index.handler
Role: !GetAtt CFNotifyRole.Arn
CodeUri: ./codebuild-notify/
Runtime: nodejs14.x
Timeout: 300
Environment:
Variables:
WEBHOOK: !Ref SlackWebHookUrl
CHANNEL: !Ref SlackChannel
DeadLetterQueue:
Type: SNS
TargetArn: !Ref SlackNotificationErrorsTopic
CodeBuildErrorInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName:
Ref: CodeBuildStatusFunction
Action: lambda:InvokeFunction
Principal: sns.amazonaws.com
SourceArn:
Ref: BuildStatusTopic
#
# CODE PIPELINE & PR BUILDER
#
CodePipelineBuildArtifactsBucket:
Type: AWS::S3::Bucket
PipelineExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- codepipeline.amazonaws.com
Path: /
Policies:
- PolicyName: CodePipelineAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'codebuild:StartBuild'
- 'codebuild:BatchGetBuilds'
Effect: Allow
Resource:
- !GetAtt CodeBuildProject.Arn
- Action:
- 's3:*'
Effect: Allow
Resource:
- !Sub 'arn:aws:s3:::${CodePipelineBuildArtifactsBucket}/*'
- Action:
- 'iam:PassRole'
Effect: Allow
Resource:
- !GetAtt CodeBuildServiceRole.Arn
CodeBuildServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Policies:
- PolicyName: CodeBuildAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Resource: "*"
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
- Effect: Allow
Resource:
- !Join
- ""
- - "arn:aws:s3:::"
- !Ref CloudFormationSyncBucket
- "/*"
- !GetAtt CloudFormationSyncBucket.Arn
Action:
- 's3:PutObject'
- 's3:DeleteObject'
- 's3:GetObject'
- 's3:ListBucket'
- Effect: Allow
Resource:
- !Sub 'arn:aws:s3:::${CodePipelineBuildArtifactsBucket}/*'
Action:
- 's3:GetObject'
- 's3:GetObjectVersion'
- 's3:PutObject'
- Effect: Allow
Action:
- 'cloudformation:ValidateTemplate'
Resource: "*"
#
# CODE BUILD
#
CodeBuildPRBuilder:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub '${AWS::StackName}-build-pr'
Description: !Sub '${AWS::StackName}-pr-build'
Artifacts:
Type: NO_ARTIFACTS
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: !Sub "${AWS::AccountId}.dkr.ecr.us-east-1.amazonaws.com/${ECRCodeBuildRepository}"
EnvironmentVariables:
- Name: S3_BUCKET_NAME
Value: !Ref CloudFormationSyncBucket
Type: PLAINTEXT
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
Type: GITHUB
Auth:
Type: OAUTH
BuildSpec: buildspec-pr.yml
GitCloneDepth: 5
Location: !Sub "https://github.com/${GithubRepoOwner}/${GithubRepoName}.git"
Triggers:
Webhook: true
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: !Sub '${AWS::StackName}-build'
Description: !Sub '${AWS::StackName}-pipeline-build'
Artifacts:
Type: CODEPIPELINE
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: !Sub "${AWS::AccountId}.dkr.ecr.us-east-1.amazonaws.com/${ECRCodeBuildRepository}"
EnvironmentVariables:
- Name: S3_BUCKET_NAME
Value: !Ref CloudFormationSyncBucket
Type: PLAINTEXT
ServiceRole: !GetAtt CodeBuildServiceRole.Arn
Source:
Type: CODEPIPELINE
BuildSpec: buildspec-sync.yml
EventRule:
Type: "AWS::Events::Rule"
Properties:
EventPattern:
source:
- "aws.codebuild"
detail-type:
- "CodeBuild Build State Change"
detail:
build-status: