简体   繁体   中英

How can i create a cloudwatch rule to trigger a lambda function every 15 minutes using aws cdk (JAVA)?

I am trying to create a cloudwatch rule to trigger my lambda function using java aws cdk. I found something like below but it give me error.

Code:

Rule rule =
                Rule.Builder.create(this, "rule-" + id)
                        .schedule(Schedule.rate(Duration.minutes(15)))
                        .description("CloudWatch Event" + id)
                        .enabled(true)
                        .build();

        //rule.addTarget(alias);

        CfnEventSourceMapping eventSource = CfnEventSourceMapping.Builder
                .create(this, "EventSource")
                .functionName(lambda.getFunctionName())
                .eventSourceArn(rule.getRuleArn())
                .enabled(true)
                .build();

error:

Invalid request provided: Unrecognized event source, must be kinesis, dynamodb stream or sqs.

Please guide me here. Thank you.

You probably have your lambda created already but here is a working example where we create the lambda then add it to the cloudwatch rule so lambda will be triggered every 15 minutes.

Function lambda =
            Function.Builder.create(this, "my-lambda-name")
                    .runtime(Runtime.JAVA_11)
                    .code(LambdaCode)
                    .functionName("my-lambda-name")
                    .handler("handler_name")
                    .role(role)
                    .memorySize(3008)
                    .timeout(Duration.minutes(15))
                    .environment(LAMBDA_ENV)
                    .build();
    Rule rule =
                Rule.Builder.create(this, "rule-" + id)
                        .schedule(Schedule.rate(Duration.minutes(15)))
                        .description("CloudWatch Event" + id)
                        .build();

    rule.addTarget(new LambdaFunction(lambda));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM