Invoking AWS lambda on a schedule using the Go CDK

Viewed 26

I have an awslambda.Function that I've configured using the Go CDK. I want to trigger this lambda every night at 2 AM UTC. To this end, I am trying to define a CloudWatch Event alike the one from the below JavaScript example:

const newLambda = new lambda.Function(this, 'newLambda',{
  runtime: lambda.Runtime.PYTHON_3_8,
  code: lambda.Code.fromAsset('functions'),
  handler: 'index.handler',
});
const eventRule = new events.Rule(this, 'scheduleRule', {
  schedule: events.Schedule.cron({ minute: '0', hour: '1' }),
});
eventRule.addTarget(new targets.LambdaFunction(newLambda))

I believe that I need to use awsevents.NewRule, but I cannot figure out how to pass my lambda where the "WHAT GOES HERE?" placeholder is written. Note that I am using v2 of the aws-cdk.

event := awsevents.NewRule(stack, aws.String("TriggerDownloadOrdersToS3LambdaEvent"), &awsevents.RuleProps{
    Schedule: awsevents.Schedule_Cron(&awsevents.CronOptions{Hour: aws.String("2")}),
    Targets:  &[]awsevents.IRuleTarget{WHAT GOES HERE?},
})

Does anyone know how I can define a recurring trigger for my lambda using the Go CDK?

1 Answers

You can create an IRuleTarget using the awseventstargets module:

&awsevents.RuleProps{
    Schedule: awsevents.Schedule_Cron(&awsevents.CronOptions{Hour: aws.String("2"), Minute: aws.String("0")}),
    Targets:  &[]awsevents.IRuleTarget{awseventstargets.NewLambdaFunction(lambda, nil)},
},
Related