I have a task that need to be scheduled on aws lambda function. I wrote a SAM template as below and I see it works when deploying on aws environment (my function get triggered intervally).
But we want to do testing on dev environment first before deploying. I use sam local start-api [OPTIONS]
to deploy our functions to dev environment. But the problem is that, every functions configured as rest API work, but the schedule task not. I’m not sure is it possible on local/dev environment or not. If not, please suggest a solution (is it possible?). Thank you
This is template:
aRestApi:
...
...
sendMonthlyReport:
Type: AWS::Serverless::Function
Properties:
Handler: src.monthlyReport
Runtime: nodejs16.x
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: "cron(* * * * *)"
3
Answers
I used
localstack
for the demonstrate. LocalStack is a cloud service emulator that runs in a single container on your laptop or in your CI environment. You can see more detail in this link https://github.com/localstack/localstackI would use node-cron to set a scheduler on a node file to run.
npm install –save node-cron
https://www.npmjs.com/package/node-cron
You can also check for this DigitalOcean tutorial!
If you search for local testing before deployment of lambda functions you will probably be fed this resource by the quote-unquote "google": https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-debugging.html
However, there are other ways to do it.
I personally use the public docker image from aws.
Here is an example of the docker image created for a specific use case of mine.
In your case, follow instructions for node and run the docker image locally. If it works, you can then continue with aws lambda creation/update.
I see that you also have a cron job, why not use the cron job to invoke this lambda function separately and not define it in you SAML?
There are a number of ways you can invoke a lambda function based on event.
For example to invoke using cli: (For aws-cliv2)
make sure you configure awscli
Makes it loosely coupled.
Then you can use carlo’s node cronjob suggestion to invoke is as many times you like, free of charge.