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:
- Whether we can attach 2 or more schedule/cron type events to an AWS lambda function?
- If yes, are the code changes I have done correct?
This will go directly into production, and I can't seem to figure out a way to test it locally.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…