serverlessamazon-cloudwatch-events

Serverless - Schedule event not creating CloudWatch Events


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)

Solution

  • 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:

    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).