-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserverless.yml
425 lines (404 loc) · 12 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
service: nebula-serverless
# app and org for use with dashboard.serverless.com
# app: nebula
# org: ORG_NAME
frameworkVersion: "2"
package:
individually: true
provider:
name: aws
runtime: nodejs12.x
stage: dev
profile: ${opt:profile, self:custom.profiles.${self:custom.stage}}
stackName: ${self:service}-${self:custom.stage}
region: us-east-1
environment:
AUTH0_DOMAIN: ${ssm:/${self:custom.stage}/auth0/domain, env:NEXT_PUBLIC_AUTH0_DOMAIN}
AUTH0_AUDIENCE: ${ssm:/${self:custom.stage}/auth0/audience}
AUTH0_CLIENT_ID: ${ssm:/${self:custom.stage}/auth0/client_id,
env:NEXT_PUBLIC_AUTH0_CLIENT_ID}
AUTH0_CLIENT_SECRET: ${ssm:/${self:custom.stage}/auth0/client_secret~true}
JWT_TOKEN_ISSUER: ${ssm:/${self:custom.stage}/jwt/token_issuer}
JWKS_URI: ${ssm:/${self:custom.stage}/jwt/jwks_uri}
POSTER_TABLE: ${self:custom.posterTable}
CONNECTION_TABLE: ${self:custom.connectionTable}
POSTER_BUCKET: ${self:custom.posterBucketName}
STAGE: ${self:custom.stage}
lambdaHashingVersion: 20201221
apiGateway:
shouldStartNameWithService: true
iamRoleStatements:
- Effect: Allow
Action:
- ses:SendEmail
Resource:
Fn::Join:
- ""
- - "arn:aws:ses:"
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":identity/*"
- Effect: Allow
Action:
- s3:PutObject
Resource: arn:aws:s3:::${self:custom.posterBucketName}/*
- Effect: Allow
Action:
- "execute-api:Invoke"
Resource:
- "arn:aws:execute-api:*:*:**/@connections/*"
- Effect: "Allow"
Action:
- sqs:SendMessage
- sqs:DeleteMessage
Resource:
Fn::GetAtt: [PosterQueue, Arn]
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:DescribeTable
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/*"
custom:
stage: ${opt:stage, self:provider.stage, 'dev'}
posterTable: ${self:service}-poster-${self:custom.stage}
connectionTable: ${self:service}-connection-${self:custom.stage}
posterBucketName: ${self:service}-poster-bucket-${self:custom.stage}
profiles:
dev: nebulaDev
staging: nebulaStaging
production: nebulaProduction
logs:
restApi: true
serverless-offline:
httpPort: 1337
functionsBasePath: api
webpack:
webpackConfig: api/webpack.config.js
includeModules:
forceExclude:
- chrome-aws-lambda
forceInclude:
- uuid
packager: "yarn"
domainRoot: yearincode.dev
cdnDomains:
production: cdn.${self:custom.domainRoot}
staging: staging-cdn.${self:custom.domainRoot}
dev: dev-cdn.${self:custom.domainRoot}
domains:
production: api.${self:custom.domainRoot}
staging: staging-api.${self:custom.domainRoot}
dev: dev-api.${self:custom.domainRoot}
customDomain:
rest:
domainName: ${self:custom.domains.${self:custom.stage}}
stage: ${self:custom.stage}
createRoute53Record: true
endpointType: regional
autoDomain: true
websocket:
domainName: ws-${self:custom.domains.${self:custom.stage}}
stage: ${self:custom.stage}
createRoute53Record: true
endpointType: regional
autoDomain: true
serverless-offline-sqs:
autoCreate: true
apiVersion: "2012-11-05"
endpoint: http://0.0.0.0:9324
region: us-east-1
accessKeyId: local
secretAccessKey: local
skipCacheInvalidation: false
dotenvInclude:
dev: ""
staging: ""
production: []
dotenv:
logging: false
include: ${self:custom.dotenvInclude.${self:custom.stage}}
required:
file: false
dynamodb: # DynamoDB Local Instance
stages:
- dev
start:
host: localhost
port: "8000" # the port of our Dynamo docker container
noStart: true
migrate: true
s3:
host: localhost
directory: api/tmp
cors: api/serverless-s3-local.xml
ses:
stages:
- dev
port: 9001
outputDir: api/output
clean: true
layers:
puppeteer:
package:
artifact: api/layers/chrome_aws_lambda.zip
name: ${self:custom.stage}-chrome-aws-lambda
description: Layer to add puppeteer to Lambda functions.
retain: false
compatibleRuntimes:
- nodejs12.x
functions:
authorize:
handler: auth/authorize.authorize
authorizeWS:
handler: auth/authorizeWS.authorizeWS
environment:
WEBSOCKET_PAYLOAD_SECRET:
${ssm:/${self:custom.stage}/websocket/payload_secret~true,
env:WEBSOCKET_PAYLOAD_SECRET}
queuePoster:
handler: posters/queuePoster.queuePoster
environment:
SQS_QUEUE_URL: { Ref: PosterQueue }
events:
- http:
path: users/{userId}/posters/queue
method: post
authorizer: authorize
cors: true
getPosters:
handler: posters/getPosters.getPosters
events:
- http:
path: users/{userId}/posters
method: get
authorizer: authorize
cors: true
getPosterBySlug:
handler: posters/getPosterBySlug.getPosterBySlug
events:
- http:
path: posters/{slug}
method: get
cors: true
getGalleryPosters:
handler: posters/getGalleryPosters.getGalleryPosters
events:
- http:
path: posters/gallery
method: get
cors: true
sendPosterStatistics:
handler: posters/statistics/send.sendPosterStatistics
environment:
SEND_POSTER_ANALYTICS_RECIPIENTS:
${ssm:/${self:custom.stage}/ses/poster_analytics_recipients,
env:SEND_POSTER_ANALYTICS_RECIPIENTS}
AWS_SOURCE_EMAIL: ${ssm:/${self:custom.stage}/ses/source_email, env:AWS_SOURCE_EMAIL}
events:
- schedule: cron(0 0 ? * 1 *) # Notify at the start of the week
connect:
handler: posters/connect.connect
environment:
WEBSOCKET_PAYLOAD_SECRET:
${ssm:/${self:custom.stage}/websocket/payload_secret~true,
env:WEBSOCKET_PAYLOAD_SECRET}
events:
- websocket:
route: $connect
authorizer:
name: authorizeWS
identitySource:
- "route.request.querystring.wsPayload"
disconnect:
handler: posters/disconnect.disconnect
events:
- websocket:
route: $disconnect
start:
handler: posters/start/start.start
timeout: 900
reservedConcurrency: 8 # No more than 8 concurrent invocations
memorySize: 2048
layers:
- { Ref: PuppeteerLambdaLayer }
environment:
AWS_SOURCE_EMAIL: ${ssm:/${self:custom.stage}/ses/source_email, env:AWS_SOURCE_EMAIL}
SITE_URL: ${ssm:/${self:custom.stage}/site/url,
env:NEXT_PUBLIC_POST_LOGOUT_REDIRECT_URI}
WEBSOCKET_API_ENDPOINT:
Fn::Join:
- ""
- - Ref: WebsocketsApi
- .execute-api.
- Ref: AWS::Region
- .amazonaws.com/
- ${self:custom.stage}
events:
- sqs:
arn:
Fn::GetAtt:
- PosterQueue
- Arn
resources:
Resources:
# SQS
PosterQueue:
Type: "AWS::SQS::Queue"
Properties:
QueueName: sqs-poster-queue-${self:custom.stage}
VisibilityTimeout: 900
# DynamoDB
ConnectionTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
Properties:
TableName: ${self:custom.connectionTable}
AttributeDefinitions:
- AttributeName: connectionId
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: connectionId
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: userIdIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: ALL
BillingMode: PAY_PER_REQUEST
PosterTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
Properties:
TableName: ${self:custom.posterTable}
AttributeDefinitions:
- AttributeName: userId
AttributeType: S
- AttributeName: posterSlug
AttributeType: S
KeySchema:
- AttributeName: posterSlug
KeyType: HASH
- AttributeName: userId
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: userIdIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: ALL
BillingMode: PAY_PER_REQUEST
# API Gateway
GatewayResponse:
Type: "AWS::ApiGateway::GatewayResponse"
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: EXPIRED_TOKEN
RestApiId:
Ref: "ApiGatewayRestApi"
StatusCode: "401"
AuthFailureGatewayResponse:
Type: "AWS::ApiGateway::GatewayResponse"
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: UNAUTHORIZED
RestApiId:
Ref: "ApiGatewayRestApi"
StatusCode: "401"
# S3
PosterBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketName: ${self:custom.posterBucketName}
AccessControl: Private
CorsConfiguration:
CorsRules:
- AllowedHeaders: ["*"]
AllowedMethods: ["PUT", "GET"]
AllowedOrigins: ["*"] # Update with domain when available
PosterBucketPolicy:
Type: AWS::S3::BucketPolicy
DeletionPolicy: Retain
Properties:
Bucket:
Ref: PosterBucket
PolicyDocument:
Statement:
- Action: s3:GetObject
Effect: "Allow"
Resource: arn:aws:s3:::${self:custom.posterBucketName}/*
Principal:
CanonicalUser:
Fn::GetAtt:
- CloudFrontOriginAccessIdentity
- S3CanonicalUserId
CloudFrontOriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
DeletionPolicy: Retain
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: "Identity for accessing CloudFront from S3 within stack
#{AWS::StackName}"
PosterCdnDistribution:
Type: AWS::CloudFront::Distribution
DeletionPolicy: Retain
Properties:
DistributionConfig:
Origins:
# S3 origin for static content
- DomainName: "${self:custom.posterBucketName}.s3.amazonaws.com"
Id: PosterBucketS3Origin
S3OriginConfig:
OriginAccessIdentity:
Fn::Join:
- ""
- - "origin-access-identity/cloudfront/"
- Ref: CloudFrontOriginAccessIdentity
Comment: CDN for poster photos.
Enabled: true
DefaultRootObject: index.html
HttpVersion: http2
DefaultCacheBehavior:
AllowedMethods:
- HEAD
- GET
- OPTIONS
Compress: true
TargetOriginId: PosterBucketS3Origin
ForwardedValues:
Headers:
[
"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Origin",
]
QueryString: false
Cookies:
Forward: none
ViewerProtocolPolicy: redirect-to-https
PriceClass: PriceClass_100
plugins:
- serverless-domain-manager
- serverless-dotenv-plugin
- serverless-functions-base-path
- serverless-offline-ses
- serverless-webpack
- serverless-s3-local
- serverless-dynamodb-local
- serverless-offline-sqs
- serverless-offline