-
Notifications
You must be signed in to change notification settings - Fork 0
/
jambolan-cloudformation.yaml
442 lines (419 loc) · 11.8 KB
/
jambolan-cloudformation.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
---
AWSTemplateFormatVersion: '2010-09-09'
Description: >
JAMBOLAN Serverless Stack
This template defines CreateStore and FindStoreById Lambda Functions
and the API Gateway resources (POST /store and GET store/{id} need for exposure)
Parameters:
JarS3BucketParam:
Type: String
Default: 'jambolan-function'
JarFilenameParam:
Type: String
Default: 'jambolanapi-LATEST.jar'
ApiStageNameParam:
Type: String
Default: 'LATEST'
UserPoolArnParam:
Type: String
TwilioAccountSIdParam:
Type: String
TwilioAuthTokenParam:
Type: String
TwilioMessageServiceIdParam:
Type: String
Resources:
# ----- LAMBDA RESOURCES ------
# IAM Role that will be assumed by CreateStore and FindStoreById
JambolanLambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
# Policy associated with JambolanLambdaExecutionRole.
# This gives CreateStore and FindStoreById permission to access JAMBOLAN table in dynamodb
# and also to to write logs to CloudWatch
JambolanLambdaExecutionPolicy:
DependsOn:
- JambolanLambdaExecutionRole
- JambolanDynamoDBTable
Type: AWS::IAM::Policy
Properties:
PolicyName: LambdaJambolanDataAccessPolicy
Roles:
- Ref: JambolanLambdaExecutionRole
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:*
Resource:
- Fn::Join:
- ''
- - 'arn:aws:logs:'
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":log-group:/aws/lambda/*"
- Effect: Allow
Action:
- dynamodb:BatchGetItem
- dynamodb:GetItem
- dynamodb:Query
- dynamodb:Scan
- dynamodb:BatchWriteItem
- dynamodb:PutItem
- dynamodb:UpdateItem
Resource:
- Fn::GetAtt:
- JambolanDynamoDBTable
- Arn
# Create Store Lambda function
CreateStore:
DependsOn:
- JambolanLambdaExecutionRole
Type: AWS::Lambda::Function
Properties:
FunctionName: CreateStoreFuntion
MemorySize: 256
Code:
S3Bucket:
Ref: JarS3BucketParam
S3Key:
Ref: JarFilenameParam
Role:
Fn::GetAtt:
- JambolanLambdaExecutionRole
- Arn
Handler: com.three55.jambolanapi.function.CreateStoreFunction::handle
Environment:
Variables:
TWILIO_ACCOUNT_SID:
Ref: TwilioAccountSIdParam
TWILIO_AUTH_TOKEN:
Ref: TwilioAuthTokenParam
TWILIO_MESSAGING_SERVICE_ID:
Ref: TwilioMessageServiceIdParam
Runtime: java8
Timeout: 25
# Find Store by ID Lambda function
FindStoreById:
DependsOn:
- JambolanLambdaExecutionRole
Type: AWS::Lambda::Function
Properties:
FunctionName: FindStoreByIdFunction
MemorySize: 256
Code:
S3Bucket:
Ref: JarS3BucketParam
S3Key:
Ref: JarFilenameParam
Role:
Fn::GetAtt:
- JambolanLambdaExecutionRole
- Arn
Handler: com.three55.jambolanapi.function.FindStoreFunction::handle
Runtime: java8
Timeout: 25
## ------ DYNAMODB RESOURCES -------
# JAMBALON TABLE
# itemPK and itemSK for primary and sort key respectively
# Global Secondary Index to search a store by country (PK) and store name (SK)
JambolanDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: JAMBOLAN
AttributeDefinitions:
- AttributeName: itemPK
AttributeType: S
- AttributeName: itemSK
AttributeType: S
- AttributeName: name
AttributeType: S
- AttributeName: addrCountry
AttributeType: S
- AttributeName: adminLogin
AttributeType: S
KeySchema:
- AttributeName: itemPK
KeyType: HASH
- AttributeName: itemSK
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
GlobalSecondaryIndexes:
- IndexName: StoresAndCountryIndex
KeySchema:
- AttributeName: addrCountry
KeyType: HASH
- AttributeName: name
KeyType: RANGE
Projection:
ProjectionType: KEYS_ONLY
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
- IndexName: StoresAdminIndex
KeySchema:
- AttributeName: adminLogin
KeyType: HASH
Projection:
ProjectionType: KEYS_ONLY
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
## ------ API GATEWAY RESOURCES -------
StoreApi:
Type: AWS::ApiGateway::RestApi
Properties:
Description: API for accessing store data
Name: Store API
FailOnWarnings: true
CreateStoreFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- CreateStore
- Arn
Principal: apigateway.amazonaws.com
SourceArn:
Fn::Join:
- ''
- - 'arn:aws:execute-api:'
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":"
- Ref: StoreApi
- "/*"
FindStoreByFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName:
Fn::GetAtt:
- FindStoreById
- Arn
Principal: apigateway.amazonaws.com
SourceArn:
Fn::Join:
- ''
- - 'arn:aws:execute-api:'
- Ref: AWS::Region
- ":"
- Ref: AWS::AccountId
- ":"
- Ref: StoreApi
- "/*"
StoreApiStage:
DependsOn:
- ApiGatewayAccount
Type: AWS::ApiGateway::Stage
Properties:
DeploymentId:
Ref: ApiDeployment
MethodSettings:
- DataTraceEnabled: true
HttpMethod: "*"
LoggingLevel: INFO
ResourcePath: "/*"
RestApiId:
Ref: StoreApi
StageName:
Ref: ApiStageNameParam
ApiGatewayCloudWatchLogsRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- apigateway.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: ApiGatewayLogsPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:GetLogEvents
- logs:FilterLogEvents
Resource: "*"
ApiGatewayAccount:
Type: AWS::ApiGateway::Account
Properties:
CloudWatchRoleArn:
Fn::GetAtt:
- ApiGatewayCloudWatchLogsRole
- Arn
ApiDeployment:
Type: AWS::ApiGateway::Deployment
DependsOn:
- FindStoreRequest
Properties:
RestApiId:
Ref: StoreApi
StageName: DummyStage
# /stores resource
StoresResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId:
Ref: StoreApi
ParentId:
Fn::GetAtt:
- StoreApi
- RootResourceId
PathPart: 'stores'
# POST /stores
CreateStoreRequest:
DependsOn:
- CreateStoreFunctionPermission
- JambolanAuthorizer
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: COGNITO_USER_POOLS
AuthorizationScopes:
- https://5kkqz6p3pl.execute-api.us-west-1.amazonaws.com/dev-stage/stores/stores.read
- https://5kkqz6p3pl.execute-api.us-west-1.amazonaws.com/dev-stage/stores/stores.write
AuthorizerId:
Ref: JambolanAuthorizer
HttpMethod: POST
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri:
Fn::Join:
- ''
- - 'arn:aws:apigateway:'
- Ref: AWS::Region
- ":lambda:path/2015-03-31/functions/"
- Fn::GetAtt:
- CreateStore
- Arn
- "/invocations"
IntegrationResponses:
- StatusCode: 200
- StatusCode: 400
SelectionPattern: '^(\[400\]).*$'
- StatusCode: 404
SelectionPattern: '^(\[404\]).*$'
- StatusCode: 500
SelectionPattern: '^(\[500\]).*$'
- StatusCode: 502
SelectionPattern: '^.*error.*$'
- StatusCode: 503
SelectionPattern: '^(\[503\]).*$'
PassthroughBehavior: WHEN_NO_TEMPLATES
ResourceId:
Ref: StoresResource
RestApiId:
Ref: StoreApi
MethodResponses:
- StatusCode: 200
- StatusCode: 400
- StatusCode: 404
- StatusCode: 500
- StatusCode: 502
- StatusCode: 503
OperationName: CreateStore
# /stores/{id} resource
StoreResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId:
Ref: StoreApi
ParentId:
Ref: StoresResource
PathPart: '{id}'
JambolanAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
IdentitySource: method.request.header.Authorization
Name: JambolanAuthorizer
ProviderARNs:
- Ref: UserPoolArnParam
RestApiId:
Ref: StoreApi
Type: COGNITO_USER_POOLS
# GET /stores/{storeId}
FindStoreRequest:
DependsOn:
- FindStoreByFunctionPermission
- JambolanAuthorizer
Type: AWS::ApiGateway::Method
Properties:
AuthorizationType: COGNITO_USER_POOLS
AuthorizationScopes:
- https://5kkqz6p3pl.execute-api.us-west-1.amazonaws.com/dev-stage/stores/stores.read
- https://5kkqz6p3pl.execute-api.us-west-1.amazonaws.com/dev-stage/stores/stores.write
AuthorizerId:
Ref: JambolanAuthorizer
HttpMethod: GET
RequestParameters:
method.request.path.id: True
Integration:
Type: AWS
IntegrationHttpMethod: POST
Uri:
Fn::Join:
- ''
- - 'arn:aws:apigateway:'
- Ref: AWS::Region
- ":lambda:path/2015-03-31/functions/"
- Fn::GetAtt:
- FindStoreById
- Arn
- "/invocations"
IntegrationResponses:
- StatusCode: 200
- StatusCode: 404
SelectionPattern: '^(\[404\]).*$'
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestParameters:
integration.request.path.storeId: method.request.path.id
#integration.request.header.Authorization: method.request.header.Authorization
RequestTemplates:
application/json: "\"$input.params('id')\""
ResourceId:
Ref: StoreResource
RestApiId:
Ref: StoreApi
MethodResponses:
- StatusCode: 200
- StatusCode: 404
OperationName: FindStoreById
Outputs:
RootUrl:
Description: Root URL of the API gateway
Value:
Fn::Join:
- ''
- - https://
- Ref: StoreApi
- ".execute-api."
- Ref: AWS::Region
- ".amazonaws.com"
...