Send payload to AWS Lambda from Cloudwatch scheduled eventΒΆ

How to send payload to AWS Lambda from Cloudwatch scheduled event.

Here we invoke the lambda every 5 minutes and send JSON payload [{ customer: \'client1\' }]

  • When the lambda is invoked the payload will be available in the event.

import * as cdk from 'aws-cdk-lib';
import * as events from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import * as lambda from 'aws-cdk-lib/aws-lambda';

export class MyStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
   super(scope, id, props);

   const myLambda = new lambda.Function(this, 'MyLambda', {
      runtime: lambda.Runtime.NODEJS_14_X,
      handler: 'index.handler',
      code: lambda.Code.fromAsset('lambda'),
   });

   const rule = new events.Rule(this, 'MyRule', {
      schedule: events.Schedule.rate(cdk.Duration.minutes(5)),
   });

   rule.addTarget(new targets.LambdaFunction(myLambda, {
      event: events.RuleTargetInput.fromObject({ customer: 'client1' }),
   }));
}
}

const app = new cdk.App();
new MyStack(app, 'MyStack');
app.synth();

Comments

comments powered by Disqus