How to configure eventbridge rule in serverless.yml (using serverless framework ) to invoke lambda on specific time

Viewed 2663

We have already python lambda function running with AWS eventbridge which was configured manually using console to trigger lambda on 9 PM everyday. Currently, we also have rule arn for eventbridge.

Plan:

So, We are migrating everything to serverless framework to automate the whole lambda deployment and configuring eventbridge using serverless.yml to invoke the lambda on 9 pm.

Can anyone please advise how do I do that ?

Sample code:

functions:
  myFunction:
    handler: index.handler
    events:
      - eventBridge:
          --------
          --------
          --------
2 Answers

There is documentation at serverless.com that describes this. All you do is add the cron schedule to the EventBridge event as if it was a schedule event. For example:

functions:
  myFunction:
    handler: index.handler
    events:
      - eventBridge:
          schedule: cron(0 12 * * ? *)
          input:
            key1: value1

You can find the documentation for EventBridge here: https://www.serverless.com/framework/docs/providers/aws/events/event-bridge

And for the Schedule event with an example of a cron schedule here: https://www.serverless.com/framework/docs/providers/aws/events/schedule

In addition to @Gareth McCumskey answer. If you are planing to hook an existing event bus you could simply pass it's arn to eventBus key. Here is an example from serverless docs

- eventBridge:
    eventBus: arn:aws:events:us-east-1:12345:event-bus/custom- 
    private-events
    pattern:
      source:
        - custom.private

Another option is to use CloudFormation's intrinsic functions.

Related