-
Notifications
You must be signed in to change notification settings - Fork 3
/
serverless.yml
409 lines (379 loc) · 12.3 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
service: calendly-backend
frameworkVersion: '2'
plugins:
- serverless-appsync-plugin
- serverless-iam-roles-per-function
- serverless-export-env
- serverless-plugin-include-dependencies
- serverless-plugin-common-excludes
- serverless-stack-output
- serverless-lumigo
provider:
name: aws
runtime: nodejs12.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-1'}
lambdaHashingVersion: '20201221'
environment:
STAGE: ${opt:stage}
AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1"
USERS_TABLE: !Ref UsersTable
PINPOINT_APP: ${self:custom.pinpointAppId}
NEW_SESSION_TEMPLATE: ${self:custom.pinpointNewSessionTemplate}
custom:
bucket-name: ${file(./config.${opt:stage, 'dev'}.json):BUCKET_NAME} # S3 Bucket Name
region: ${opt:region, self:provider.region}
appSync:
- ${file(serverless.appsync-api.yml)}
includeDependencies:
enableCaching: true
pinpointAppId: !Ref PinpointApplication
pinpointNewSessionTemplate: !Ref NewSessionBookedPinpointTemplate
FromEmailAddress: ${file(./config.${opt:stage, 'dev'}.json):FromEmailAddress}
output:
file: output-stack.yml
lumigo:
token: ${file(./config.${opt:stage, 'dev'}.json):LUMIGO_TOKEN}
nodePackageManager: npm
package:
exclude:
- node_modules/**/aws-sdk/**
- email-templates/**
- __tests__/**
- mapping-templates/**
- '*.json'
- '*.yml'
- '*.graphql'
- assets/**
- output-stack.yml
excludeDevDependencies: false
individually: true
functions:
getUserInfo:
handler: functions/api/get-user-info.handler
events:
- http:
path: user-info
method: get
cors: true
environment:
USER_LINK_TABLE: !Ref UserLinkTable
USER_TABLE: !Ref UsersTable
iamRoleStatements:
- Effect: Allow
Action: dynamodb:GetItem
Resource: !GetAtt UserLinkTable.Arn
- Effect: Allow
Action: dynamodb:GetItem
Resource: !GetAtt UsersTable.Arn
getUserSchedule:
handler: functions/api/get-user-schedule.handler
events:
- http:
path: user-schedule
method: get
cors: true
environment:
USER_SESSION_TABLE: !Ref UserSessionTable
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:Query
Resource: !GetAtt UserSessionTable.Arn
createSession:
handler: functions/api/create-session.handler
events:
- http:
path: user-session
method: post
cors: true
environment:
USER_SESSION_TABLE: !Ref UserSessionTable
iamRoleStatements:
- Effect: Allow
Action: dynamodb:PutItem
Resource: !GetAtt UserSessionTable.Arn
- Effect: Allow
Action: dynamodb:GetItem
Resource: !GetAtt UsersTable.Arn
- Effect: Allow
Action:
- mobiletargeting:SendMessages
Resource: '*'
confirmUserSignup:
handler: functions/cognito/confirm-user-signup.handler
environment:
USERS_TABLE: !Ref UsersTable
iamRoleStatements:
- Effect: Allow
Action: dynamodb:PutItem
Resource: !GetAtt UsersTable.Arn
- Effect: Allow
Action: cognito-idp:AdminAddUserToGroup
Resource: '*'
userSessionStreamEvent:
handler: functions/stream/user-session-event.handler
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt:
- UserSessionTable
- StreamArn
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:Query
- dynamodb:GetItem
- dynamodb:DeleteItem
Resource:
- Fn::GetAtt:
- UserSessionTable
- Arn
resources:
Resources:
UsersTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
AttributeDefinitions:
- AttributeName: id
AttributeType: S
UserLinkTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: link
KeyType: HASH
AttributeDefinitions:
- AttributeName: link
AttributeType: S
UserEventTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: shortName
KeyType: RANGE
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: shortName
AttributeType: S
UserSessionTable:
Type: AWS::DynamoDB::Table
Properties:
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
- AttributeName: sessionTime
KeyType: RANGE
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: sessionTime
AttributeType: S
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
#****** Cognito Configuration ******#
CognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
AutoVerifiedAttributes:
- email
VerificationMessageTemplate:
DefaultEmailOption: CONFIRM_WITH_LINK
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
RequireUppercase: true
RequireSymbols: false
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: name
Required: false
Mutable: true
LambdaConfig:
PostConfirmation: !GetAtt ConfirmUserSignupLambdaFunction.Arn
CognitoUserGroupNormalUsers:
Type: AWS::Cognito::UserPoolGroup
Properties:
GroupName: User
Description: Calendly Normal Users
Precedence: 1
UserPoolId:
Ref: CognitoUserPool
UserPoolInvokeConfirmUserSignupLambdaPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:invokeFunction
FunctionName: !Ref ConfirmUserSignupLambdaFunction
Principal: cognito-idp.amazonaws.com
SourceArn: !GetAtt CognitoUserPool.Arn
WebUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref CognitoUserPool
ClientName: web
ExplicitAuthFlows:
- ALLOW_USER_SRP_AUTH
- ALLOW_USER_PASSWORD_AUTH
- ALLOW_REFRESH_TOKEN_AUTH
PreventUserExistenceErrors: LEGACY
AllowedOAuthFlowsUserPoolClient: true
SupportedIdentityProviders:
- COGNITO
CallbackURLs:
- !Join ["", ["https://", !GetAtt CloudFrontDistro.DomainName, ""]]
LogoutURLs:
- !Join ["",["https://",!GetAtt CloudFrontDistro.DomainName, ""]]
AllowedOAuthFlows:
- implicit
AllowedOAuthScopes:
- email
- openid
- profile
CalendlyCognitoDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: 'calendly-app-${self:provider.stage}'
UserPoolId: !Ref CognitoUserPool
#****** Pinpoint Configuration ******#
PinpointApplication:
Type: AWS::Pinpoint::App
Properties:
Name: 'calendly-app-${self:provider.stage}'
PinpointEmailChannelFeature:
Type: AWS::Pinpoint::EmailChannel
Properties:
ApplicationId: !Ref PinpointApplication
Enabled: True
FromAddress: ${self:custom.FromEmailAddress}
Identity: !Sub 'arn:aws:ses:${AWS::Region}:${AWS::AccountId}:identity/${self:custom.FromEmailAddress}'
NewSessionBookedPinpointTemplate:
Type: AWS::Pinpoint::EmailTemplate
Properties:
TemplateName: 'calendly-app-${self:provider.stage}-new-session-booked'
Subject: 'Calendly Clone App - New Session Alert!! {{Duration}} booked by {{Name}}'
HtmlPart: '<html> <head> <style> body{ text-align: center; } body table{ margin: 0 auto; } </style> </head> <body > <h1>New Session Booked!</h1> <table border="1" cellpadding="10"> <tr> <td>Name</td> <td>{{Name}}</td> </tr> <tr> <td>Email</td> <td>{{Email}}</td> </tr> <tr> <td>Scheduled On</td> <td>{{ScheduledOn}} for {{Duration}}</td> </tr> </table> <br><br> Note: You will receive an email reminder 10 minutes prior to your session. </body> </html>'
# Use https://tools.knowledgewalls.com/online-multiline-to-single-line-converter
# To convert HTML into single line and paste in the HtmlPart
# Website Hosting S3 Bucket
S3SiteBucket: # Host website content in this bucket
Type: AWS::S3::Bucket
Properties:
BucketName: ${self:custom.bucket-name}
WebsiteConfiguration:
IndexDocument: index.html
CorsConfiguration:
CorsRules:
- AllowedHeaders:
- '*'
AllowedMethods:
- GET
AllowedOrigins:
- '*'
ExposedHeaders:
- Date
Id: myCORSRuleId1
MaxAge: 3600
OriginAccessIdentity:
Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Sub 'OriginAccessIdentity for ${S3SiteBucket}'
BucketPolicy: # Allow site access from CloudFront only
DependsOn: OriginAccessIdentity
Type: AWS::S3::BucketPolicy
Properties:
PolicyDocument:
Id: OriginBucketPolicy
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
CanonicalUser: !GetAtt OriginAccessIdentity.S3CanonicalUserId
Action: 's3:GetObject'
Resource: !Sub 'arn:aws:s3:::${S3SiteBucket}/*'
Bucket: !Ref S3SiteBucket
CloudFrontDistro: # To serve the website via CDN
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Origins:
- DomainName: ${self:custom.bucket-name}.s3.amazonaws.com
Id: myS3Origin
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${OriginAccessIdentity}'
Enabled: 'true'
Comment: 'Calendly Clone App Distro'
DefaultRootObject: index.html
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- DELETE
- OPTIONS
- PATCH
- POST
- PUT
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
DefaultTTL: 300
ForwardedValues:
Headers:
- Accept
- Referer
- Authorization
- Content-Type
QueryString: true
MaxTTL: 300
TargetOriginId: myS3Origin
ViewerProtocolPolicy: https-only
PriceClass: 'PriceClass_100'
ViewerCertificate:
CloudFrontDefaultCertificate: true
AppSyncLoggingServiceRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: "calendly-appsync-logs-${self:provider.region}-${self:provider.stage}-role"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "appsync.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: "calendly-appsync-logs-${self:provider.region}-${self:provider.stage}-policy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action: ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
Resource: "*"
Outputs:
CognitoUserPoolId:
Value: !Ref CognitoUserPool
WebUserPoolClientId:
Value: !Ref WebUserPoolClient
CloudfrontDomainName:
Value: !GetAtt CloudFrontDistro.DomainName