@@ -7,9 +7,11 @@ import {
7
7
aws_events as events ,
8
8
aws_events_targets as targets ,
9
9
aws_iam as iam ,
10
+ aws_lambda as lambda ,
10
11
aws_lambda_nodejs as nodejsLambda ,
11
12
aws_s3 as s3 ,
12
13
aws_s3_notifications as s3n ,
14
+ aws_sqs as sqs ,
13
15
Duration ,
14
16
NestedStack ,
15
17
NestedStackProps ,
@@ -41,6 +43,7 @@ interface ChannelsStackProps extends NestedStackProps {
41
43
resourceConfig : ChannelsResourceConfig ;
42
44
tags : { [ key : string ] : string } ;
43
45
cognitoCleanupScheduleExp : string ;
46
+ stageCleanupScheduleExp : string ;
44
47
}
45
48
46
49
export class ChannelsStack extends NestedStack {
@@ -67,7 +70,12 @@ export class ChannelsStack extends NestedStack {
67
70
const region = Stack . of ( this . nestedStackParent ! ) . region ;
68
71
const nestedStackName = 'Channels' ;
69
72
const stackNamePrefix = `${ parentStackName } -${ nestedStackName } ` ;
70
- const { resourceConfig, cognitoCleanupScheduleExp, tags } = props ;
73
+ const {
74
+ resourceConfig,
75
+ cognitoCleanupScheduleExp,
76
+ stageCleanupScheduleExp,
77
+ tags
78
+ } = props ;
71
79
72
80
// Configuration variables based on the stage (dev or prod)
73
81
const {
@@ -471,7 +479,7 @@ export class ChannelsStack extends NestedStack {
471
479
472
480
// Cleanup idle stages users policies
473
481
const deleteIdleStagesIvsPolicyStatement = new iam . PolicyStatement ( {
474
- actions : [ 'ivs:ListStages' , 'ivs:DeleteStage' ] ,
482
+ actions : [ 'ivs:ListStages' , 'ivs:DeleteStage' , 'dynamodb:BatchWriteItem' ] ,
475
483
effect : iam . Effect . ALLOW ,
476
484
resources : [ '*' ]
477
485
} ) ;
@@ -488,6 +496,23 @@ export class ChannelsStack extends NestedStack {
488
496
resources : [ userPool . userPoolArn ]
489
497
} ) ;
490
498
499
+ // Cleanup idle stages lambda
500
+ const cleanupIdleStagesHandler = new nodejsLambda . NodejsFunction (
501
+ this ,
502
+ `${ stackNamePrefix } -CleanupIdleStages-Handler` ,
503
+ {
504
+ ...defaultLambdaParams ,
505
+ logRetention : RetentionDays . ONE_WEEK ,
506
+ functionName : `${ stackNamePrefix } -CleanupIdleStages` ,
507
+ entry : getLambdaEntryPath ( 'cleanupIdleStages' ) ,
508
+ timeout : Duration . minutes ( 10 ) ,
509
+ initialPolicy : [ deleteIdleStagesIvsPolicyStatement ] ,
510
+ environment : {
511
+ STACK_TAG : parentStackName
512
+ }
513
+ }
514
+ ) ;
515
+
491
516
// Cleanup unverified users lambda
492
517
const cleanupUnverifiedUsersHandler = new nodejsLambda . NodejsFunction (
493
518
this ,
@@ -505,6 +530,18 @@ export class ChannelsStack extends NestedStack {
505
530
}
506
531
) ;
507
532
533
+ // Scheduled cleanup idle stages lambda function
534
+ new events . Rule ( this , 'Cleanup-Idle-Stages-Schedule-Rule' , {
535
+ schedule : events . Schedule . expression ( stageCleanupScheduleExp ) ,
536
+ ruleName : `${ stackNamePrefix } -CleanupIdleStages-Schedule` ,
537
+ targets : [
538
+ new targets . LambdaFunction ( cleanupIdleStagesHandler , {
539
+ maxEventAge : Duration . minutes ( 2 ) ,
540
+ retryAttempts : 2
541
+ } )
542
+ ]
543
+ } ) ;
544
+
508
545
// Scheduled cleanup unverified users lambda function
509
546
new events . Rule ( this , 'Cleanup-Unverified-Users-Schedule-Rule' , {
510
547
schedule : events . Schedule . expression ( cognitoCleanupScheduleExp ) ,
0 commit comments