0

I tried creating a cloudwatch event rule with the below code. However, it doesn't seem to be working. I checked AWS CloudWatch Events, and the rule is there with my attached lambda function. However, the event never gets triggered as it should. How can I fix this problem?

async function createCloudWatchEvent(zip){
    if(zip){
        try{
            await putRule(zip);
            await putTarget(zip);
        } catch (e) {
            console.log(`Can't create [${zip}] cloudwatch event`);
        }
    }
}

function putRule (ruleName) {
    let cloudWatchParams = {
      Name: ruleName, // required
      ScheduleExpression: 'rate(1 minute)',
      State: 'ENABLED'
    };

    return new Promise(resolve => {
        cloudwatchevent.putRule(cloudWatchParams, async function(err, data) {
          if(err){
            console.log(err);
            resolve(err);
          }else{
            console.log('Successfully created rule: ' + ruleName);
            resolve();
          }
        });
    });
}

function putTarget (ruleName) {
    let cloudWatchParams = {
      Rule: ruleName, // required
      Targets: [ // required
        {
          Arn: 'xxxxxxxxxx',
          Id: 'Determine_State',
          Input: JSON.stringify({zip: ruleName})
        }
      ]
    };

    return new Promise(resolve => {
        // Attach "Determine_State" target to cloudwatch event
        cloudwatchevent.putTargets(cloudWatchParams, function(err, data) {
          if(err){
            console.log(err);
            resolve(err);
          }else{
            console.log('Successfully attached target [' + cloudWatchParams.Targets[0].Id + '] to rule: ' + ruleName);
            resolve();
          }
        });
    });
}
Kulasangar
  • 9,046
  • 5
  • 51
  • 82
Alex Feng
  • 45
  • 1
  • 9

2 Answers2

0

I am not sure if you know t already, the cron schedule that you set to your lambda functions are in UTC time. For e.g, if you specify 5 pm, it is 5 pm UTC time. Maybe you are checking the logs before it executes.

Lambda permissions to log to cloudwatch

the lambda should have permission to log to cloudwatch. otherwise the lambda function will not add logs to cloudwatch. here is a policy that you can refer.

{
    "Statement": [
        {
            "Action": [
                "logs:CreateLogGroup",
                 "logs:CreateLogStream",
                 "logs:PutLogEvents"
            ],
            "Effect": "Allow",
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
} 

Reference:

Can't get AWS Lambda function to log (text output) to CloudWatch

hope this helps.

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
0

I believe you need to give permission to invoke your lambda function from the CloudWatch events (your rule). Have a look at this tutorial.

You have to either do it via CLI or from the language's AWS API. Eg:

aws lambda add-permission \
--function-name LogScheduledEvent \
--statement-id my-scheduled-event \
--action 'lambda:InvokeFunction' \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:123456789012:rule/my-scheduled-rule
Kulasangar
  • 9,046
  • 5
  • 51
  • 82