[英]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.我正在尝试使用 java aws cdk 创建一个 cloudwatch 规则来触发我的 lambda function。 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.您可能已经创建了 lambda,但这里是一个工作示例,我们创建 lambda 然后将其添加到 cloudwatch 规则,因此 lambda 将每 15 分钟触发一次。
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.