-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambdaedge-cf.yml
187 lines (187 loc) · 6.85 KB
/
lambdaedge-cf.yml
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
Parameters:
S3BucketName:
Type: String
Resources:
WebBucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName:
Ref: S3BucketName
AccessControl: PublicRead
WebsiteConfiguration:
ErrorDocument: index.html
IndexDocument: index.html
WebBucketPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket:
Ref: S3BucketName
PolicyDocument:
Statement:
- Action:
- "s3:GetObject"
Effect: "Allow"
Principal: "*"
Resource:
Fn::Join:
- ""
- - "arn:aws:s3:::"
- Ref: S3BucketName
- "/*"
LambdaEdgeExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: logging
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Deny
Action:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
SetCustomHeaders:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.handler"
Role:
Fn::GetAtt:
- "LambdaEdgeExecutionRole"
- "Arn"
Code:
ZipFile: !Sub |
'use strict';
/* change the version number below whenever this code is modified */
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const user_agent = headers['user-agent'];
const host = headers['host'];
if (user_agent && host) {
var prerender = /googlebot|Mediapartners-Google|AdsBot-Google-Mobile|AdsBot-Google-Mobile|AdsBot-Google|Googlebot-Image|Googlebot-News|FeedFetcher-Google|Google-Read-Aloud|Google-Structured-Data-Testing-Tool|Google Page Speed Insights|Chrome-Lighthouse|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/i.test(user_agent[0].value);
prerender = prerender || /_escaped_fragment_/.test(request.querystring);
prerender = prerender && ! /\.(js|css|xml|less|png|jpg|jpeg|json|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)$/i.test(request.uri);
if (prerender) {
headers['x-prerender-host'] = [{ key: 'X-Prerender-Host', value: host[0].value}];
headers['x-prerender-cachebuster'] = [{ key: 'X-Prerender-Cachebuster', value: Date.now().toString()}];
}
}
callback(null, request);
};
Runtime: "nodejs18.x"
SetCustomHeadersVersion6:
Type: "AWS::Lambda::Version"
Properties:
FunctionName:
Ref: "SetCustomHeaders"
Description: "SetCustomHeaders"
RedirectToPrerender:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.handler"
Role:
Fn::GetAtt:
- "LambdaEdgeExecutionRole"
- "Arn"
Code:
ZipFile: |
'use strict';
/* change the version number below whenever this code is modified */
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (request.headers['x-prerender-host']) {
request.origin = {
custom: {
domainName: 'prerender.naadanchords.com',
port: 443,
protocol: 'https',
readTimeout: 20,
keepaliveTimeout: 5,
customHeaders: {},
sslProtocols: ['TLSv1', 'TLSv1.1'],
path: '/prerender?url=https%3A%2F%2F' + request.headers['x-prerender-host'][0].value
}
};
request.headers['host'] = [{ key: 'host', value: 'prerender.naadanchords.com'}];
}
callback(null, request);
};
Runtime: "nodejs18.x"
RedirectToPrerenderVersion4:
Type: "AWS::Lambda::Version"
Properties:
FunctionName:
Ref: "RedirectToPrerender"
Description: "RedirectToPrerender"
CloudFront:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
DefaultCacheBehavior:
Compress: true
# NOTE: we let cloudfront cache heavily the resurces of the SPA. Your deploy
# step will need to include an invalidation of the cloudfromt cache.
# The requests to prerender.naadanchords.com are NOT cached thanks to the X-Prerender-Cachebuster
# header.
MinTTL: 31536000
DefaultTTL: 31536000
ForwardedValues:
QueryString: true
Headers:
- "X-Prerender-Host"
- "X-Prerender-Cachebuster"
TargetOriginId: origin
ViewerProtocolPolicy: redirect-to-https
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN:
!Join [
":",
[
!GetAtt [SetCustomHeaders, Arn],
!GetAtt [SetCustomHeadersVersion6, Version],
],
]
- EventType: origin-request
LambdaFunctionARN:
!Join [
":",
[
!GetAtt [RedirectToPrerender, Arn],
!GetAtt [RedirectToPrerenderVersion4, Version],
],
]
Enabled: true
CustomErrorResponses:
- ErrorCode: 404
ResponseCode: 200
ResponsePagePath: /index.html
- ErrorCode: 501
ResponseCode: 404
ResponsePagePath: /index.html
Aliases:
- "www.naadanchords.com"
ViewerCertificate:
AcmCertificateArn: "arn:aws:acm:us-east-1:608318364761:certificate/2460dc04-8a28-4e30-aa3c-d9e0b63a52e0"
MinimumProtocolVersion: "TLSv1.1_2016"
SslSupportMethod: "sni-only"
HttpVersion: http2
Origins:
- CustomOriginConfig:
OriginProtocolPolicy: http-only
DomainName:
!Select [2, !Split ["/", !GetAtt [WebBucket, WebsiteURL]]]
Id: origin
PriceClass: PriceClass_All