-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
546 lines (531 loc) · 17.8 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
538
539
540
541
542
543
544
545
546
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
users-microservice
Sample SAM Template for users-microservice
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 60
MemorySize: 2048
Environment:
Variables:
TABLE_NAME: !Ref TableName
DEPLOY_ENVIRONMENT: !Ref DeployEnviroment
#AWS_REGION: !Ref AWSRegion
IS_OFFLINE: !Ref IsOffline #change this to false for deploy in aws or true for use in offline
DYNAMOHOST: !Ref DynamoHost
DYNAMO_PORT: 4566
SECRET_KEY: !Ref SecretKey
EXPIRATION: !Ref Expiration
ADMIN_EMAIL: !Ref AdminEmail
Parameters:
TableName:
Type: String
DeployEnviroment:
Type: String
IsOffline:
Type: String
DynamoHost:
Type: String
SecretKey:
Type: String
Expiration:
Type: String
AdminEmail:
Type: String
ffmpegLayerArn:
Type: String
WaterMarkImg:
Type: String
Resources:
# Define an SNS topic for User Service notifications
TopicUserService:
Type: "AWS::SNS::Topic"
Properties:
# Set the display name for the SNS topic
DisplayName: TopicUserService
# Set the name of the SNS topic
TopicName: TopicUserService
# Use the default AWS managed key for SNS encryption
KmsMasterKeyId: alias/aws/sns
# Add tags to the SNS topic (e.g., for identifying the environment)
Tags:
- Key: Environment
Value: Production
# Subscription:
# - Protocol: email
# Endpoint: !Ref emailValidator
# Define an SQS queue for User Service messages
UserQueue:
Type: "AWS::SQS::Queue"
Properties:
# Set the name of the SQS queue
QueueName: UserQueue
# Set the visibility timeout (in seconds) for messages in the queue
VisibilityTimeout: 300
# Set the maximum message size (in bytes) for the queue
# MaximumMessageSize: 2048
# Set the message retention period (in seconds) for the queue
MessageRetentionPeriod: 86400
# Set the dead-letter queue configuration for the queue
RedrivePolicy:
# Set the Amazon Resource Name (ARN) of the dead-letter queue
deadLetterTargetArn: !GetAtt UserDeadLetterQueue.Arn
# Set the maximum number of receives before moving messages to the dead-letter queue
maxReceiveCount: 1
# Add tags to the SQS queue (e.g., for identifying the environment)
Tags:
- Key: Environment
Value: Production
# Define an SQS dead-letter queue for User Service messages
UserDeadLetterQueue:
Type: "AWS::SQS::Queue"
Properties:
# Set the name of the dead-letter queue
QueueName: UserDeadLetterQueue
# Set the message retention period (in seconds) for the dead-letter queue
MessageRetentionPeriod: 1209600
# Define an SQS [Success] queue for Meta data lambda destination
MetaDataOnSuccessQueue:
Type: "AWS::SQS::Queue"
Properties:
# Set the name of the queue
QueueName: MetaDataOnSuccessQueue
# Set the message retention period (in seconds) for the dead-letter queue
MessageRetentionPeriod: 1209600
# Define an SQS [Fail] queue for Meta data lambda destination
MetaDataOnFailQueue:
Type: "AWS::SQS::Queue"
Properties:
# Set the name of the queue
QueueName: MetaDataOnFailQueue
# Set the message retention period (in seconds) for the dead-letter queue
MessageRetentionPeriod: 1209600
# Define an SQS Access Policy
SampleSQSPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref UserQueue # Use !Ref to get URL Queue
PolicyDocument:
Version: "2012-10-17"
Id: !Sub "topic-subscription-${UserQueue}"
Statement:
- Sid: !Sub "topic-subscription-${TopicUserService}"
Effect: Allow
Principal:
AWS: "*" #!Sub 'arn:aws:iam::${AWS::AccountId}:root'
Action: SQS:*
Resource: !GetAtt UserQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref TopicUserService
# Subscribe the SQS queue to the SNS topic
MySQSToSNSSubscription:
Type: "AWS::SNS::Subscription"
Properties:
# Set the protocol to 'sqs' for the SQS endpoint
Protocol: "sqs"
# Reference the ARN of the SNS topic to subscribe to
TopicArn: !Ref TopicUserService
# Reference the ARN of the SQS queue as the endpoint for the subscription
Endpoint: !GetAtt UserQueue.Arn
UserTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Ref TableName
AttributeDefinitions:
# Defining table attributes
- AttributeName: id
AttributeType: S
- AttributeName: username
AttributeType: S
- AttributeName: email
AttributeType: S
- AttributeName: createdAt
AttributeType: N
KeySchema:
# Primary Key Definition
- AttributeName: id
KeyType: HASH
- AttributeName: createdAt
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
- IndexName: UsernameIndex
KeySchema:
# Secondary Global Index Key Definition
- AttributeName: username
KeyType: HASH
Projection:
# Specifies which attributes should be projected to the index
ProjectionType: ALL
- IndexName: EmailIndex
KeySchema:
# Secondary Global Index Key Definition
- AttributeName: email
KeyType: HASH
Projection:
# Specifies which attributes should be projected to the index
ProjectionType: ALL
AvatarS3Bucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: avatars-bucket-ecv
AccessControl: Private
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: false
IgnorePublicAcls: false
RestrictPublicBuckets: false
WaterMarkS3Bucket:
Type: "AWS::S3::Bucket"
Properties:
BucketName: watter-mark-ecv
AccessControl: Private
OwnershipControls:
Rules:
- ObjectOwnership: BucketOwnerPreferred
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: false
IgnorePublicAcls: false
RestrictPublicBuckets: false
MySESVerifiedEmail:
Type: "AWS::SES::EmailIdentity"
Properties:
EmailIdentity: !Ref AdminEmail
SharedLayerLambda:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: SharedLayerLambda
ContentUri: layers/general/dist
CompatibleRuntimes:
- nodejs18.x
RetentionPolicy: Retain
Metadata:
BuildMethod: nodejs18.x
CreateUser:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: create-user.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
- SNSPublishMessagePolicy:
TopicName: !GetAtt TopicUserService.TopicName
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Environment:
Variables:
TOPIC_ARN: !Ref TopicUserService
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /create
Method: post
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- create-user.ts
NotificationUser:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: user-message.handler
Runtime: nodejs18.x
Policies:
- SQSPollerPolicy:
QueueName: !GetAtt UserQueue.QueueName
- AmazonSESFullAccess
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Events:
UserQueue:
Type: SQS
Properties:
Queue: !GetAtt UserQueue.Arn
BatchSize: 10
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- user-message.ts
MetaDataValidator:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: meta-data-validator.handler
Runtime: nodejs18.x
Architectures:
- x86_64
Policies:
- SQSSendMessagePolicy:
QueueName: !GetAtt MetaDataOnSuccessQueue.QueueName
- SQSSendMessagePolicy:
QueueName: !GetAtt MetaDataOnFailQueue.QueueName
- S3WritePolicy:
BucketName: !Ref WaterMarkS3Bucket
- S3ReadPolicy:
BucketName: !Ref WaterMarkS3Bucket
- S3ReadPolicy:
BucketName: avatars-bucket-ecv
Environment:
Variables:
BUCKET_NAME: !Ref WaterMarkS3Bucket
WATER_MARK_IMG: !Ref WaterMarkImg
EphemeralStorage:
Size: 10240
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
- !Ref ffmpegLayerArn
EventInvokeConfig:
DestinationConfig:
OnSuccess:
Type: SQS
Destination: !GetAtt MetaDataOnSuccessQueue.Arn
OnFailure:
Type: SQS
Destination: !GetAtt MetaDataOnFailQueue.Arn
Events:
MyS3Event:
Type: S3
Properties:
Bucket: !Ref AvatarS3Bucket
Events: s3:ObjectCreated:*
# Filter:
# S3Key:
# Rules:
# - Name: suffix
# Value: .png
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- meta-data-validator.ts
GetUserById:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: get-user-by-id.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /get/{userId}
Method: get
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- get-user-by-id.ts
UpdateAvatarUser:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: update-avatar.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
- S3WritePolicy:
BucketName: !Ref AvatarS3Bucket
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Environment:
Variables:
BUCKET_NAME: !Ref AvatarS3Bucket
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /avatar/{userId}
Method: patch
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- update-avatar.ts
GetUsers:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: get-users.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /get
Method: get
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- get-users.ts
DeleteUser:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: delete-user.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /delete/{userId}
Method: delete
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- delete-user.ts
UpdateUser:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: update-user.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /update/{userId}
Method: patch
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- update-user.ts
LoginUser:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: lambdas/src/functions/
Handler: login.handler
Runtime: nodejs18.x
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TableName
Architectures:
- x86_64
Layers:
- !Ref SharedLayerLambda # Reference to the Lambda Layer
Events:
UserAPI:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /login
Method: post
Metadata: # Manage esbuild properties
BuildMethod: esbuild
BuildProperties:
Minify: true
External:
- "@general"
Target: es2020
Sourcemap: false
EntryPoints:
- login.ts
ApplicationResourceGroup:
Type: AWS::ResourceGroups::Group
Properties:
Name:
Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
ResourceQuery:
Type: CLOUDFORMATION_STACK_1_0
ApplicationInsightsMonitoring:
Type: AWS::ApplicationInsights::Application
Properties:
ResourceGroupName:
Ref: ApplicationResourceGroup
AutoConfigurationEnabled: "true"
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
CreateUser:
Description: API Gateway endpoint URL for Prod stage for CreateUser function
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/create/"