import { Stack , Construct , StackProps , RemovalPolicy } from '@aws-cdk/core' ; import { Function , Code , Runtime } from '@aws-cdk/aws-lambda' ; import { Table , AttributeType } from "@aws-cdk/aws-dynamodb" ; import { Schedule , Rule } from '@aws-cdk/aws-events' ; import { LambdaFunction } from '@aws-cdk/aws-events-targets' ; export class TheScheduledLambdaStack extends Stack { constructor ( scope , id , props ? ) { super ( scope , id , props ) ; const table = new Table ( this , 'RequestTable' , { partitionKey : { name : 'requestid' , type : AttributeType . STRING } , removalPolicy : RemovalPolicy . DESTROY } ) ; const scheduledLambda = new Function ( this , 'scheduledLambda' , { runtime : Runtime . NODEJS_12_X , code : Code . fromAsset ( 'lambda-fns' ) , handler : 'index.handler' , environment : { TABLE_NAME : table . tableName } , } ) ; table . grantWriteData ( scheduledLambda ) ; const schedule = new Rule ( this , 'scheduledLambda-schedule' , { schedule : Schedule . expression ( 'rate(2 minutes)' ) , } ) ; schedule . addTarget ( new LambdaFunction ( scheduledLambda ) ) ; } }