Serene
Serene

Reputation: 21

AWS CloudWatch Events Do not Trigger Lambda Function

I'm having trouble triggering my AWS Lambda function.

The function works perfectly fine when I click Test, but I've created a new scheduled rule which triggers the Lambda function every minute. It works once, and then never again. I've also tried to use Cron, same results.

The logs should output a print function, but instead they read this:

02:07:40
START RequestId: |numbers| Version: 8

02:07:40
END RequestId: |numbers|

I've clicked Enable on 'CloudWatch Events will add necessary permissions for target(s) so they can be invoked when this rule is triggered.', so I suspect that my permissions aren't an issue.

As a side note, I've done everything on the console and am not really sure how to properly use the CLI. Any help would be wonderful. Thank you.

Upvotes: 0

Views: 1598

Answers (2)

Serene
Serene

Reputation: 21

OK, here's where I went wrong:

According to this answer: https://forums.aws.amazon.com/thread.jspa?threadID=264583 AWS only runs the entire S3 zip package once. I needed to put all of my code into the handler to fix this.

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269091

The best way is to start simple, then build-up to the ultimate goal.

Start by creating an AWS Lambda function that simply prints something to the log file. Here is an example in Python:

def lambda_handler(event, context):

    print ('Within function')

Then, ensure that the function has been assigned an IAM Role with the AWSLambdaBasicExecutionRole policy, or another policy that grants access to CloudWatch Logs:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "*"
        }
    ]
}

Then, configure CloudWatch Events to trigger the function once per minute and check the log files in Amazon CloudWatch Logs to confirm that the function is executing.

This will hopefully work correctly. It's then just a matter of comparing the configurations to find out why the existing function is not successfully running each minute. You can also look at the Monitoring tab to see whether any executions produced errors.

Upvotes: 1

Related Questions