-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yaml
More file actions
197 lines (181 loc) · 5.66 KB
/
Copy pathtemplate.yaml
File metadata and controls
197 lines (181 loc) · 5.66 KB
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
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Serverless SPA website
A serverless website for SPA
Parameters:
WebACLId:
Type: String
Description: WAFArn
Domain:
Type: String
Description: Domain alias
AcmCertificateArn:
Type: String
Description: Certificate ARN
BucketName:
Type: String
Description: Bucket name
Metadata:
AWS::ServerlessRepo::Application:
Name: serverless-spa
Description: A serverless SPA container
Author: user1
SpdxLicenseId: MIT
LicenseUrl: LICENSE.txt
ReadmeUrl: README.md
Labels: ['SPA', 'Serverless', 'Cloudfront']
HomePageUrl: https://github.com/Ryanair/sam-spa
SemanticVersion: 0.0.1
SourceCodeUrl: https://github.com/Ryanair/sam-spa
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Conditions:
HasWebAcl: !Not [!Equals [!Ref WebACLId, '']]
HasAcm: !Not [!Equals [!Ref AcmCertificateArn, '']]
HasDomain: !Not [!Equals [!Ref Domain, '']]
Resources:
LambdaEdgeFunctionRole:
Type: "AWS::IAM::Role"
Properties:
Path: '/'
ManagedPolicyArns:
- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
-
Sid: "AllowLambdaServiceToAssumeRole"
Effect: "Allow"
Action:
- "sts:AssumeRole"
Principal:
Service:
- "lambda.amazonaws.com"
- "edgelambda.amazonaws.com"
RewriteLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: rewrite/
Description: 'Rewrite lambda for SPA'
Handler: app.lambdaHandler
Runtime: nodejs10.x
MemorySize: 128
Timeout: 1
Role: !GetAtt LambdaEdgeFunctionRole.Arn
AutoPublishAlias: live
SecureHeadersLambda:
Type: AWS::Serverless::Function
Properties:
CodeUri: secure-headers/
Description: 'Add security headers to index.html response'
Handler: app.lambdaHandler
Runtime: nodejs10.x
MemorySize: 128
Timeout: 1
Role: !GetAtt LambdaEdgeFunctionRole.Arn
AutoPublishAlias: live
CloudFrontOriginAccessIdentity:
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'Origin access identity serverless website'
CloudfrontDistribution:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
WebACLId:
!If
- HasWebAcl
- !Ref WebACLId
- !Ref AWS::NoValue
PriceClass: 'PriceClass_100' #If needed global replication you can remove it
Aliases:
!If
- HasDomain
- - !Ref Domain
- !Ref AWS::NoValue
Comment: "Cloudfront distribution for serverless website"
ViewerCertificate:
AcmCertificateArn:
!If
- HasAcm
- !Ref AcmCertificateArn
- !Ref AWS::NoValue
CloudFrontDefaultCertificate:
!If
- HasAcm
- 'false'
- 'true'
MinimumProtocolVersion:
!If
- HasAcm
- 'TLSv1.2_2018'
- !Ref AWS::NoValue
SslSupportMethod:
!If
- HasAcm
- 'sni-only'
- !Ref AWS::NoValue
DefaultRootObject: "index.html"
Enabled: 'true'
HttpVersion: 'http2'
Origins:
- Id: 's3-website'
DomainName: !Join ['.', [!Ref BucketName, 's3.amazonaws.com']]
S3OriginConfig:
OriginAccessIdentity:
Fn::Sub: 'origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}'
DefaultCacheBehavior:
Compress: 'true'
ForwardedValues:
QueryString: 'false'
TargetOriginId: 's3-website'
ViewerProtocolPolicy : "redirect-to-https"
LambdaFunctionAssociations:
- EventType: 'origin-request'
LambdaFunctionARN: !Ref RewriteLambda.Version
- EventType: 'viewer-response'
LambdaFunctionARN: !Ref SecureHeadersLambda.Version
CacheBehaviors:
- PathPattern: '/dist/*'
Compress: 'true'
TargetOriginId: 's3-website'
ViewerProtocolPolicy : "redirect-to-https"
ForwardedValues:
QueryString: 'false'
S3Bucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref BucketName
S3BucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref S3Bucket
PolicyDocument:
Statement:
-
Effect: Allow
Action: 's3:GetObject'
Resource:
- !Sub "arn:aws:s3:::${S3Bucket}/*"
Principal:
AWS: !Sub "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginAccessIdentity}"
Outputs:
RewriteLambdaEdge:
Description: Rewrite Lambda@Edge
Value: !GetAtt RewriteLambda.Arn
RewriteLambdaEdgeVersion:
Description: RewriteLambdaEdge version
Value: !Ref RewriteLambda.Version
SecureHeadersLambdaEdge:
Description: Secure headers Lambda@Edge
Value: !GetAtt SecureHeadersLambda.Arn
SecureHeadersLambdaEdgeVersion:
Description: SecureHeaders version
Value: !Ref SecureHeadersLambda.Version
CFDistribution:
Description: Cloudfront Distribution Domain Name
Value: !GetAtt CloudfrontDistribution.DomainName