1

How can I add new trigger for existing AWS Lambda function using Java API?

I would like to add CloudWatch Events - Schedule trigger.

It looks like I should use AmazonCloudWatchEventsClient. How can I set the credentials for the client?

Any examples will be appreciated.

Thanks.

2 Answers 2

4

It is possible to add event sources via aws sdk. I faced the same issue and please see code below as the solution using 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);
Sign up to request clarification or add additional context in comments.

2 Comments

This works fine for me, but only problem is AWSLambdaAsyncClient is deprecated.
@chaitanya it works also with AWSLambda client AWSLambda lambdaClient = AWSLambdaClientBuilder.defaultClient();
0

Thanks needed it in Kotlin myself, the thing missing from the previous answer was the dependency:

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

code:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.