Skip to content

Latest commit

 

History

History

apigw-dynamodb-lambda-scheduler-sqs-cdk

AWS DynamoDB to EventBridge Scheduler via AWS Lambda function

This pattern demonstrates a sample to delay Amazon DynamoDB Stream processing using Amazon EventBridge Scheduler. EventBridge Scheduler invokes a target one-time or at regular intervals defined by a cron or rate expression.

Amazon DynamoDB streams are used to deliver event payload to the handlers in near real time. This pattern will help customers with usecases such as process stream events at a particular time window, as well as triggring one target AWS service from EventBridge Scheduler.

Learn more about this pattern at Serverless Land Patterns: Delayed processing of Dynamodb stream with EventBridge Scheduler

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

Deployment Instructions

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
    git clone https://github.com/aws-samples/serverless-patterns
  2. Change directory to the pattern directory:
    cd serverless-patterns/apigw-dynamodb-lambda-scheduler-sqs-cdk
  3. Install the project dependencies
     npm run init
  4. Build the project to convert typescript to js
     npm run build
  5. Deploy the stack to your default AWS account and region
    cdk deploy

Architecture

Architecture Diagram

How it works

In this sample, Amazon Apigateway receives a request from end-users that has a schedule time and event category provided in the request body. The data is stored in Amazon DynamoDB table. Amazon DynamoDB table has stream enabled and it publishes event to a Lambda function. Lambda function invokes Amazon EventBridge Scheduler and selects the groups provided the request body. EventBridge Scheduler provides a universal target parameter that you can use to create customized triggers that targets more than 270 AWS service and over 6,000 API operation on a schedule. In this example, we are showing integration with two targets, i.e. AWS Lamdba Function, and Amazon SQS queue.

Once the pattern is deployed to AWS, you will have the following resources created with the described capabilities

  • An edge optimized Amazon API Gateway endpoint that will receive user request
    • A Post method to add a new record in Amazon DynamoDB.
    • A Get method sent with an id to retrive the scheduler.
  • DynamoDB table with DynamoDB Stream enabled.
  • Amazon Lambda functions
    • Producer Lambda function that listens to DynamoDB stream and calls Amazon EventBridge Scheduler
    • Consumer Lambda function acts as a target and listens to EventBridge Scheduler. It performs given actions and deletes the schedule.
  • An EventBridge Schedule groups in which Producer Lambda function can categories an event.
  • An SQS Queue as another sample target for EventBridge Scheduler.
  • Messages with eventType:"Lambda" go to Lambda function at a given schedule under EventBridge Schedule group name - Lambda.
  • Messages with eventType:"Queue" or others will go to Queue at a given schedule via EventBridge Schedule group name - Queue.

Testing

In the AWS Console, browse to the API Gateway and find the EBSchedulerDemo Service that is created. Once the API is opened, go to Resources then select method POST. Select inbuild Test feature to Submit a request. Enter the below in the Message Body

{
    "eventName":"TestEventLambda",
    "eventType":"Lambda",
    "scheduleTime":"1679566052"
}

or

{
    "eventName":"TestEventQueue",
    "eventType":"Queue",  
    "scheduleTime":"1679566052"
}

After publishing, browse to the Schedules and EventBridge Scheduler, and inspect that the scheduler has placed an entry to schedule a message at a given time, for the given target.

Cleanup

  1. Delete the stack
    cdk destroy

Documentation


Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0