Currently my lambda function works successfully with one schedule event attached to it. Relevant excerpt from my template.yaml:
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
PARAM1: VALUE
Events:
CronHourlyEvent: # This already works
Type: Schedule
Properties:
Description: Send John Doe
Enabled: True
Schedule: "cron(0 0/1 * * ? *)"
Input: !Sub '{"name": "John Doe"}'
Lambda is triggered every one hour here and it works fine.
Now I would like to add another schedule event that triggers same lambda once a day at 12 Noon. One way of doing it would be to create a separate lambda and attach daily schedule event to that, but I don't want to create a new lambda just for that. I was hoping if I could attach two schedule events to the same lambda.
I could not find any example online where more that one schedule events were attached to a lambda, but I think following addition in template.yaml file would be needed:
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java11
MemorySize: 512
Environment:
Variables:
PARAM1: VALUE
Events:
CronHourlyEvent: # this was already present
Type: Schedule
Properties:
Description: Send John Doe
Enabled: True
Schedule: "cron(0 0/1 * * ? *)"
Input: !Sub '{"name": "John Doe"}'
CronDailyEvent: # added this
Type: Schedule
Properties:
Description: Send Jane Doe
Enabled: True
Schedule: "cron(0 12 1/1 * ? *)"
Input: !Sub '{"name": "Jane Doe"}'
I want to test it locally, so I downloaded and configured sam-sdk. But I don't think that supports running cron jobs locally. I was able to trigger events manually, but couldn't find any provision to run schedule jobs automatically based on cron expression provided.
So I would like to know:
This will go directly into production, and I can't seem to figure out a way to test it locally.
I might be a little late, but for anyone looking for a similar solution. Just use different names for the trigger.
Events:
Serverless:
Type: Api
Properties:
Path: /serverless
Method: get
FirstConfigScheduledEvent:
Type: Schedule
Properties:
Schedule: cron(30 21 * * ? *)
SecondConfigScheduledEvent:
Type: Schedule
Properties:
Schedule: cron(30 23 * * ? *)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With