-
Notifications
You must be signed in to change notification settings - Fork 55
/
automated-ui-testing.yaml
1029 lines (986 loc) · 35.1 KB
/
automated-ui-testing.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
Description: >
Template which creates automated UI testing using Selenium with AWS CodeBuild and AWS CodePipeline.
Parameters:
SourceRepo:
Description: Enter the name of CodeCommit source repository
Type: String
ApprovalTopic:
Description: Enter the name of the SNS topic to which approval notification should be sent
Type: String
ProductionBucket:
Description: Enter the name of the production S3 bucket
Type: String
Resources:
TestWebsite:
Type: "AWS::S3::Bucket"
DeletionPolicy: Retain
Properties:
AccessControl: Private
BucketName:
Fn::Sub: test-website-${AWS::StackName}
WebsiteConfiguration:
IndexDocument: index.html
Tags:
- Key: Name
Value:
Fn::Sub: TestWebsite-${AWS::StackName}
StatusWebsite:
Type: "AWS::S3::Bucket"
DeletionPolicy: Retain
Properties:
AccessControl: Private
BucketName:
Fn::Sub: status-website-${AWS::StackName}
WebsiteConfiguration:
IndexDocument: index.html
Tags:
- Key: Name
Value:
Fn::Sub: StatusWebsite-${AWS::StackName}
TestOutput:
Type: "AWS::S3::Bucket"
DeletionPolicy: Retain
Properties:
AccessControl: Private
BucketName:
Fn::Sub: test-output-${AWS::StackName}
Tags:
- Key: Name
Value:
Fn::Sub: TestOutput-${AWS::StackName}
CodePipelineArtifact:
Type: "AWS::S3::Bucket"
DeletionPolicy: Retain
Properties:
AccessControl: Private
BucketName:
Fn::Sub: codepipeline-artifact-${AWS::StackName}
Tags:
- Key: Name
Value:
Fn::Sub: CodePipelineArtifact-${AWS::StackName}
ModuleTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
-
AttributeName: "module"
AttributeType: "S"
KeySchema:
-
AttributeName: "module"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
TableName:
Fn::Sub: ModuleTable-${AWS::StackName}
Tags:
- Key: Name
Value:
Fn::Sub: ModuleTable-${AWS::StackName}
StatusTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
-
AttributeName: "module"
AttributeType: "S"
-
AttributeName: "testcaseid"
AttributeType: "S"
KeySchema:
-
AttributeName: "module"
KeyType: "HASH"
-
AttributeName: "testcaseid"
KeyType: "RANGE"
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
TableName:
Fn::Sub: StatusTable-${AWS::StackName}
Tags:
- Key: Name
Value:
Fn::Sub: StatusTable-${AWS::StackName}
StatusPageCognitoIP:
Type: "AWS::Cognito::IdentityPool"
Properties:
IdentityPoolName:
Fn::Sub: StatusPageCognitoIP_${AWS::StackName}
AllowUnauthenticatedIdentities: true
StatusPageUnAuthRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: StatusPageUnAuthRole-${AWS::StackName}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Federated:
- "cognito-identity.amazonaws.com"
Action:
- "sts:AssumeRoleWithWebIdentity"
Condition:
StringEquals:
"cognito-identity.amazonaws.com:aud":
Ref: StatusPageCognitoIP
ForAnyValue:StringLike:
"cognito-identity.amazonaws.com:amr": unauthenticated
Path: /
Policies:
-
PolicyName:
Fn::Sub: StatusPageUnAuthRole-${AWS::StackName}-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "dynamodb:Scan"
Resource:
- Fn::GetAtt: [ ModuleTable, Arn ]
- Fn::GetAtt: [ StatusTable, Arn ]
AttachCognitoUnAuthRole:
Type: "AWS::Cognito::IdentityPoolRoleAttachment"
Properties:
IdentityPoolId:
Ref: StatusPageCognitoIP
Roles:
unauthenticated:
Fn::GetAtt: [ StatusPageUnAuthRole, Arn ]
CodeBuildServiceRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: CodeBuildServiceRole-${AWS::StackName}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "codebuild.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /service-role/
Policies:
-
PolicyName:
Fn::Sub: CodeBuildServiceRole-${AWS::StackName}-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "codecommit:ListBranches"
- "codecommit:ListRepositories"
- "codecommit:BatchGetRepositories"
- "codecommit:Get*"
- "codecommit:GitPull"
Resource:
- Fn::Sub: arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${SourceRepo}
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/codebuild/*
-
Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
- "s3:ListBucket"
Resource:
- Fn::Sub: arn:aws:s3:::test-website-${AWS::StackName}/*
- Fn::Sub: arn:aws:s3:::test-website-${AWS::StackName}
- Fn::Sub: arn:aws:s3:::codepipeline-artifact-${AWS::StackName}/*
- Fn::Sub: arn:aws:s3:::codepipeline-artifact-${AWS::StackName}
- Fn::Sub: arn:aws:s3:::codepipeline-${AWS::Region}-*
-
Effect: "Allow"
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:UpdateItem"
Resource:
- Fn::GetAtt: [ ModuleTable, Arn ]
- Fn::GetAtt: [ StatusTable, Arn ]
-
Effect: "Allow"
Action:
- "lambda:CreateFunction"
- "lambda:GetFunction"
- "lambda:InvokeFunction"
- "lambda:ListFunctions"
- "lambda:UpdateFunctionCode"
- "lambda:UpdateFunctionConfiguration"
- "iam:PassRole"
Resource:
- "*"
S3UploadLambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: S3UploadLambdaRole-${AWS::StackName}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /
Policies:
-
PolicyName:
Fn::Sub: S3UploadLambdaRole-${AWS::StackName}-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
-
Effect: "Allow"
Action:
- "s3:PutObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
- "s3:ListBucket"
- "s3:PutObjectAcl"
Resource:
- Fn::Sub: arn:aws:s3:::test-website-${AWS::StackName}/*
- Fn::Sub: arn:aws:s3:::test-website-${AWS::StackName}
- Fn::Sub: arn:aws:s3:::status-website-${AWS::StackName}/*
- Fn::Sub: arn:aws:s3:::status-website-${AWS::StackName}
- Fn::Sub: arn:aws:s3:::codepipeline-artifact-${AWS::StackName}/*
- Fn::Sub: arn:aws:s3:::codepipeline-artifact-${AWS::StackName}
- Fn::Sub: arn:aws:s3:::codepipeline-${AWS::Region}-*
- Fn::Sub: arn:aws:s3:::${ProductionBucket}
- Fn::Sub: arn:aws:s3:::${ProductionBucket}/*
-
Effect: "Allow"
Action:
- "codepipeline:PutApprovalResult"
- "codepipeline:PutJobFailureResult"
- "codepipeline:PutJobSuccessResult"
Resource:
- "*"
PhatomJSLambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: PhatomJSLambdaRole-${AWS::StackName}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /
Policies:
-
PolicyName:
Fn::Sub: PhatomJSLambdaRole-${AWS::StackName}-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
-
Effect: "Allow"
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:UpdateItem"
Resource:
- Fn::GetAtt: [ StatusTable, Arn ]
BuildSpecLambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName:
Fn::Sub: BuildSpecLambdaRole-${AWS::StackName}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /
Policies:
-
PolicyName:
Fn::Sub: BuildSpecLambdaRole-${AWS::StackName}-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- Fn::Sub: arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*
-
Effect: "Allow"
Action:
- "codebuild:UpdateProject"
Resource:
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/TestPhantomJSProject-${AWS::StackName}
CodePipelineRole:
Type: "AWS::IAM::Role"
Properties:
RoleName:
Fn::Sub: CodePipelineRole-${AWS::StackName}
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Principal:
Service:
- "codepipeline.amazonaws.com"
Action:
- "sts:AssumeRole"
Path: /
Policies:
-
PolicyName:
Fn::Sub: CodePipelineRole-${AWS::StackName}-Policy
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: "Allow"
Action:
- "s3:DeleteObject"
- "s3:GetObject"
- "s3:GetObjectVersion"
- "s3:ListBucket"
- "s3:PutObject"
- "s3:GetBucketPolicy"
Resource:
- Fn::Sub: arn:aws:s3:::codepipeline-artifact-${AWS::StackName}
- Fn::Sub: arn:aws:s3:::codepipeline-artifact-${AWS::StackName}/*
-
Effect: "Allow"
Action:
- "codecommit:ListBranches"
- "codecommit:ListRepositories"
- "codecommit:BatchGetRepositories"
- "codecommit:Get*"
- "codecommit:GitPull"
- "codecommit:UploadArchive"
Resource:
- Fn::Sub: arn:aws:codecommit:${AWS::Region}:${AWS::AccountId}:${SourceRepo}
-
Effect: "Allow"
Action:
- "codebuild:StartBuild"
- "codebuild:BatchGetBuilds"
Resource:
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/TestChromeProject-${AWS::StackName}
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/TestFirefoxProject-${AWS::StackName}
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:project/TestPhantomJSProject-${AWS::StackName}
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:build/TestChromeProject-${AWS::StackName}:*
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:build/TestFirefoxProject-${AWS::StackName}:*
- Fn::Sub: arn:aws:codebuild:${AWS::Region}:${AWS::AccountId}:build/TestPhantomJSProject-${AWS::StackName}:*
-
Effect: "Allow"
Action:
- "lambda:GetAlias"
- "lambda:GetFunction"
- "lambda:InvokeAsync"
- "lambda:InvokeFunction"
- "lambda:ListAliases"
- "lambda:ListVersionsByFunction"
Resource:
- Fn::Sub: arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:*
-
Effect: "Allow"
Action:
- "lambda:ListFunctions"
Resource:
- "*"
-
Effect: "Allow"
Action:
- "sns:Publish"
Resource:
- Fn::Sub: arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${ApprovalTopic}
AmazonCloudWatchEventRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Principal:
Service:
- events.amazonaws.com
Action: sts:AssumeRole
Path: /
Policies:
-
PolicyName: cwe-pipeline-execution
PolicyDocument:
Version: "2012-10-17"
Statement:
-
Effect: Allow
Action: codepipeline:StartPipelineExecution
Resource: !Join [ '', [ 'arn:aws:codepipeline:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref AutomatedUITestPipeline ] ]
AmazonCloudWatchEventRule:
Type: AWS::Events::Rule
Properties:
EventPattern:
source:
- aws.codecommit
detail-type:
- 'CodeCommit Repository State Change'
resources:
- !Join [ '', [ 'arn:aws:codecommit:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref SourceRepo ] ]
detail:
event:
- referenceCreated
- referenceUpdated
referenceType:
- branch
referenceName:
- master
Targets:
-
Arn:
!Join [ '', [ 'arn:aws:codepipeline:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref AutomatedUITestPipeline ] ]
RoleArn: !GetAtt AmazonCloudWatchEventRole.Arn
Id: codepipeline-AutomatedUITestPipeline
TestChromeProject:
Type: AWS::CodeBuild::Project
Properties:
Name:
Fn::Sub: TestChromeProject-${AWS::StackName}
Description: Run UI testing on Chrome browser
ServiceRole:
Fn::GetAtt: [ CodeBuildServiceRole, Arn ]
Artifacts:
Type: S3
Location:
Ref: CodePipelineArtifact
Name:
Fn::Sub: TestChromeProject-${AWS::StackName}
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: selenium/standalone-chrome
EnvironmentVariables:
- Name: BROWSER
Value: chrome
- Name: WebURL
Value:
Fn::GetAtt: [ TestWebsite, WebsiteURL ]
- Name: ArtifactBucket
Value:
Ref: TestWebsite
- Name: ModuleTable
Value:
Ref: ModuleTable
- Name: StatusTable
Value:
Ref: StatusTable
- Name: MODULES
Value: mod1,mod2,mod3
Source:
Location:
Fn::Sub: https://git-codecommit.${AWS::Region}.amazonaws.com/v1/repos/${SourceRepo}
Type: CODECOMMIT
TimeoutInMinutes: 15
EncryptionKey:
Fn::Sub: arn:aws:kms:${AWS::Region}:${AWS::AccountId}:alias/aws/s3
Tags:
- Key: Name
Value:
Fn::Sub: TestChromeProject-${AWS::StackName}
TestFirefoxProject:
Type: AWS::CodeBuild::Project
Properties:
Name:
Fn::Sub: TestFirefoxProject-${AWS::StackName}
Description: Run UI testing on Firefox browser
ServiceRole:
Fn::GetAtt: [ CodeBuildServiceRole, Arn ]
Artifacts:
Type: S3
Location:
Ref: CodePipelineArtifact
Name:
Fn::Sub: TestFirefoxProject-${AWS::StackName}
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: selenium/standalone-firefox
EnvironmentVariables:
- Name: BROWSER
Value: firefox
- Name: WebURL
Value:
Fn::GetAtt: [ TestWebsite, WebsiteURL ]
- Name: ArtifactBucket
Value:
Ref: TestWebsite
- Name: ModuleTable
Value:
Ref: ModuleTable
- Name: StatusTable
Value:
Ref: StatusTable
- Name: MODULES
Value: mod1,mod2,mod3
Source:
Location:
Fn::Sub: https://git-codecommit.${AWS::Region}.amazonaws.com/v1/repos/${SourceRepo}
Type: CODECOMMIT
TimeoutInMinutes: 15
EncryptionKey:
Fn::Sub: arn:aws:kms:${AWS::Region}:${AWS::AccountId}:alias/aws/s3
Tags:
- Key: Name
Value:
Fn::Sub: TestFirefoxProject-${AWS::StackName}
TestPhantomJSProject:
Type: AWS::CodeBuild::Project
Properties:
Name:
Fn::Sub: TestPhantomJSProject-${AWS::StackName}
Description: Run UI testing on PhantomJS
ServiceRole:
Fn::GetAtt: [ CodeBuildServiceRole, Arn ]
Artifacts:
Type: S3
Location:
Ref: CodePipelineArtifact
Name:
Fn::Sub: TestPhantomJSProject-${AWS::StackName}
Environment:
Type: LINUX_CONTAINER
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/python:2.7.12
EnvironmentVariables:
- Name: BROWSER
Value: phantomjs
- Name: WebURL
Value:
Fn::GetAtt: [ TestWebsite, WebsiteURL ]
- Name: ArtifactBucket
Value:
Ref: TestWebsite
- Name: ModuleTable
Value:
Ref: ModuleTable
- Name: StatusTable
Value:
Ref: StatusTable
- Name: MODULES
Value: mod1,mod2,mod3
- Name: LambdaRole
Value:
Fn::GetAtt: [ PhatomJSLambdaRole, Arn ]
Source:
Location:
Fn::Sub: https://git-codecommit.${AWS::Region}.amazonaws.com/v1/repos/${SourceRepo}
Type: CODECOMMIT
TimeoutInMinutes: 15
EncryptionKey:
Fn::Sub: arn:aws:kms:${AWS::Region}:${AWS::AccountId}:alias/aws/s3
Tags:
- Key: Name
Value:
Fn::Sub: TestPhantomJSProject-${AWS::StackName}
S3UploadFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.lambda_handler"
Role:
Fn::GetAtt:
- "S3UploadLambdaRole"
- "Arn"
Code:
ZipFile: |
from __future__ import print_function
from boto3.session import Session
from botocore.vendored import requests
import json
import boto3
import random
import string
import botocore
import traceback
import zipfile
import os.path
import os
print('Loading function')
code_pipeline = boto3.client('codepipeline')
def lambda_handler(event, context):
try:
if 'StackId' in event:
responseBody = {'Status': 'SUCCESS',
'Reason': context.log_stream_name,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': 'Created/Deleted function successfully'}
requests.put(event['ResponseURL'], data=json.dumps(responseBody))
print(event)
s3 = boto3.resource('s3')
rdmstring = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(32)])
tmp_file = '/tmp/' + rdmstring + '.zip'
job_id = event['CodePipeline.job']['id']
job_data = event['CodePipeline.job']['data']
artifacts = job_data['inputArtifacts']
bucket = artifacts[0]['location']['s3Location']['bucketName']
key = artifacts[0]['location']['s3Location']['objectKey']
user_parameters = job_data['actionConfiguration']['configuration']['UserParameters']
params = json.loads(user_parameters)
if 'bucket' not in params or 'path' not in params:
raise Exception('Your UserParameters JSON must include the both path and bucket name')
s3.meta.client.download_file(bucket, key, tmp_file)
with zipfile.ZipFile(tmp_file, 'r') as z:
z.extractall('/tmp/' + rdmstring)
filelist = []
for dirname, dirnames, filenames in os.walk('/tmp/' + rdmstring + '/' + params['path']):
for filename in filenames:
filelist.append(os.path.join(dirname, filename))
for f in filelist:
bn = os.path.basename(f)
ft = bn.split('.')[1].lower()
if ft == 'html':
s3.meta.client.upload_file(f, params['bucket'], bn, ExtraArgs={'ContentType': "text/html", 'ACL': "public-read"})
elif ft == 'js':
if bn == 'generatetable.js':
replacements = {'Status_Table': os.environ['StatusTable'], 'Modules_Table': os.environ['ModuleTable'], 'AWS_REGION': os.environ['REGION'], 'Cognito_IDP_ID': os.environ['Cognito_IDP_ID']}
lines = []
with open(f) as infile:
for line in infile:
for src, target in replacements.iteritems():
line = line.replace(src, target)
lines.append(line)
with open(f, 'w') as outfile:
for line in lines:
outfile.write(line)
s3.meta.client.upload_file(f, params['bucket'], bn, ExtraArgs={'ContentType': "application/x-javascript", 'ACL': "public-read"})
elif ft == 'png':
s3.meta.client.upload_file(f, params['bucket'], bn, ExtraArgs={'ContentType': "image/png", 'ACL': "public-read"})
else:
s3.meta.client.upload_file(f, params['bucket'], bn, ExtraArgs={'ACL': "public-read"})
code_pipeline.put_job_success_result(jobId=job_id)
except Exception as e:
print(e)
traceback.print_exc()
print('Putting job failure')
code_pipeline.put_job_failure_result(jobId=job_id,
failureDetails={'message': 'Function exception: ' + str(e),
'type': 'JobFailed'})
return "Complete."
Runtime: "python2.7"
Environment:
Variables:
ModuleTable:
Fn::Sub: ModuleTable-${AWS::StackName}
StatusTable:
Fn::Sub: StatusTable-${AWS::StackName}
Cognito_IDP_ID:
Ref: StatusPageCognitoIP
REGION:
Ref: AWS::Region
Timeout: 150
MemorySize: 256
Tags:
- Key: Name
Value:
Fn::Sub: S3UploadFunction-${AWS::StackName}
BuildSpecFunction:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.lambda_handler"
Role:
Fn::GetAtt:
- "BuildSpecLambdaRole"
- "Arn"
Code:
ZipFile: |
from botocore.vendored import requests
import json
import boto3
import botocore
import traceback
import os
def sendResponse(event, context, responseStatus, responseData):
responseBody = {'Status': responseStatus,
'Reason': 'See the details in CloudWatch Log Stream: ' + context.log_stream_name,
'PhysicalResourceId': context.log_stream_name,
'StackId': event['StackId'],
'RequestId': event['RequestId'],
'LogicalResourceId': event['LogicalResourceId'],
'Data': responseData}
print 'RESPONSE BODY:\n' + json.dumps(responseBody)
try:
req = requests.put(event['ResponseURL'], data=json.dumps(responseBody))
if req.status_code != 200:
print req.text
raise Exception('Recieved non 200 response while sending response to CFN.')
return
except requests.exceptions.RequestException as e:
print e
raise
def lambda_handler(event, context):
print event
try:
if event['RequestType'] == 'Delete':
sendResponse(event, context, 'SUCCESS', {})
return 0
cb = boto3.client('codebuild')
cb_res = cb.update_project(name=os.environ['CB_Name'], source={"type": "CODECOMMIT", "location": os.environ['CC_URL'], "buildspec": os.environ['BS_PATH']})
responseData = {'Success': 'Successfully updated CodeBuild project'}
sendResponse(event, context, 'SUCCESS', {})
except Exception as e:
print 'Received client error: %s' % e
responseData = {'Failed': 'Received client error: %s' % e}
sendResponse(event, context, 'FAILED', responseData)
Runtime: "python2.7"
Environment:
Variables:
CB_Name:
Ref: TestPhantomJSProject
CC_URL:
Fn::Sub: https://git-codecommit.${AWS::Region}.amazonaws.com/v1/repos/${SourceRepo}
BS_PATH: buildspec-phantomjs.yml
Timeout: 150
MemorySize: 256
Tags:
- Key: Name
Value:
Fn::Sub: BuildSpecFunction-${AWS::StackName}
UpdateBuildProject:
Type: Custom::UpdateBuildProject
Properties:
ServiceToken:
Fn::GetAtt: [ BuildSpecFunction, Arn ]
AutomatedUITestPipeline:
DependsOn: UpdateBuildProject
Type: "AWS::CodePipeline::Pipeline"
Properties:
Name:
Fn::Sub: AutomatedUITestPipeline-${AWS::StackName}
RoleArn:
Fn::GetAtt: [ CodePipelineRole, Arn ]
Stages:
-
Name: WebsiteSource
Actions:
-
Name: UITestSource
ActionTypeId:
Category: Source
Owner: AWS
Version: 1
Provider: CodeCommit
OutputArtifacts:
-
Name: UITestSourceOutput
Configuration:
PollForSourceChanges: False
BranchName: master
RepositoryName:
Ref: SourceRepo
RunOrder: 1
-
Name: UITest
Actions:
-
Name: DeployTestSite
InputArtifacts:
- Name: UITestSourceOutput
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
Configuration:
FunctionName:
Ref: S3UploadFunction
UserParameters:
Fn::Join: [
"",
[
"{\"bucket\":\"",
Ref: TestWebsite,
"\",\"path\":\"website\"}"
]]
OutputArtifacts:
-
Name: DeployedTestSite
RunOrder: 1
-
Name: DeployStatusPage
InputArtifacts:
- Name: UITestSourceOutput
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
Configuration:
FunctionName:
Ref: S3UploadFunction
UserParameters:
Fn::Join: [
"",
[
"{\"bucket\":\"",
Ref: StatusWebsite,
"\",\"path\":\"status\"}"
]]
OutputArtifacts:
-
Name: DeployedStatusPage
RunOrder: 1
-
Name: TestOnChrome
InputArtifacts:
- Name: UITestSourceOutput
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
-
Name: TestOnChromeArtifact
Configuration:
ProjectName:
Ref: TestChromeProject
RunOrder: 2
-
Name: TestOnFirefox
InputArtifacts:
- Name: UITestSourceOutput
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
-
Name: TestOnFirefoxArtifact
Configuration:
ProjectName:
Ref: TestFirefoxProject
RunOrder: 2
-
Name: TestOnPhantomJS
InputArtifacts:
- Name: UITestSourceOutput
ActionTypeId:
Category: Build
Owner: AWS
Version: 1
Provider: CodeBuild
OutputArtifacts:
-
Name: TestOnPhantomJSArtifact
Configuration:
ProjectName:
Ref: TestPhantomJSProject
RunOrder: 2
-
Name: Approval
Actions:
-
Name: DeployApproval
ActionTypeId:
Category: Approval
Owner: AWS
Version: 1
Provider: Manual
Configuration:
NotificationArn:
Fn::Sub: arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${ApprovalTopic}
CustomData: Approve once UI testing has been completed.
RunOrder: 1
-
Name: Production
Actions:
-
Name: DeployProduction
ActionTypeId:
Category: Invoke
Owner: AWS
Version: 1
Provider: Lambda
InputArtifacts:
- Name: UITestSourceOutput
Configuration:
FunctionName: