-
Notifications
You must be signed in to change notification settings - Fork 24
/
template.yaml
537 lines (515 loc) · 17.3 KB
/
template.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: "ACD Leave Approval Application"
# Parameters Section
Parameters:
CognitoUserPoolDomain:
Type: String
Description: Auth domain used in cognito to create the sign in / sign up links
S3BucketName:
Type: String
Description: Auth domain used in cognito to create the sign in / sign up links
# Resources Section
Resources:
# cognito trigger
CognitoTriggerPostSignup:
Type: AWS::Serverless::Function
Properties:
FunctionName: create-user-trigger
CodeUri: lambdas/create-user-trigger/
Handler: app.lambdaHandler
Runtime: nodejs14.x
Environment:
Variables:
leavesTable: !Ref LeaveTable
userinfoTable: !Ref UserInfoTable
Policies:
- AWSLambdaDynamoDBExecutionRole
- DynamoDBCrudPolicy:
TableName: !Ref LeaveTable
- DynamoDBCrudPolicy:
TableName: !Ref UserInfoTable
# get user infor after login
GetUserInfo:
Type: AWS::Serverless::Function
Properties:
FunctionName: get-user-info
CodeUri: lambdas/get-user-info/
Handler: app.lambdaHandler
Runtime: nodejs14.x
Environment:
Variables:
leavesTable: !Ref LeaveTable
userinfoTable: !Ref UserInfoTable
Policies:
- AWSLambdaDynamoDBExecutionRole
- DynamoDBCrudPolicy:
TableName: !Ref LeaveTable
- DynamoDBCrudPolicy:
TableName: !Ref UserInfoTable
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref LeaveApprovalApi2
Path: /getUserInfo
Method: POST
# async trigger for leave approval
ManualApproval:
Type: AWS::Serverless::Function
Properties:
FunctionName: manual-approval
CodeUri: lambdas/manual-approval
Handler: app.lambdaHandler
Runtime: nodejs14.x
Environment:
Variables:
leavesTable: !Ref LeaveTable
userinfoTable: !Ref UserInfoTable
Policies:
- AWSLambdaDynamoDBExecutionRole
- DynamoDBCrudPolicy:
TableName: !Ref LeaveTable
- DynamoDBCrudPolicy:
TableName: !Ref UserInfoTable
- Statement:
- Sid: StepFunctionTaskToken
Effect: Allow
Action:
- states:SendTaskSuccess
Resource: "*"
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref LeaveApprovalApi2
Path: /manualApproval
Method: POST
# step function lambda - approve leave
ApproveLeave:
Type: AWS::Serverless::Function
Properties:
FunctionName: approve-leave
CodeUri: lambdas/approve-leave
Handler: app.lambdaHandler
Runtime: nodejs14.x
Environment:
Variables:
leavesTable: !Ref LeaveTable
userinfoTable: !Ref UserInfoTable
Policies:
- AWSLambdaDynamoDBExecutionRole
- DynamoDBCrudPolicy:
TableName: !Ref LeaveTable
- DynamoDBCrudPolicy:
TableName: !Ref UserInfoTable
# step function lambda - wait for task token
WaitForTaskToken:
Type: AWS::Serverless::Function
Properties:
FunctionName: wait-for-task-token
CodeUri: lambdas/wait-for-tasktoken
Handler: app.lambdaHandler
Runtime: nodejs14.x
Environment:
Variables:
leavesTable: !Ref LeaveTable
userinfoTable: !Ref UserInfoTable
Policies:
- AWSLambdaDynamoDBExecutionRole
- DynamoDBCrudPolicy:
TableName: !Ref LeaveTable
- DynamoDBCrudPolicy:
TableName: !Ref UserInfoTable
# step function
LeaveApplicationStepFunction:
Type: AWS::Serverless::StateMachine
Properties:
Name: approve-leave
DefinitionUri: stepfunction/approve-leave.asl.json
DefinitionSubstitutions:
ApproveLeaveArn: !GetAtt ApproveLeave.Arn
WaitForTaskTokenArn: !GetAtt WaitForTaskToken.Arn
Policies:
- CloudWatchLogsFullAccess
- LambdaInvokePolicy:
Effect: Allow
Action: lambda:InvokeFunction
FunctionName: "*"
Resource:
- !GetAtt WaitForTaskToken.Arn
- !GetAtt ApproveLeave.Arn
Logging:
IncludeExecutionData: true
Level: ALL
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt LeaveApplicationLogGroup.Arn
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref LeaveApprovalApi2
Path: /requestLeave
Method: POST
LeaveApplicationLogGroup:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 7
LogGroupName: !Sub "LeaveApplicationLogGroup"
#dynamodb - userInfo
UserInfoTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: UserInfoTable
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: "email"
AttributeType: "S"
KeySchema:
- AttributeName: "email"
KeyType: "HASH"
#dynamodb
LeaveTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: LeaveTable
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
#api
LeaveApprovalApi2:
Type: AWS::Serverless::Api
Properties:
Name: !Sub "LeaveApprovalApi2"
StageName: prod
DefinitionBody:
openapi: "3.0.1"
info:
title: "acd-api"
version: "1.0"
paths:
/getUserInfo:
post:
security:
- CognitoAuthorizer: []
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetUserInfo.Arn}/invocations"
passthroughBehavior: "when_no_match"
type: "aws_proxy"
options:
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
Access-Control-Allow-Methods:
schema:
type: "string"
Access-Control-Allow-Headers:
schema:
type: "string"
content: {}
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: !Sub "'https://${CloudFrontDistribution.DomainName}'"
responseTemplates:
application/json: "{}\n"
requestTemplates:
application/json: "{\n \"statusCode\" : 200\n}\n"
passthroughBehavior: "when_no_match"
type: "mock"
/manualApproval:
post:
security:
- CognitoAuthorizer: []
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ManualApproval.Arn}/invocations"
passthroughBehavior: "when_no_match"
type: "aws_proxy"
options:
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
Access-Control-Allow-Methods:
schema:
type: "string"
Access-Control-Allow-Headers:
schema:
type: "string"
content: {}
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: !Sub "'https://${CloudFrontDistribution.DomainName}'"
responseTemplates:
application/json: "{}\n"
requestTemplates:
application/json: "{\n \"statusCode\" : 200\n}\n"
passthroughBehavior: "when_no_match"
type: "mock"
/requestLeave:
post:
responses:
"400":
description: "400 response"
content: {}
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
content: {}
security:
- CognitoAuthorizer: []
x-amazon-apigateway-integration:
credentials: !GetAtt LeaveApprovalApi2Role.Arn
httpMethod: "POST"
uri: !Sub "arn:aws:apigateway:${AWS::Region}:states:action/StartExecution"
responses:
"200":
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
"400":
statusCode: "400"
requestTemplates:
application/json:
!Sub "{\"input\": \"$util.escapeJavaScript($input.json('$'))\"\
, \"stateMachineArn\": \"${LeaveApplicationStepFunction}\"\
}"
passthroughBehavior: "when_no_match"
type: "aws"
options:
responses:
"200":
description: "200 response"
headers:
Access-Control-Allow-Origin:
schema:
type: "string"
Access-Control-Allow-Methods:
schema:
type: "string"
Access-Control-Allow-Headers:
schema:
type: "string"
content: {}
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: !Sub "'https://${CloudFrontDistribution.DomainName}'"
responseTemplates:
application/json: "{}\n"
requestTemplates:
application/json: "{\n \"statusCode\" : 200\n}\n"
passthroughBehavior: "when_no_match"
type: "mock"
components:
securitySchemes:
CognitoAuthorizer:
type: "apiKey"
name: "Authorization"
in: "header"
x-amazon-apigateway-authtype: "cognito_user_pools"
x-amazon-apigateway-authorizer:
providerARNs:
- !Sub "arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/${CognitoUserPool}"
type: "cognito_user_pools"
#api role
LeaveApprovalApi2Role:
Type: AWS::IAM::Role
Properties:
ManagedPolicyArns:
- >-
arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: states:StartExecution
Resource:
- Ref: LeaveApplicationStepFunction
PolicyName: StepFunctionSyncExecution
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: lambda:InvokeFunction
Resource: !Sub "arn:aws:lambda:*:${AWS::AccountId}:function:*"
PolicyName: LambdaFunctionInvocation
#cognito pool
CognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: ACD-User-Pool
AutoVerifiedAttributes:
- email
AliasAttributes:
- email
Policies:
PasswordPolicy:
MinimumLength: 8
RequireUppercase: true
RequireLowercase: true
RequireNumbers: true
RequireSymbols: true
Schema:
- AttributeDataType: String
Name: email
Required: true
LambdaConfig:
PostConfirmation: !GetAtt CognitoTriggerPostSignup.Arn
# Permission for Cognito User Pool to trigger Lambda
CognitoTriggerPostSignupPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt CognitoTriggerPostSignup.Arn
Principal: cognito-idp.amazonaws.com
SourceArn: !GetAtt CognitoUserPool.Arn
#cognito pool client
CognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref CognitoUserPool
ClientName: ACD-User-Pool-Client
GenerateSecret: false
SupportedIdentityProviders:
- COGNITO
AllowedOAuthFlows:
- implicit
AllowedOAuthScopes:
- phone
- email
- openid
- profile
- aws.cognito.signin.user.admin
CallbackURLs:
- !Sub
- https://${CloudfrontDomain}
- { CloudfrontDomain: !GetAtt CloudFrontDistribution.DomainName }
LogoutURLs:
- !Sub
- https://${CloudfrontDomain}
- { CloudfrontDomain: !GetAtt CloudFrontDistribution.DomainName }
ExplicitAuthFlows:
- ALLOW_USER_PASSWORD_AUTH
- ALLOW_REFRESH_TOKEN_AUTH
AllowedOAuthFlowsUserPoolClient: true
#cognito domain
CognitoDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: !Ref CognitoUserPoolDomain
UserPoolId: !Ref CognitoUserPool
#s3 bucket and hosting
# Create the bucket to contain the website HTML
S3Bucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: !Ref S3BucketName
WebsiteConfiguration:
ErrorDocument: "error.html"
IndexDocument: "index.html"
# Configure the bucket as a CloudFront Origin
ReadPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: !Ref S3Bucket
PolicyDocument:
Statement:
- Action: "s3:GetObject"
Effect: Allow
Resource: !Sub "arn:aws:s3:::${S3Bucket}/*"
Principal:
CanonicalUser: !GetAtt CloudFrontOriginAccessIdentity.S3CanonicalUserId
# Configure Access to CloudFroun
CloudFrontOriginAccessIdentity:
Type: "AWS::CloudFront::CloudFrontOriginAccessIdentity"
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: !Ref S3Bucket
# Configure CloudFront
CloudFrontDistribution:
Type: "AWS::CloudFront::Distribution"
Properties:
DistributionConfig:
CustomErrorResponses:
- ErrorCode: 403 # not found
ResponseCode: 404
ResponsePagePath: "/index.html"
DefaultCacheBehavior:
AllowedMethods:
- GET
- HEAD
- OPTIONS
CachedMethods:
- GET
- HEAD
- OPTIONS
Compress: true
DefaultTTL: 3600 # in seconds
ForwardedValues:
Cookies:
Forward: none
QueryString: false
MaxTTL: 86400 # in seconds
MinTTL: 60 # in seconds
TargetOriginId: s3origin
ViewerProtocolPolicy: "allow-all"
# This DefaultRootObject configuration is not enough.
DefaultRootObject: "/index.html"
Enabled: true
HttpVersion: http2
Origins:
- DomainName: !GetAtt "S3Bucket.DomainName"
Id: s3origin
S3OriginConfig:
OriginAccessIdentity: !Sub "origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}"
PriceClass: "PriceClass_All"
# Outputs Section
Outputs:
CloudfrontUrl:
Value: !GetAtt CloudFrontDistribution.DomainName
APIDomain:
Value: !Ref LeaveApprovalApi2
CognitoClientId:
Value: !Ref CognitoUserPoolClient