-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra.yaml
338 lines (320 loc) · 11.8 KB
/
infra.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
Parameters:
BranchName:
Type: String
Description: The name of the branch being deployed
LimulusDotNetZoneId:
Type: String
Description: The Route 53 hosted zone ID for limulus.net
LimulusDotNetCertificateArn:
Type: String
Description: The ARN of the certificate for limulus.net (and www and *)
Outputs:
StaticSiteBucketName:
Description: 'Name of the static site S3 bucket'
Value: !Ref StaticSiteBucket
CloudFrontDistributionLogsBucketName:
Description: 'Name of the CloudFront distribution logs S3 bucket'
Value: !Ref CloudFrontDistributionLogsBucket
Conditions:
IsMainBranch:
Fn::Equals:
- !Ref BranchName
- 'main'
Resources:
CloudFrontDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Comment: !Sub 'limulus-dot-net ${BranchName}'
Aliases: !If
- IsMainBranch
- - 'limulus.net'
- 'www.limulus.net'
- - !Sub '${BranchName}.limulus.net'
CacheBehaviors:
- PathPattern: /.well-known/webfinger
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
Compress: true
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3
TargetOriginId: mastodon.limulus.net
ViewerProtocolPolicy: redirect-to-https
- PathPattern: /.well-known/host-meta
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
Compress: true
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3
TargetOriginId: mastodon.limulus.net
ViewerProtocolPolicy: redirect-to-https
- PathPattern: /.well-known/nodeinfo
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad
Compress: true
OriginRequestPolicyId: 216adef6-5c7f-47e4-b989-5492eafa07d3
TargetOriginId: mastodon.limulus.net
ViewerProtocolPolicy: redirect-to-https
- PathPattern: /assets/immutable/*
CachePolicyId: !Ref CloudFrontDistributionForeverCachePolicy
Compress: true
OriginRequestPolicyId: 88a5eaf4-2fd4-4709-b370-b4c650ea3fcf
TargetOriginId: static-site-bucket-origin
ResponseHeadersPolicyId: !Ref CloudFrontImmutableResponseHeadersPolicy
ViewerProtocolPolicy: redirect-to-https
- PathPattern: /*.????????.*
CachePolicyId: !Ref CloudFrontDistributionForeverCachePolicy
Compress: true
OriginRequestPolicyId: 88a5eaf4-2fd4-4709-b370-b4c650ea3fcf
TargetOriginId: static-site-bucket-origin
ResponseHeadersPolicyId: !Ref CloudFrontImmutableResponseHeadersPolicy
ViewerProtocolPolicy: redirect-to-https
CustomErrorResponses:
- ErrorCachingMinTTL: 30
ErrorCode: 404
ResponseCode: 404
ResponsePagePath: /404/index.html
DefaultCacheBehavior:
CachePolicyId: !Ref CloudFrontDistributionDefaultCachePolicy
Compress: true
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt CloudFrontRequestFunction.FunctionMetadata.FunctionARN
MaxTTL: 31536000
MinTTL: 0
OriginRequestPolicyId: 88a5eaf4-2fd4-4709-b370-b4c650ea3fcf
ResponseHeadersPolicyId: !Ref CloudFrontResponseHeadersPolicy
TargetOriginId: static-site-bucket-origin
ViewerProtocolPolicy: redirect-to-https
DefaultRootObject: index.html
Enabled: true
HttpVersion: http2and3
IPV6Enabled: true
Logging:
Bucket: !GetAtt CloudFrontDistributionLogsBucket.DomainName
IncludeCookies: false
Prefix: 'limulus-dot-net/'
Origins:
- Id: static-site-bucket-origin
DomainName: !GetAtt StaticSiteBucket.RegionalDomainName
OriginAccessControlId: !Ref StaticSiteBucketOriginAccessControl
S3OriginConfig:
OriginAccessIdentity: ''
- Id: mastodon.limulus.net
DomainName: mastodon.limulus.net
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: https-only
PriceClass: PriceClass_All
ViewerCertificate:
AcmCertificateArn: !Ref LimulusDotNetCertificateArn
MinimumProtocolVersion: TLSv1.2_2021
SslSupportMethod: sni-only
CloudFrontDistributionDefaultCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: !Sub 'limulus-dot-net-cache-${BranchName}'
Comment: Default cache policy for limulus.net
DefaultTTL: 60
MaxTTL: 31536000
MinTTL: 0
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingGzip: true
EnableAcceptEncodingBrotli: true
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: none
CloudFrontDistributionForeverCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: !Sub 'limulus-dot-net-forever-cache-${BranchName}'
Comment: Cache policy for immutable assets
DefaultTTL: 31536000
MaxTTL: 31536000
MinTTL: 31536000
ParametersInCacheKeyAndForwardedToOrigin:
CookiesConfig:
CookieBehavior: none
EnableAcceptEncodingGzip: true
EnableAcceptEncodingBrotli: true
HeadersConfig:
HeaderBehavior: none
QueryStringsConfig:
QueryStringBehavior: none
CloudFrontDistributionLogsBucket:
Type: AWS::S3::Bucket
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
Properties:
BucketEncryption:
ServerSideEncryptionConfiguration:
- BucketKeyEnabled: true
ServerSideEncryptionByDefault:
SSEAlgorithm: AES256
LifecycleConfiguration:
Rules:
- Id: MoveToInfrequentAccess
Status: Enabled
Transitions:
- StorageClass: STANDARD_IA
TransitionInDays: 30
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
CloudFrontResponseHeadersPolicy:
Type: AWS::CloudFront::ResponseHeadersPolicy
Properties:
ResponseHeadersPolicyConfig:
Name: !Sub 'limulus-dot-net-response-headers-${BranchName}'
Comment: Add headers to allow fine grained timers in JS runtime
CustomHeadersConfig:
Items:
- Header: Cross-Origin-Opener-Policy
Override: false
Value: same-origin
- Header: Cross-Origin-Embedder-Policy
Override: false
Value: require-corp
CloudFrontImmutableResponseHeadersPolicy:
Type: AWS::CloudFront::ResponseHeadersPolicy
Properties:
ResponseHeadersPolicyConfig:
Name: !Sub 'limulus-dot-net-immutable-response-headers-${BranchName}'
Comment: Add headers to cache resources forever
CustomHeadersConfig:
Items:
- Header: Cache-Control
Override: true
Value: 'public, max-age=31536000, immutable'
- Header: Cross-Origin-Opener-Policy
Override: false
Value: same-origin
- Header: Cross-Origin-Embedder-Policy
Override: false
Value: require-corp
CloudFrontRequestFunction:
Type: AWS::CloudFront::Function
Properties:
Name: !Sub 'limulus-dot-net-rewrite-${BranchName}'
AutoPublish: true
FunctionCode: !Sub |
const needsIndexHtmlRe = /(\/[^\/.]+)\/*$/
const penumbraJournalRedirectRe = /^\/penumbra\/journal\/00[1-5]-/
const mastodonProfileRedirectRe = /^\/@eric\/?/
function handler(event) {
const request = event.request
const host = request.headers.host
if (host && host.value === 'www.limulus.net') {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
location: {
value: 'https://limulus.net' + request.uri
}
}
}
}
if (penumbraJournalRedirectRe.test(request.uri)) {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
location: {
value: request.uri.replace(/00[1-5]-/, ''),
},
},
}
}
if (mastodonProfileRedirectRe.test(request.uri)) {
return {
statusCode: 302,
statusDescription: 'Found',
headers: {
location: {
value: 'https://mastodon.limulus.net' + request.uri
}
}
}
}
request.uri = request.uri.replace(needsIndexHtmlRe, '$1/index.html')
return request
}
FunctionConfig:
Comment: Default request handler
Runtime: cloudfront-js-2.0
Route53RecordSetGroup:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneId: !Ref LimulusDotNetZoneId
RecordSets: !If
- IsMainBranch
- - Name: 'limulus.net'
Type: A
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
- Name: 'limulus.net'
Type: AAAA
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
- Name: 'www.limulus.net'
Type: A
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
- Name: 'www.limulus.net'
Type: AAAA
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
- - Name: !Sub '${BranchName}.limulus.net'
Type: A
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
- Name: !Sub '${BranchName}.limulus.net'
Type: AAAA
AliasTarget:
DNSName: !GetAtt CloudFrontDistribution.DomainName
HostedZoneId: Z2FDTNDATAQYW2
StaticSiteBucket:
Type: AWS::S3::Bucket
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
StaticSiteBucketOriginAccessControl:
Type: AWS::CloudFront::OriginAccessControl
Properties:
OriginAccessControlConfig:
Name: !Sub 'limulus-dot-net-oac-${BranchName}'
OriginAccessControlOriginType: s3
SigningBehavior: always
SigningProtocol: sigv4
StaticSiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref StaticSiteBucket
PolicyDocument:
Id: PolicyForCloudFrontPrivateContent
Version: '2008-10-17'
Statement:
- Sid: AllowCloudFrontGetObject
Condition:
StringEquals:
AWS:SourceArn: !Sub 'arn:aws:cloudfront::${AWS::AccountId}:distribution/${CloudFrontDistribution.Id}'
Resource: !Sub '${StaticSiteBucket.Arn}/*'
Action: s3:GetObject
Effect: Allow
Principal:
Service: cloudfront.amazonaws.com
- Sid: AllowCloudFrontListBucket
Condition:
StringEquals:
AWS:SourceArn: !Sub 'arn:aws:cloudfront::${AWS::AccountId}:distribution/${CloudFrontDistribution.Id}'
Resource: !Sub '${StaticSiteBucket.Arn}'
Action: s3:ListBucket
Effect: Allow
Principal:
Service: cloudfront.amazonaws.com