Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

EventBridge Scheduler to Lambda every 5 minutes

Created with SnapAmazon EventBridge SchedulerScheduleAWS Lambdaevery 5 minutes

Simple pattern that triggers a Lambda function every 5 minutes using Amazon EventBridge Scheduler

Creates a schedule that runs every 5 minutes to trigger a given Lambda function.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  (uksb-1tthgi812) (tag:eventbridge-schedule-to-lambda)
  Simple stack that uses EventBridge schedules to trigger a lambda function.

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  ScheduledLambdaFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: functions/
      Handler: app.lambdaHandler
      Runtime: nodejs20.x
      Architectures:
        - x86_64
    Metadata: # Manage esbuild properties
      BuildMethod: esbuild
      BuildProperties:
        Minify: true
        Target: 'es2020'
        # Sourcemap: true # Enabling source maps will create the required NODE_OPTIONS environment variables on your lambda function during sam build
        EntryPoints:
          - app.ts

  MyFirstSchedule:
    Type: AWS::Scheduler::Schedule
    Properties:
      Name: 'MySchedule'
      ScheduleExpression: 'rate(5 minute)'
      FlexibleTimeWindow:
        Mode: 'OFF'
      Target:
        Arn: !GetAtt ScheduledLambdaFunction.Arn
        RoleArn: !GetAtt MyFirstScheduleRole.Arn

  MyFirstScheduleRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - scheduler.amazonaws.com
            Action:
              - sts:AssumeRole
      Policies:
        - PolicyName: MyFirstScheduleRolePolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - 'lambda:InvokeFunction'
                Resource:
                  - !GetAtt ScheduledLambdaFunction.Arn

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/eventbridge-schedule-to-lambda

Deploy

sam deploy


Cleanup

Delete the stack: sam delete.

David Boyne

Presented by David Boyne

Helping people learn about event-driven architectures and serverless

Follow on LinkedIn