-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.yml
More file actions
890 lines (845 loc) · 28.7 KB
/
template.yml
File metadata and controls
890 lines (845 loc) · 28.7 KB
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: |
SAM template for a banking transaction system with intuitive transaction types.
Includes Cognito User Pool authentication for API Gateway and a custom login/refresh proxy.
Parameters:
Environment:
Type: String
Description: The deployment environment (e.g. dev, test, prod).
AllowedValues:
- dev
- test
- prod
Default: dev
AwsRegion:
Type: String
Description: The region to deploy the infrastructure in.
Default: eu-west-2
SesEnabled:
Type: String
Description: Whether to enable SES email sending (true/false).
AllowedValues:
- true
- false
Default: true
Globals:
Function:
Runtime: python3.12
MemorySize: 128
Timeout: 90
Tracing: Active
Environment:
Variables:
POWERTOOLS_LOG_LEVEL: INFO
POWERTOOLS_SERVICE_NAME: !Sub ${AWS::StackName}
ENVIRONMENT_NAME: !Ref Environment
SES_ENABLED: !Ref SesEnabled
SES_SENDER_EMAIL: !Sub '{{resolve:ssm:/banking-app/${Environment}/SesSenderEmail}}'
SES_NO_REPLY_EMAIL: !Sub '{{resolve:ssm:/banking-app/${Environment}/SesNoReplyEmail}}'
SES_REPLY_EMAIL: !Sub '{{resolve:ssm:/banking-app/${Environment}/SesReplyEmail}}'
SES_BOUNCE_EMAIL: !Sub '{{resolve:ssm:/banking-app/${Environment}/SESBounceEmail}}'
Resources:
# --- Cognito User Pool ---
BankingUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: !Sub ${AWS::StackName}-user-pool
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
RequireSymbols: true
RequireUppercase: true
AutoVerifiedAttributes:
- email
UsernameAttributes:
- email
Schema:
- Name: email
AttributeDataType: String
Mutable: true
Required: true
AccountRecoverySetting:
RecoveryMechanisms:
- Name: verified_email
Priority: 1
LambdaConfig:
PostConfirmation: !GetAtt CognitoPostSignUpFunction.Arn
BankingUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
ClientName: !Sub ${AWS::StackName}-user-pool-client
UserPoolId: !Ref BankingUserPool
ExplicitAuthFlows:
- ALLOW_ADMIN_USER_PASSWORD_AUTH
- ALLOW_REFRESH_TOKEN_AUTH
GenerateSecret: false
AccessTokenValidity: 15
IdTokenValidity: 15
RefreshTokenValidity: 30
# --- API Gateway ---
BankingApiGateway:
Type: AWS::Serverless::Api
Properties:
Name: !Sub ${AWS::StackName}-transaction-api
StageName: !Ref Environment
EndpointConfiguration: REGIONAL
TracingEnabled: true
Auth:
DefaultAuthorizer: CognitoAuthorizer
Authorizers:
CognitoAuthorizer:
UserPoolArn: !GetAtt BankingUserPool.Arn
Domain:
DomainName: !Sub '{{resolve:ssm:/banking-app/${Environment}/DomainName}}'
CertificateArn: !Ref ApiCertificate
EndpointConfiguration: REGIONAL
Route53:
HostedZoneId: !Sub '{{resolve:ssm:/banking-app/${Environment}/Route53HostedZoneId}}'
# --- Lambda Functions ---
RequestTransactionFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-request-transaction
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: RequestTransactionFunction
CodeUri: functions/transactions/request_transaction/
Handler: request_transaction.app.lambda_handler
Environment:
Variables:
TRANSACTIONS_TABLE_NAME: !Ref TransactionsTable
DYNAMODB_ENDPOINT: ''
COGNITO_USER_POOL_ID: !Ref BankingUserPool
COGNITO_CLIENT_ID: !Ref BankingUserPoolClient
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAuthenticationLambdaLayer
Events:
ApiSaveTransactionEvent:
Type: Api
Properties:
Path: /transactions
Method: POST
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TransactionsTable
GetTransactionsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-get-transactions
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: GetTransactionsFunction
CodeUri: functions/transactions/get_transactions/
Handler: get_transactions.app.lambda_handler
Environment:
Variables:
TRANSACTIONS_TABLE_NAME: !Ref TransactionsTable
DYNAMODB_ENDPOINT: ''
COGNITO_USER_POOL_ID: !Ref BankingUserPool
COGNITO_CLIENT_ID: !Ref BankingUserPoolClient
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAuthenticationLambdaLayer
Events:
ApiGetAllTransactionsEvent:
Type: Api
Properties:
Path: /transactions
Method: GET
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
ApiGetTransactionByIdEvent:
Type: Api
Properties:
Path: /transactions/{id}
Method: GET
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TransactionsTable
ProcessTransactionsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-process-transactions
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: ProcessTransactionsFunction
CodeUri: functions/transactions/process_transactions/
Handler: process_transactions.app.lambda_handler
Environment:
Variables:
ACCOUNTS_TABLE_NAME: !Ref AccountsTable
TRANSACTIONS_TABLE_NAME: !Ref TransactionsTable
TRANSACTION_PROCESSING_DLQ_URL: !Ref TransactionProcessingDLQ
DYNAMODB_ENDPOINT: ''
SQS_ENDPOINT: ''
COGNITO_USER_POOL_ID: !Ref BankingUserPool
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAccountsLambdaLayer
Events:
DynamoDBStream:
Type: DynamoDB
Properties:
Stream: !GetAtt TransactionsTable.StreamArn
StartingPosition: TRIM_HORIZON
BatchSize: 10
MaximumRetryAttempts: 1
DestinationConfig:
OnFailure:
Type: SQS
Destination: !GetAtt TransactionProcessingDLQ.Arn
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref TransactionsTable
- DynamoDBCrudPolicy:
TableName: !Ref AccountsTable
- DynamoDBStreamReadPolicy:
TableName: !Ref TransactionsTable
StreamName: !Select
- 3
- !Split
- /
- !GetAtt TransactionsTable.StreamArn
- Statement:
Effect: Allow
Action:
- cognito-idp:AdminGetUser
Resource: !GetAtt BankingUserPool.Arn
- Statement:
Effect: Allow
Action:
- sqs:SendMessage
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
Resource: !GetAtt TransactionProcessingDLQ.Arn
GetAccountsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-get-accounts
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: GetAccountsFunction
CodeUri: functions/accounts/get_accounts/
Handler: get_accounts.app.lambda_handler
Environment:
Variables:
ACCOUNTS_TABLE_NAME: !Ref AccountsTable
DYNAMODB_ENDPOINT: ''
COGNITO_USER_POOL_ID: !Ref BankingUserPool
COGNITO_CLIENT_ID: !Ref BankingUserPoolClient
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAuthenticationLambdaLayer
Events:
ApiGetAllAccountsEvent:
Type: Api
Properties:
Path: /accounts
Method: GET
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
ApiGetAccountByIdEvent:
Type: Api
Properties:
Path: /accounts/{account_id}
Method: GET
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref AccountsTable
GetAccountTransactionsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-get-account-transactions
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: GetAccountTransactionsFunction
CodeUri: functions/accounts/get_account_transactions
Handler: get_account_transactions.app.lambda_handler
Environment:
Variables:
TRANSACTIONS_TABLE_NAME: !Ref TransactionsTable
DYNAMODB_ENDPOINT: ''
Layers:
- !Ref PythonHelpersLambdaLayer
Events:
ApiGetAccountTransactionsEvent:
Type: Api
Properties:
Path: /accounts/{account_id}/transactions
Method: GET
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
Policies:
- DynamoDBReadPolicy:
TableName: !Ref TransactionsTable
AuthFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-auth
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: AuthFunction
CodeUri: functions/auth/
Handler: auth.app.lambda_handler
Environment:
Variables:
COGNITO_USER_POOL_ID: !Ref BankingUserPool
COGNITO_CLIENT_ID: !Ref BankingUserPoolClient
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAuthenticationLambdaLayer
Events:
ApiLoginEvent:
Type: Api
Properties:
Path: /auth/login
Method: POST
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: NONE
ApiTokenRefreshEvent:
Type: Api
Properties:
Path: /auth/refresh
Method: POST
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: NONE
Policies:
- Statement:
Effect: Allow
Action:
- cognito-idp:InitiateAuth
- cognito-idp:RespondToAuthChallenge
- cognito-idp:AdminInitiateAuth
- cognito-idp:AdminRespondToAuthChallenge
Resource: !GetAtt BankingUserPool.Arn
CognitoPostSignUpFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-cognito-post-sign-up-function
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: CognitoPostSignUpFunction
CodeUri: functions/cognito/post_sign_up/
Handler: post_sign_up.app.lambda_handler
Environment:
Variables:
ACCOUNTS_TABLE_NAME: !Ref AccountsTable
DYNAMODB_ENDPOINT: ''
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAccountsLambdaLayer
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref AccountsTable
- Statement:
- Effect: Allow
Action:
- ses:SendEmail
- ses:SendRawEmail
Resource: '*'
MonthlyAccountReportsTriggerFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-monthly-account-reports-trigger-function
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: MonthlyAccountReportsTriggerFunction
CodeUri: functions/monthly_reports/accounts/trigger/
Handler: trigger.app.lambda_handler
Timeout: 300
Environment:
Variables:
ACCOUNTS_TABLE_NAME: !Ref AccountsTable
STATE_MACHINE_ARN: !Ref StateMachine
CONTINUATION_QUEUE_URL: !Ref MonthlyAccountReportsContinuationQueue
DLQ_URL: !Ref MonthlyAccountReportsContinuationDLQ
DYNAMODB_ENDPOINT: ''
SQS_ENDPOINT: ''
Events:
MonthlySchedule:
Type: Schedule
Properties:
Name: !Sub ${AWS::StackName}-monthly-accounts-reports-trigger
Schedule: cron(0 0 1 * ? *)
Description: !Sub Monthly accounts reports trigger for ${Environment}
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonMonthlyReportsLambdaLayer
ReservedConcurrentExecutions: 1
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref AccountsTable
- StepFunctionsExecutionPolicy:
StateMachineName: !GetAtt StateMachine.Name
- Statement:
Effect: Allow
Action:
- sqs:SendMessage
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
Resource: !GetAtt MonthlyAccountReportsContinuationQueue.Arn
- Statement:
Effect: Allow
Action:
- sqs:SendMessage
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
Resource: !GetAtt MonthlyAccountReportsContinuationDLQ.Arn
MonthlyAccountReportsProcessPendingReportsFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-monthly-accounts-reports-continuation
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: MonthlyAccountReportsProcessPendingReportsFunction
CodeUri: functions/monthly_reports/accounts/process_pending_reports/
Handler: process_pending_reports.app.lambda_handler
Timeout: 150
Environment:
Variables:
ACCOUNTS_TABLE_NAME: !Ref AccountsTable
STATE_MACHINE_ARN: !Ref StateMachine
CONTINUATION_QUEUE_URL: !Ref MonthlyAccountReportsContinuationQueue
DLQ_URL: !Ref MonthlyAccountReportsContinuationDLQ
DYNAMODB_ENDPOINT: ''
SQS_ENDPOINT: ''
Events:
SQSEvent:
Type: SQS
Properties:
Queue: !GetAtt MonthlyAccountReportsContinuationQueue.Arn
BatchSize: 1
Enabled: true
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonMonthlyReportsLambdaLayer
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref AccountsTable
- StepFunctionsExecutionPolicy:
StateMachineName: !GetAtt StateMachine.Name
- Statement:
Effect: Allow
Action:
- sqs:ReceiveMessage
- sqs:DeleteMessage
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
Resource: !GetAtt MonthlyAccountReportsContinuationQueue.Arn
- Statement:
Effect: Allow
Action:
- sqs:SendMessage
- sqs:GetQueueAttributes
- sqs:GetQueueUrl
Resource: !GetAtt MonthlyAccountReportsContinuationDLQ.Arn
CreateAccountsReportFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-create-accounts-report
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: CreateAccountsReportFunction
CodeUri: functions/monthly_reports/accounts/create_report
Handler: create_report.app.lambda_handler
Environment:
Variables:
REPORTS_BUCKET: !Ref MonthlyReportsBucket
Layers:
- !Ref PythonHelpersLambdaLayer
Policies:
- S3WritePolicy:
BucketName: !Ref MonthlyReportsBucket
- Statement:
Effect: Allow
Action:
- s3:GetObject
Resource: !Sub ${MonthlyReportsBucket.Arn}/*
AccountsReportsNotifyClientFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub ${AWS::StackName}-account-reports-notify-client
Description: !Sub
- Stack ${AWS::StackName} Function ${ResourceName} for Stage ${Environment}
- ResourceName: AccountsReportsNotifyClientFunction
CodeUri: functions/monthly_reports/accounts/notify_client
Handler: notify_client.app.lambda_handler
Environment:
Variables:
REPORTS_BUCKET: !Ref MonthlyReportsBucket
COGNITO_USER_POOL_ID: !Ref BankingUserPool
COGNITO_CLIENT_ID: !Ref BankingUserPoolClient
ACCOUNTS_TABLE_NAME: !Ref AccountsTable
DYNAMODB_ENDPOINT: ''
Events:
ApiRequestNewMonthlyAccountReportEvent:
Type: Api
Properties:
Path: /accounts/{account_id}/reports/{statement_period}
Method: GET
RestApiId: !Ref BankingApiGateway
Auth:
Authorizer: CognitoAuthorizer
Layers:
- !Ref PythonHelpersLambdaLayer
- !Ref PythonAuthenticationLambdaLayer
- !Ref PythonAccountsLambdaLayer
Policies:
- Statement:
Effect: Allow
Action:
- s3:GetObject
Resource: !Sub ${MonthlyReportsBucket.Arn}/*
- Statement:
Effect: Allow
Action:
- cognito-idp:AdminGetUser
Resource: !GetAtt BankingUserPool.Arn
- DynamoDBReadPolicy:
TableName: !Ref AccountsTable
- Statement:
- Effect: Allow
Action:
- ses:SendEmail
- ses:SendRawEmail
Resource: '*'
# --- Lambda Layers ---
PythonHelpersLambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub ${AWS::StackName}-banking-python-helpers-layer
ContentUri: layers/python/helpers
CompatibleRuntimes:
- python3.12
Metadata:
BuildMethod: python3.12
BuildArchitecture: x86_64
PythonAccountsLambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub ${AWS::StackName}-banking-python-accounts-layer
ContentUri: layers/python/accounts
CompatibleRuntimes:
- python3.12
Metadata:
BuildMethod: python3.12
BuildArchitecture: x86_64
PythonAuthenticationLambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub ${AWS::StackName}-banking-python-authentication-layer
ContentUri: layers/python/authentication
CompatibleRuntimes:
- python3.12
Metadata:
BuildMethod: python3.12
BuildArchitecture: x86_64
PythonMonthlyReportsLambdaLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: !Sub ${AWS::StackName}-banking-python-monthly-reports-layer
ContentUri: layers/python/monthly_reports
CompatibleRuntimes:
- python3.12
Metadata:
BuildMethod: python3.12
BuildArchitecture: x86_64
# --- Log Groups ---
RequestTransactionFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${RequestTransactionFunction}
RetentionInDays: 7
AuthFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${AuthFunction}
RetentionInDays: 7
ProcessTransactionsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${ProcessTransactionsFunction}
RetentionInDays: 7
GetTransactionsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${GetTransactionsFunction}
RetentionInDays: 7
CognitoPostSignUpFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${CognitoPostSignUpFunction}
RetentionInDays: 7
GetAccountsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${GetAccountsFunction}
RetentionInDays: 7
MonthlyAccountReportsTriggerFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${MonthlyAccountReportsTriggerFunction}
RetentionInDays: 7
MonthlyAccountReportsProcessPendingReportsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${MonthlyAccountReportsProcessPendingReportsFunction}
RetentionInDays: 7
GetAccountTransactionsFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${GetAccountTransactionsFunction}
RetentionInDays: 7
CreateAccountsReportFunctionLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${CreateAccountsReportFunction}
RetentionInDays: 7
AccountsReportsNotifyClientLogGroup:
Type: AWS::Logs::LogGroup
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
LogGroupName: !Sub /aws/lambda/${AccountsReportsNotifyClientFunction}
RetentionInDays: 7
# --- DynamoDB Table for Transactions ---
TransactionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub ${AWS::StackName}-transactions-table
KeySchema:
- AttributeName: idempotencyKey
KeyType: HASH
AttributeDefinitions:
- AttributeName: idempotencyKey
AttributeType: S
- AttributeName: id
AttributeType: S
- AttributeName: userId
AttributeType: S
- AttributeName: accountId
AttributeType: S
- AttributeName: createdAt
AttributeType: S
BillingMode: PAY_PER_REQUEST
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
SSESpecification:
SSEEnabled: true
GlobalSecondaryIndexes:
- IndexName: TransactionIdIndex
KeySchema:
- AttributeName: id
KeyType: HASH
Projection:
ProjectionType: ALL
- IndexName: UserIdIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: ALL
- IndexName: AccountDateIndex
KeySchema:
- AttributeName: accountId
KeyType: HASH
- AttributeName: createdAt
KeyType: RANGE
Projection:
ProjectionType: ALL
TimeToLiveSpecification:
AttributeName: ttlTimestamp
Enabled: true
StreamSpecification:
StreamViewType: NEW_IMAGE
# --- DynamoDB Table for Accounts ---
AccountsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub ${AWS::StackName}-accounts-table
KeySchema:
- AttributeName: accountId
KeyType: HASH
AttributeDefinitions:
- AttributeName: accountId
AttributeType: S
- AttributeName: userId
AttributeType: S
BillingMode: PAY_PER_REQUEST
PointInTimeRecoverySpecification:
PointInTimeRecoveryEnabled: true
SSESpecification:
SSEEnabled: true
GlobalSecondaryIndexes:
- IndexName: UserIdIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: ALL
TimeToLiveSpecification:
AttributeName: ttlTimestamp
Enabled: true
# --- SQS Queues ---
MonthlyAccountReportsContinuationQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub ${AWS::StackName}-monthly-account-reports-continuation-queue
VisibilityTimeout: 200
MessageRetentionPeriod: 1209600 # 14 days
ReceiveMessageWaitTimeSeconds: 20
RedrivePolicy:
deadLetterTargetArn: !GetAtt MonthlyAccountReportsContinuationDLQ.Arn
maxReceiveCount: 3
MonthlyAccountReportsContinuationDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub ${AWS::StackName}-monthly-account-reports-dlq
MonthlyAccountReportsCreationDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub ${AWS::StackName}-monthly-account-reports-creation-dlq
TransactionProcessingDLQ:
Type: AWS::SQS::Queue
Properties:
QueueName: !Sub ${AWS::StackName}-transaction-processing-dlq
# --- Permissions ---
CognitoPostSignUpFunctionPermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref CognitoPostSignUpFunction
Action: lambda:InvokeFunction
Principal: cognito-idp.amazonaws.com
SourceArn: !GetAtt BankingUserPool.Arn
# S3 Buckets
MonthlyReportsBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${AWS::StackName}-monthly-reports-bucket
VersioningConfiguration:
Status: Enabled
PublicAccessBlockConfiguration:
BlockPublicAcls: true
BlockPublicPolicy: true
IgnorePublicAcls: true
RestrictPublicBuckets: true
LifecycleConfiguration:
Rules:
- Id: TransitionToIA
Status: Enabled
Transitions:
- TransitionInDays: 90
StorageClass: STANDARD_IA
- TransitionInDays: 365
StorageClass: GLACIER
# Domain Configuration
ApiCertificate:
Type: AWS::CertificateManager::Certificate
Properties:
DomainName: !Sub '{{resolve:ssm:/banking-app/${Environment}/DomainName}}'
ValidationMethod: DNS
DomainValidationOptions:
- DomainName: !Sub '{{resolve:ssm:/banking-app/${Environment}/DomainName}}'
HostedZoneId: !Sub '{{resolve:ssm:/banking-app/${Environment}/Route53HostedZoneId}}'
# State machines
StateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Name: !Sub ${AWS::StackName}-monthly-reports-creation-state-machine
Type: STANDARD
Definition:
StartAt: GetAccountTransactions
States:
GetAccountTransactions:
Type: Task
Resource: !GetAtt GetAccountTransactionsFunction.Arn
Next: CreateAccountsReport
Retry:
- ErrorEquals: [ "States.ALL" ]
IntervalSeconds: 5
MaxAttempts: 3
BackoffRate: 2.0
Catch:
- ErrorEquals: [ "States.ALL" ]
ResultPath: $.error
Next: SendToDLQ
CreateAccountsReport:
Type: Task
Resource: !GetAtt CreateAccountsReportFunction.Arn
Next: NotifyClient
Retry:
- ErrorEquals: [ "States.ALL" ]
IntervalSeconds: 5
MaxAttempts: 3
BackoffRate: 2.0
Catch:
- ErrorEquals: [ "States.ALL" ]
ResultPath: $.error
Next: SendToDLQ
NotifyClient:
Type: Task
Resource: !GetAtt AccountsReportsNotifyClientFunction.Arn
End: true
Retry:
- ErrorEquals: [ "States.ALL" ]
IntervalSeconds: 10
MaxAttempts: 2
BackoffRate: 2.0
Catch:
- ErrorEquals: [ "States.ALL" ]
ResultPath: $.error
Next: SendToDLQ
SendToDLQ:
Type: Task
Resource: arn:aws:states:::sqs:sendMessage
Parameters:
QueueUrl: !Ref MonthlyAccountReportsCreationDLQ
MessageBody.$: $
End: true
Policies:
- LambdaInvokePolicy:
FunctionName: !Ref GetAccountTransactionsFunction
- LambdaInvokePolicy:
FunctionName: !Ref CreateAccountsReportFunction
- LambdaInvokePolicy:
FunctionName: !Ref AccountsReportsNotifyClientFunction
- SQSSendMessagePolicy:
QueueName: !GetAtt MonthlyAccountReportsCreationDLQ.QueueName