-
Notifications
You must be signed in to change notification settings - Fork 13
/
serverless.yml
216 lines (205 loc) · 6.5 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
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
service: 'remix-serverless-app'
frameworkVersion: '2'
package:
individually: true
plugins:
- serverless-esbuild
- serverless-s3-sync
custom:
# stage-specific variables
dev:
HOST: my-cloudfront-domain.cloudfront.net # replace with the WebsiteDomain from stack outputs
prod:
HOST: my-domain.com # replace with your production url
# serverless-esbuild config, bundles your code and allows you to use typescript or es-next features in your handler code
esbuild:
bundle: true
minify: false
sourcemap: true
sourcesContent: false
exclude: ['aws-sdk']
target: 'node14'
define:
'require.resolve': null
platform: 'node'
# serverless-s3-sync config, handles uploading static files to S3
s3Sync:
buckets:
- bucketNameKey: WebsiteBucketName
bucketPrefix: website/
localDir: public
params:
- favicon.png:
CacheControl: 'public, max-age=3600'
- 'static/**/*':
CacheControl: 'public, max-age=31536000, immutable'
provider:
name: aws
runtime: nodejs14.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-1'}
environment:
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1'
NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000'
lambdaHashingVersion: '20201221'
functions:
remix:
handler: server/index.handler
description: My Remix app!
events:
- httpApi:
method: any
path: '/{proxy+}'
resources:
Resources:
# Created by serverless
# HttpApi:
# Type: AWS::ApiGatewayV2::Api
# Properties:
# Name: ${self:provider.stage}-${self:service}
# ProtocolType: HTTP
# S3 bucket for static assets
WebsiteBucket:
Type: AWS::S3::Bucket
Properties: {}
# Allow CloudFront to use GetObject in the WebsiteBucket
WebsiteOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: Origin Access Identity to Access ${self:service} Website Bucket ${self:provider.stage}
WebsiteBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref WebsiteBucket
PolicyDocument:
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource:
Fn::Join:
- /
- - Fn::GetAtt:
- WebsiteBucket
- Arn
- '*'
Principal:
CanonicalUser:
Fn::GetAtt:
- WebsiteOriginAccessIdentity
- S3CanonicalUserId
# Use CloudFront in front of the Remix API Gateway endpoint
RemixCachePolicy:
Type: AWS::CloudFront::CachePolicy
Properties:
CachePolicyConfig:
Name: RemixCachePolicy
DefaultTTL: 60
MinTTL: 0
MaxTTL: 60
ParametersInCacheKeyAndForwardedToOrigin:
HeadersConfig:
HeaderBehavior: none
EnableAcceptEncodingGzip: true
QueryStringsConfig:
QueryStringBehavior: all
CookiesConfig:
CookieBehavior: all
# CloudFront Configuration
CDN:
Type: AWS::CloudFront::Distribution
DependsOn:
- WebsiteBucket
- HttpApi
Properties:
DistributionConfig:
Origins:
- DomainName:
Fn::GetAtt:
- WebsiteBucket
- DomainName
Id: StaticOrigin
S3OriginConfig:
OriginAccessIdentity:
Fn::Join:
- /
- - origin-access-identity
- cloudfront
- !Ref WebsiteOriginAccessIdentity
OriginPath: '/website'
- DomainName:
Fn::Join:
- ''
- - Ref: HttpApi
- '.execute-api.${self:provider.region}.amazonaws.com'
Id: RemixOrigin
# UNCOMMENT THIS TO SET THE X-Forwarded-Host header for API Gateway
# OriginCustomHeaders:
# - HeaderName: X-Forwarded-Host
# HeaderValue: ${self:${self:provider.stage}.HOST}
CustomOriginConfig:
OriginProtocolPolicy: https-only
OriginSSLProtocols: [TLSv1.2]
# missing static assets will give a 403 from s3, so you can override the error response here
# CustomErrorResponses:
# - ErrorCachingMinTTL: 60
# ErrorCode: 403
# ResponseCode: 404
# ResponsePagePath: /404
# By default, all requests go to remix
DefaultCacheBehavior:
AllowedMethods: [GET, HEAD, OPTIONS, PUT, PATCH, POST, DELETE]
CachedMethods: [GET, HEAD, OPTIONS]
Compress: true
CachePolicyId:
Ref: RemixCachePolicy
TargetOriginId: RemixOrigin
ViewerProtocolPolicy: redirect-to-https
# Requests to /static go to S3
CacheBehaviors:
- PathPattern: 'static/*'
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
Compress: true
ForwardedValues:
QueryString: true
Cookies:
Forward: none
TargetOriginId: StaticOrigin
ViewerProtocolPolicy: redirect-to-https
# Special rule for browser favicon requests to also go to S3 origin
- PathPattern: '/favicon.*'
AllowedMethods:
- GET
- HEAD
CachedMethods:
- GET
- HEAD
Compress: false
ForwardedValues:
QueryString: false
Cookies:
Forward: none
TargetOriginId: StaticOrigin
ViewerProtocolPolicy: redirect-to-https
Comment: ${self:service}-${self:provider.stage}
Enabled: true
HttpVersion: http2
PriceClass: PriceClass_100
ViewerCertificate:
CloudFrontDefaultCertificate: true
Outputs:
WebsiteBucketName:
Value:
Ref: WebsiteBucket
DistributionID:
Value:
Ref: CDN
WebsiteDomain:
Value:
Fn::GetAtt: [CDN, DomainName]