35

I have been trying to deploy a Lambda in Serverless to run on a Cron schedule that invokes it every hour. When it is invoked, I want the event inside the Lambda to be populated by my own JSON input rather than the info from the Cron event which is the default input when it is deployed.

Inside the AWS Console, I am able to manually go into the Cron trigger for the Lambda and change the input from "Matched event" to "Constant (JSON text)" in order to get the result that I want. Since Serverless creates this rule while deploying the Lambda, I feel like there should be some way of changing the input through a configuration in the serverless.yml file. I haven't been able to find anything while searching through the docs for Serverless, so now I'm wondering if this is possible through Serverless in its current state, and if so how to go about it.

Any advice would be appreciated.

Edit: There was an update that should have added this functionality, however I still have not been able to deploy with a schedule with JSON using Serverless 1.3.0 (and have also tested with 1.2.0). Some examples of the serverless.yml I used are below:

functions:
  test:
    handler: test.test
    description: "test serverless Lambda"
    memorySize: 128
    timeout: 300
    events:
      - schedule:
        rate: rate(10 minutes)
        input:
          key: value
      - schedule:
        rate: rate(10 minutes)
        input: '{"key": "value"}'
      - schedule:
        rate: rate(10 minutes)
        input:
          key: 'value'

Would anybody be able to comment on the state of this feature in Serverless as of 1.3.0, and whether or not my serverless.yml above looks fine?

Edit 2: Posting the working serverless.yml

functions:
  test:
    handler: test.test
    description: "test serverless Lambda"
    memorySize: 128
    timeout: 300
    events:
      - schedule:
          rate: rate(10 minutes)
          enabled: true
          input:
            key: value
      - schedule:
          rate: rate(10 minutes)
          input: '{"key": "value"}'
          enabled: true
      - schedule:
          rate: rate(10 minutes)
          input:
            key: 'value'
          enabled: true
Raiju
  • 723
  • 1
  • 6
  • 11

1 Answers1

31

EDIT TO YOUR EDIT: I did some digging, it seems like serverless will automatically disable the schedule if it's not a string. Meaning if your entire event is - schedule: rate(10 minutes) it will be enabled. But if you have other properties you HAVE TO enable it because it will be disabled by default.

So your current .yml should look like this:

functions:   test:
    handler: test.test
    description: "test serverless Lambda"
    memorySize: 128
    timeout: 300
    events:
      - schedule:
        rate: rate(10 minutes)
        enabled: true
        input:
          key: value
      - schedule:
        rate: rate(10 minutes)
        input: '{"key": "value"}'
        enabled: true
      - schedule:
        rate: rate(10 minutes)
        input:
          key: 'value'
        enabled: true

You can use same input and inputPath in your serverless.yml file just like you would do with cloudwatch event rule. The only difference from cloudwath rules is that you can actually pass an object and serverless will stringify it for you automatically.

Example:

functions:
  crawl:
    handler: crawl
    events:
      - schedule: 
          rate: rate(1 hours)
          input: 
            key1: value1
            key2: value2

This will be equal to cloudformation event rule with input:"{'key1':'value1','key2':'value2'}" thus passing json instead of matched event.

Noticed just now that the question was asked on November 2nd. At that time it was not possible to do it but it was implemented soon after the question was asked. https://github.com/serverless/serverless/pull/2567

Erndob
  • 2,512
  • 20
  • 32
  • Thanks for the reply. I've actually been keeping up with this PR, but still haven't been able to deploy with the JSON I have input. I'l update the question and post some examples in it. – Raiju Dec 12 '16 at 14:32
  • I looked at the first example that you had posted again and I noticed that it was the indents that were the problem. I had originally copied the formatting from the PR which did not include the indents which caused the schedule to not appear at all. The tip to set enabled to true was very helpful as well, thank you very much for all the help! – Raiju Dec 15 '16 at 16:13
  • can you pass an array? input: ['test1','test2'] – inside Jan 31 '17 at 16:43
  • or does it have to be in quotes? input: '["test1","test2"]' – inside Jan 31 '17 at 16:44
  • Hey @Erndob can you please provide a link to the documentation where you found this? – aviggiano Mar 04 '18 at 15:59
  • How would you access this input variable in your lambda function? – Oamar Kanji Jan 29 '19 at 19:30
  • 2
    @OamarKanji You should create a separate question for that – Erndob Jan 30 '19 at 07:47
  • 1
    Thanks! It definitely wasn't clear for me from the documentation that when using the "long syntax" one *must* also set the `enabled` field. – ronkot Nov 13 '19 at 14:19