-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.yml
149 lines (137 loc) · 4.02 KB
/
serverless.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
service: 'demo-app-serverless-starter' #TODO: change mes
frameworkVersion: "3"
plugins:
- '@silvermine/serverless-plugin-cloudfront-lambda-edge'
- serverless-s3-deploy
provider:
name: aws
runtime: nodejs18.x
lambdaHashingVersion: 20201221
region: us-east-1 #Lambda@Edge must be deployed in us-east-1
stage: ${opt:stage, 'dev'}
package:
individually: true
exclude:
- ./**
include:
- build/server/**
- build/edge/**
custom:
assets:
auto: true
targets:
- bucket:
Ref: StaticAssets
files:
- source: ./build/assets/
globs:
- '**'
empty: true
headers:
CacheControl: max-age=31104000
- source: ./build/prerendered/
globs:
- '**'
empty: true
headers:
CacheControl: max-age=60
functions:
#SSR Function
svelte:
handler: build/server/serverless.handler
memorySize: 256
timeout: 15
url: true
#Router Function
cfLambda:
handler: build/edge/router.handler
memorySize: 128
timeout: 1
lambdaAtEdge:
distribution: 'WebsiteDistribution'
eventType: origin-request
resources:
Resources:
StaticAssets:
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:provider.stage}-${self:service}-static-assets
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: false
IgnorePublicAcls: false
RestrictPublicBuckets: false
StaticAssetsS3BucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: StaticAssets
PolicyDocument:
Statement:
- Sid: PublicReadGetObject
Effect: Allow
Principal: "*"
Action:
- s3:GetObject
Resource:
Fn::Join: ["", ["arn:aws:s3:::", { "Ref": "StaticAssets" }, "/*"]]
WebsiteDistribution:
Type: 'AWS::CloudFront::Distribution'
Properties:
DistributionConfig:
Origins:
-
DomainName: !Select [2, !Split ["/", !GetAtt ["SvelteLambdaFunctionUrl", "FunctionUrl"]]]
Id: default
OriginCustomHeaders:
#Lambda@edge does not support ENV vars, so instead we have to pass in a customHeaders.
-
HeaderName: 's3-host'
HeaderValue: '${self:provider.stage}-${self:service}-static-assets.s3.amazonaws.com'
CustomOriginConfig:
HTTPPort: 80
HTTPSPort: 443
OriginProtocolPolicy: 'https-only'
Enabled: true
Comment: '${self:service}_${self:provider.stage}'
DefaultCacheBehavior:
TargetOriginId: default
Compress: true
AllowedMethods:
- DELETE
- GET
- HEAD
- OPTIONS
- PATCH
- POST
- PUT
CachedMethods:
- GET
- HEAD
- OPTIONS
ForwardedValues:
Headers:
- x-forwarded-host
Cookies:
Forward: all
QueryString: True
ViewerProtocolPolicy: 'redirect-to-https'
FunctionAssociations:
- EventType: viewer-request
FunctionARN: !GetAtt XForwardFunction.FunctionMetadata.FunctionARN
XForwardFunction:
Type: AWS::CloudFront::Function
Properties:
AutoPublish: true
Name: "${self:provider.stage}-${self:service}-XForwardFunction"
FunctionCode: !Sub |
function handler(event) {
event.request.headers['x-forwarded-host'] = event.request.headers['host']
return event.request
}
FunctionConfig:
Comment: 'Add x-forwarded-host'
Runtime: cloudfront-js-1.0