1

如何使用 Java API 为现有 AWS Lambda 函数添​​加新触发器?

我想添加CloudWatch Events - Schedule触发器。

看起来我应该使用AmazonCloudWatchEventsClient. 如何为客户端设置凭据?

任何示例将不胜感激。

谢谢。

4

2 回答 2

4

可以通过 aws sdk 添加事件源。我遇到了同样的问题,请参阅下面的代码作为使用 java 的解决方案。

AddPermissionRequest addPermissionRequest = new AddPermissionRequest();
addPermissionRequest.setStatementId("12345ff");  //any unique string would go
addPermissionRequest.withSourceArn(ruleArn); //CloudWatch rule's arn
addPermissionRequest.setAction("lambda:InvokeFunction");
addPermissionRequest.setPrincipal("events.amazonaws.com");
addPermissionRequest.setFunctionName("name of your lambda function");

AWSLambdaAsyncClient lambdaClient = new AWSLambdaAsyncClient();
lambdaClient.withRegion(Regions.US_EAST_1); //region of your lambda's location

lambdaClient.addPermission(addPermissionRequest);
于 2017-06-22T12:38:08.017 回答
0

感谢我自己在 Kotlin 中需要它,上一个答案中缺少的是依赖项:

compile 'com.amazonaws:aws-java-sdk-lambda:1.11.520'

代码:

val addPermissionRequest = AddPermissionRequest()
addPermissionRequest.statementId = "12345ff"  //any unique string would go
addPermissionRequest.withSourceArn(ruleArn) //CloudWatch rule's arn
addPermissionRequest.action = "lambda:InvokeFunction"
addPermissionRequest.principal = "events.amazonaws.com"
addPermissionRequest.functionName = "name of your lambda function"

val lambdaClient = AWSLambdaAsyncClient.builder().build()
lambdaClient.addPermission(addPermissionRequest)
于 2019-03-25T16:34:52.220 回答