Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to attach multiple schedule/cron events with one AWS Lambda function?

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:

  1. Whether we can attach 2 or more schedule/cron type events to an AWS lambda function?
  2. 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.

like image 923
rsp Avatar asked Nov 04 '25 03:11

rsp


1 Answers

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 * * ? *)
like image 198
deep_c Avatar answered Nov 06 '25 02:11

deep_c