0
votes

I'm using AWS java sdk and need to delete a CloudWatch rule which is trigger a AWS Lambda function. The deletion of the CloudWatch rule was successful using below code. But there is a reference exist in the Lambda function and Lambda AWS console displays the CloudWatch rule as deleted and is displays a "Delete" icon to delete it. Any one know how to remove the reference to the CloudWatch rule from the Lambda function using Java SDK?

cloudWatchEvent.removeTargets(removeTargetsRequest);
cloudWatchEvent.deleteRule(deleteRequest);
1

1 Answers

0
votes

You need to remove the invoke:Lambda permission too from Lambda policy too while deleting the triggers. If you don't remove the permission, it will show up in the Lambda console.

If you don't know the statement-id associated with your trigger, extract the lambda policy using

AWSLambda client = AWSLambdaClientBuilder.standard().build();
GetPolicyRequest request = new GetPolicyRequest().withFunctionName("myFunction").withQualifier("1");
GetPolicyResult response = client.getPolicy(request);

And note down the statement id.

Next, remove this permission using

AWSLambda client = AWSLambdaClientBuilder.standard().build();
RemovePermissionRequest request = new RemovePermissionRequest().withFunctionName("myFunction").withStatementId("role-statement-id").withQualifier("1");
RemovePermissionResult response = client.removePermission(request);

Examples from here - https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/lambda/AWSLambda.html