11
votes

I am making a call to aws cloudWatchEvent putRule & PutTarget api through aws sdk to create a cloudWatch Rule and attach a target to it. My Target is a lambda function, the rule gets created, the target gets attached to the rule but when the rule triggers based on its schedule the target lambda function not trigger. So I looked further and found out that the event source under the lambda function is not added which makes it not trigger. If I create the rule and target through AWS console the event source gets created and everything works but not thorugh API.

5

5 Answers

8
votes

You'll need to call the lambda add-permission after adding the target.

That is (via boto3 for me):

  • create the lambda
  • create the rule
  • create the targets
  • call lambda add-permission with the lambda arn

see boto3 documentation or the cli doc.

2
votes

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);
                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);
1
votes

I had same issue here, and i solve this by what @Anvita Shukla has sugested.

This worked fine when i do:

  • create the lambda (this i was created in web page)

And with SDK

  • create the rule object
  • create the target object
  • put request of the rule
  • put request of the target
  • get response object of rule request to retrieve the rule ARN
  • create permission object (has @Anvita Shukla said) and set the rule ARN
  • add permission by lambda client object

In the aws lambda page i can see my lambdas with associated triggers events. And in aws cloudwatch events page i can see the created rules. I wrote this in java lang. If you want i can share the code.

1
votes

I fixed it. You need to add permission for lambda with SourceArn is cloud watch after putTargets. For example :

var lambdaPermission = {
        FunctionName: 'cloudwatch-trigger',
        StatementId : timestamp.toString(),
        Action: 'lambda:InvokeFunction',
        Principal: 'events.amazonaws.com', 
        SourceArn: 'arn:aws:events:ap-southeast-1:XXXXXX:rule/schedule_auto_1'
    };

    lambda.addPermission(lambdaPermission, function(err, data) {
        if (err) {
            console.log("Error", err);
        } else {
            console.log("Success", data);
            console.log("add permisson done");
        }
    }); 
0
votes

As far as I understand this is currently not possible through the SDK, CloudWatch event sources can only be added to lambdas through the console as you said or using the CLI. If I'm wrong I would love to know what is possible, but the documentation here seems to agree. http://docs.aws.amazon.com/lambda/latest/dg/with-scheduled-events.html