-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverless.yml
468 lines (420 loc) · 18 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
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
# ##
#
# 💵💵💵
#
# Expenses
#
# with Serverless
#
# ##
service: Expenses
plugins:
localPath: "./serverless_plugins"
modules:
- output-plugin
- client-deploy
custom:
api:
name: Expenses
provider:
name: aws
runtime: nodejs14.x
variableSyntax: "\\${{([ ~:a-zA-Z0-9._@\\'\",\\-\\/\\(\\)]+?)}}" # notice the double quotes for yaml to ignore the escape characters!
stage: ${{opt:stage,'dev'}}
region: ${{opt:region, 'us-east-1'}}
versionFunctions: false
package:
individually: true
exclude:
- "**/*"
# CloudFormation architecture
resources:
Resources:
S3Bucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: "Private"
WebsiteConfiguration:
IndexDocument: "index.html"
ErrorDocument: "index.html"
IAMUser:
Type: AWS::IAM::User
DependsOn: S3Bucket
Properties:
Policies:
- PolicyName: DynamoDBAppSyncAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "dynamodb:BatchGetItem"
- "dynamodb:BatchWriteItem"
- "dynamodb:PutItem"
- "dynamodb:DeleteItem"
- "dynamodb:GetItem"
- "dynamodb:Scan"
- "dynamodb:Query"
- "dynamodb:UpdateItem"
Resource:
- !GetAtt DynamoDBTable.Arn
- !Join
- ""
- - !GetAtt DynamoDBTable.Arn
- "/index/*"
- !Join
- ""
- - !GetAtt DynamoDBTable.Arn
- "/*"
- PolicyName: AppSyncAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "appsync:*"
Resource: !Join
- ""
- - !GetAtt AppSyncApi.Arn
- "/*"
IAMUserKey:
Type: AWS::IAM::AccessKey
DependsOn: IAMUser
Properties:
UserName: !Ref IAMUser
RoleLambdaExecution:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- appsync.amazonaws.com
- lambda.amazonaws.com
- edgelambda.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
Policies:
- PolicyName: logsAccess
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "logs:*"
Resource: "arn:aws:logs:*:*:*"
AppSyncApi:
Type: "AWS::AppSync::GraphQLApi"
DependsOn: S3Bucket
Properties:
AuthenticationType: "AWS_IAM"
Name: ${{self:custom.api.name}}
AppSyncSchema:
Type: "AWS::AppSync::GraphQLSchema"
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
Definition: |
input CreateExpenseInput {
user: String!
amount: Int!
date: Int
description: String
categories: [String!]
}
type Expense {
id: ID!
user: String!
date: Int!
amount: Int!
description: String
categories: [String!]
}
type Mutation {
createExpense(input: CreateExpenseInput!): Expense
updateExpense(id: ID!, input: UpdateExpenseInput!): Expense
deleteExpense(id: ID!): Expense
}
type Query {
getExpense(id: ID!): Expense
getTodayExpenses: [Expense]
getExpenses(since: Int!, to: Int!): [Expense]
getUserExpenses(user: String!, date: String): [Expense]
}
input UpdateExpenseInput {
user: String
date: Int
amount: Int
description: String
categories: [String!]
}
schema {
query: Query
mutation: Mutation
}
DynamoDBTable:
Type: "AWS::DynamoDB::Table"
Properties:
TableName: "Expenses_${{self:provider.stage}}"
BillingMode: "PAY_PER_REQUEST"
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
- AttributeName: "user"
AttributeType: "S"
- AttributeName: "date"
AttributeType: "N"
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
GlobalSecondaryIndexes:
- IndexName: "id-date-index"
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
- AttributeName: "date"
KeyType: "RANGE"
Projection:
ProjectionType: "ALL"
- IndexName: "user-date-index"
KeySchema:
- AttributeName: "user"
KeyType: "HASH"
- AttributeName: "date"
KeyType: "RANGE"
Projection:
ProjectionType: "ALL"
DynamoDBService:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- appsync.amazonaws.com
Action:
- "sts:AssumeRole"
Path: /
Policies:
- PolicyName: DynamoDB
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "dynamodb:*"
Resource: !GetAtt DynamoDBTable.Arn
AppSyncDataSource:
Type: "AWS::AppSync::DataSource"
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
Name: ExpensesDataSource
Description: "AppSync Data Source for Expenses"
Type: AMAZON_DYNAMODB
ServiceRoleArn: !GetAtt DynamoDBService.Arn
DynamoDBConfig:
AwsRegion: ${{self:provider.region}}
TableName: !Ref DynamoDBTable
UseCallerCredentials: True
# Query resolvers
AppSyncQueryResolverGetTodayExpenses:
Type: "AWS::AppSync::Resolver"
DependsOn: AppSyncDataSource
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Query
FieldName: getTodayExpenses
DataSourceName: !GetAtt AppSyncDataSource.Name
RequestMappingTemplate: |
{
#set( $today = $util.time.nowISO8601().substring(0, 10) )
#set( $minDate = $util.time.epochMilliSecondsToSeconds($util.time.parseISO8601ToEpochMilliSeconds("${today}T00:00:00.0Z")) )
#set( $maxDate = $util.time.epochMilliSecondsToSeconds($util.time.parseISO8601ToEpochMilliSeconds("${today}T23:59:59.9Z")) )
"version" : "2017-02-28",
"operation" : "Scan",
"filter" : {
"expression": "#date BETWEEN :min AND :max",
"expressionNames": {
"#date": "date"
},
"expressionValues" : {
":min": $util.dynamodb.toNumberJson($minDate),
":max": $util.dynamodb.toNumberJson($maxDate),
}
}
}
ResponseMappingTemplate: "$util.toJson($ctx.result.items)"
AppSyncQueryResolverGetExpenses:
Type: "AWS::AppSync::Resolver"
DependsOn: AppSyncDataSource
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Query
FieldName: getExpenses
DataSourceName: !GetAtt AppSyncDataSource.Name
RequestMappingTemplate: |
{
"version" : "2017-02-28",
"operation" : "Scan",
"filter" : {
"expression": "#date BETWEEN :min AND :max",
"expressionNames": {
"#date": "date"
},
"expressionValues" : {
":min": $util.dynamodb.toNumberJson($context.args.since),
":max": $util.dynamodb.toNumberJson($context.args.to)
}
}
}
ResponseMappingTemplate: "$util.toJson($ctx.result.items)"
# Mutation resolvers
AppSyncMutateResolverUpdateExpense:
Type: "AWS::AppSync::Resolver"
DependsOn: AppSyncDataSource
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: updateExpense
DataSourceName: !GetAtt AppSyncDataSource.Name
RequestMappingTemplate: |
{
#set( $input = $context.arguments.input )
#set( $expression = "SET" )
#set( $keys = {} )
#set( $values = {} )
#foreach( $entry in $input.entrySet() )
#set( $key = $entry.key )
#if( $key != "id" )
#set( $value = $entry.value )
#if (($key == 'categories') && ($util.isList($value)))
#set( $categories = [] )
#foreach( $category in $input.categories )
#if ($util.isString($category))
$util.qr($categories.add($category))
#end
#end
#if (!$categories.isEmpty())
#set( $value = $util.dynamodb.toStringSet($categories) )
#else
#set( $value = false )
#end
#else
#set( $value = $util.dynamodb.toDynamoDB($entry.value) )
#end
#if ( $value )
#set( $expression = "${expression} #${key} = :${key}" )
$util.qr($keys.put("#${key}", $key))
$util.qr($values.put(":${key}", $value))
#end
#if ( $foreach.hasNext )
#set( $expression = "${expression}," )
#end
#end
#end
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"id": $util.dynamodb.toStringJson($context.arguments.id)
},
"update" : {
"expression" : "${expression}"
#if( !${keys.isEmpty()} )
,"expressionNames" : $utils.toJson($keys)
#end
#if( !${values.isEmpty()} )
,"expressionValues" : $utils.toJson($values)
#end
}
}
ResponseMappingTemplate: "$util.toJson($ctx.result)"
AppSyncMutateResolverCreateExpense:
Type: "AWS::AppSync::Resolver"
DependsOn: AppSyncDataSource
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: createExpense
DataSourceName: !GetAtt AppSyncDataSource.Name
RequestMappingTemplate: |
{
#set( $input = $context.arguments.input)
#set( $attribs = {
"user": $util.dynamodb.toString($input.user),
"amount": $util.dynamodb.toNumber($input.amount)
})
## If date not set use current time
#if( !$input.date )
#set( $date = $util.time.nowEpochSeconds() )
#else
#set( $date = $input.date )
#end
$!{attribs.put("date", $util.dynamodb.toNumber($date))}
## Add description if it exists
#if( $input.description )
$!{attribs.put("description", $util.dynamodb.toString($input.description))}
#end
## Just in case check categories for any non string items
#if( ($input.categories) && ($util.isList($input.categories)) )
#set( $categories = [] )
#foreach( $category in $input.categories )
#if ($util.isString($category))
$util.qr($categories.add($category))
#end
#end
#if (!$categories.isEmpty())
$!{attribs.put("categories", $util.dynamodb.toStringSet($categories))}
#end
#end
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id": $util.dynamodb.toStringJson($util.autoId()),
},
"attributeValues" : $utils.toJson($attribs)
}
ResponseMappingTemplate: "$util.toJson($ctx.result)"
AppSyncMutateResolverDeleteExpense:
Type: "AWS::AppSync::Resolver"
DependsOn: AppSyncDataSource
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: deleteExpense
DataSourceName: !GetAtt AppSyncDataSource.Name
RequestMappingTemplate: |
{
"version" : "2017-02-28",
"operation" : "DeleteItem",
"key" : {
"id" : $util.dynamodb.toStringJson($ctx.args.id)
}
}
ResponseMappingTemplate: "$util.toJson($ctx.result)"
Outputs:
Bucket:
Description: The application bucket
Value: !Ref S3Bucket
User:
Description: The dedicated AppSync user
Value: !Ref IAMUser
AccessKeyId:
Description: The user access key
Value: !Ref IAMUserKey
SecretAccessKey:
Description: The user access secret
Value: !GetAtt IAMUserKey.SecretAccessKey
ApiEndpoint:
Description: The URL to the GraphQL Endpoint
Value: !GetAtt AppSyncApi.GraphQLUrl
ApiId:
Description: The API ID of the GraphQL API
Value: !GetAtt AppSyncApi.ApiId
ClientUrl:
Description: Client application url
Value: !GetAtt S3Bucket.WebsiteURL