Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudwatch Event putTargets not adding Lambda event sources

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.

like image 782
johnny Avatar asked Jun 01 '16 14:06

johnny


2 Answers

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.

like image 59
Raphael Fialho Avatar answered Sep 22 '22 11:09

Raphael Fialho


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");
        }
    }); 
like image 39
Đại Ngọc Nguyễn Avatar answered Sep 22 '22 11:09

Đại Ngọc Nguyễn