@@ -7,9 +7,11 @@ import {
77 aws_events as events ,
88 aws_events_targets as targets ,
99 aws_iam as iam ,
10+ aws_lambda as lambda ,
1011 aws_lambda_nodejs as nodejsLambda ,
1112 aws_s3 as s3 ,
1213 aws_s3_notifications as s3n ,
14+ aws_sqs as sqs ,
1315 Duration ,
1416 NestedStack ,
1517 NestedStackProps ,
@@ -41,6 +43,7 @@ interface ChannelsStackProps extends NestedStackProps {
4143 resourceConfig : ChannelsResourceConfig ;
4244 tags : { [ key : string ] : string } ;
4345 cognitoCleanupScheduleExp : string ;
46+ stageCleanupScheduleExp : string ;
4447}
4548
4649export class ChannelsStack extends NestedStack {
@@ -67,7 +70,12 @@ export class ChannelsStack extends NestedStack {
6770 const region = Stack . of ( this . nestedStackParent ! ) . region ;
6871 const nestedStackName = 'Channels' ;
6972 const stackNamePrefix = `${ parentStackName } -${ nestedStackName } ` ;
70- const { resourceConfig, cognitoCleanupScheduleExp, tags } = props ;
73+ const {
74+ resourceConfig,
75+ cognitoCleanupScheduleExp,
76+ stageCleanupScheduleExp,
77+ tags
78+ } = props ;
7179
7280 // Configuration variables based on the stage (dev or prod)
7381 const {
@@ -471,7 +479,7 @@ export class ChannelsStack extends NestedStack {
471479
472480 // Cleanup idle stages users policies
473481 const deleteIdleStagesIvsPolicyStatement = new iam . PolicyStatement ( {
474- actions : [ 'ivs:ListStages' , 'ivs:DeleteStage' ] ,
482+ actions : [ 'ivs:ListStages' , 'ivs:DeleteStage' , 'dynamodb:BatchWriteItem' ] ,
475483 effect : iam . Effect . ALLOW ,
476484 resources : [ '*' ]
477485 } ) ;
@@ -488,6 +496,23 @@ export class ChannelsStack extends NestedStack {
488496 resources : [ userPool . userPoolArn ]
489497 } ) ;
490498
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+
491516 // Cleanup unverified users lambda
492517 const cleanupUnverifiedUsersHandler = new nodejsLambda . NodejsFunction (
493518 this ,
@@ -505,6 +530,18 @@ export class ChannelsStack extends NestedStack {
505530 }
506531 ) ;
507532
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+
508545 // Scheduled cleanup unverified users lambda function
509546 new events . Rule ( this , 'Cleanup-Unverified-Users-Schedule-Rule' , {
510547 schedule : events . Schedule . expression ( cognitoCleanupScheduleExp ) ,
0 commit comments