Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass json inputs to a Cron scheduled Lambda deployed in Serverless using event?

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 
like image 495
Raiju Avatar asked Nov 02 '16 17:11

Raiju


People also ask

What is serverless cron job?

Cron jobs are everywhere—from scripts that run your data pipelines to automated cleanup of your development machine, from cleaning up unused resources in the cloud to sending email notifications. These tasks tend to happen unnoticed in the background.

How do you trigger Lambda on a schedule?

You can also create a Lambda function and direct AWS Lambda to invoke it on a regular schedule. You can specify a fixed rate (for example, invoke a Lambda function every hour or 15 minutes), or you can specify a Cron expression. Open the Functions page of the Lambda console. Under Function overview, choose Add trigger.


1 Answers

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

like image 168
Erndob Avatar answered Oct 09 '22 23:10

Erndob