import * as cdk from '@aws-cdk/core' ; import * as lambda from '@aws-cdk/aws-lambda' ; import { Rule , Schedule } from "@aws-cdk/aws-events" ; import { LambdaFunction } from "@aws-cdk/aws-events-targets" ; import * as path from 'path' ; export class EventbridgeScheduledLambdaCdkStack extends cdk . Stack { constructor ( scope , id , props ? ) { super ( scope , id , props ) ; const interval_in_minutes = this . node . tryGetContext ( 'interval_in_minutes' ) ; if ( typeof interval_in_minutes === 'undefined' ) { console . log ( 'example: cdk deploy --context internal_in_minutes=5' ) ; process . exit ( 1 ) ; } console . log ( "interval_in_minutes" ) ; console . log ( interval_in_minutes ) ; const myFunction = new lambda . Function ( this , 'function-name' , { runtime : lambda . Runtime . NODEJS_14_X , memorySize : 128 , timeout : cdk . Duration . seconds ( 30 ) , handler : 'index.handler' , code : lambda . Code . fromAsset ( path . join ( __dirname , '/../src' ) ) , environment : { interval_in_minutes : interval_in_minutes } , } ) ; const cronRule = new Rule ( this , 'CronRule' , { schedule : Schedule . expression ( 'cron(0/' + interval_in_minutes + ' * * * ? *)' ) } ) ; cronRule . addTarget ( new LambdaFunction ( myFunction ) ) ; } }