1

Serverless isn't creating CloudWatch Events as a trigger to a lambda. There are no warnings or errors.

functions:
  aggregate:
    handler: statistics.handler
    events:
      - schedule:
          rate: rate(10 minutes)
lko
  • 8,161
  • 9
  • 45
  • 62

1 Answers1

10

Serverless' examples don't demonstrate the critical nature of the indentation. https://serverless.com/framework/docs/providers/aws/events/schedule/#schedule

functions:
  aggregate:
    handler: statistics.handler
    events:
    # "- schedule:" has to start at the same indentation as the "events:" above it.
    - schedule:
        # The CloudWatch Events Rules have to be exactly 4 spaces indented below the "- schedule:"
        rate: rate(10 minutes)
        # ... other fields

Crtical:

  • Align - schedule: with events: above it.
  • Align the next row e.g. rate: rate(6 minutes) 4 spaces indented from the - schedule:

enter image description here

Example code:

service: my-service
provider:
  name: aws
  region: us-west-2
  runtime: nodejs10.x
functions:
  hello:
    handler: handler.hello
    events:
    - schedule:
        rate: cron(*/5 * * * ? *)
        enabled: true

with

module.exports.hello = (event, context, callback) => {
  console.log("Hello, world!");
  callback(null);
};

Simply indenting - schedule 2 spaces like I'd expect does not create cloudwatch events in AWS. That single change of 2 spaces makes the difference between whether the cloudwatch event rule is created or not.

Note: No errors are thrown between the two indents, but it creates 6 vs. 8 AWS resources (2 missing don't create cloudwatch event rules).

lko
  • 8,161
  • 9
  • 45
  • 62
  • Is there any documentation that supports this? We deploy using the same spacing that the documentation you linked shows and we have no problems. It seems that the important part is that there are 4 spaces between the schedule row and the properties row (rate, enabled, etc.). However, the documentation does show 4 spaces. It may not look like it, but if you copy it into an editor, you can count the 4 spaces. So there really is no difference between your indention and the documentation's exception for the indention between events and schedule (and from what I can tell that doesn't matter). – Eric Jan 07 '20 at 22:18
  • Not that I'm aware of. This blog https://medium.com/blogfoster-engineering/running-cron-jobs-on-aws-lambda-with-scheduled-events-e8fe38686e20 has the same indentation that works for me, and if I change that indentation it will not create cloudwatch events. Not sure what the reason is, but it's consistent for me (both in the example and my own custom serverless schedule triggered lambdas). Note: It doesn't give you errors, just creates 6 resources without the proper indentation or 8 with the `- schedule` lining up with `events:`. – lko Jan 08 '20 at 17:22
  • Here is the documentation: https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/ https://www.serverless.com/framework/docs/providers/aws/events/schedule/ – Ehsan Feb 10 '21 at 08:50