GithubHelp home page GithubHelp logo

cdk-scheduler's Introduction

cdk-scheduler

A CDK construct to schedule events precisely ⏱

This construct enables you to trigger an event at a given time on a serverless architecture.

You should use cdk-scheduler if you need to trigger an event at a precise time (down to the second) on your AWS application.

Install

To install with npm:

npm install cdk-scheduler

To install with yarn:

yarn add cdk-scheduler

Usage

cdk-scheduler is powered by SQS feature to delay events up to 15 minutes. A lambda is scheduled to query a DynamoDB Table every 15 minutes, it pushes every events scheduled in the next 15 minutes to SQS with a delay corresponding the desired publication date.

architecture: dynamoDB with scheduled event / lambda scheduled every 15 minutes / publishes to SQS with delay

Usage example with CDK - Typescript

You can check out the full implementation example in app.ts.

import { Scheduler } from 'cdk-scheduler';

class AppStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // Create Scheduler Construct
    const schedulerLib = new Scheduler(this, 'scheduler-lib');

    // Grant writing role to Dynamo DB to your service that will
    const dynamoDbApiIntegrationRole = new Role(
      this,
      'DynamoDbApiIntegrationRole',
      { assumedBy: new ServicePrincipal('apigateway.amazonaws.com') },
    );

    schedulerLib.schedulerTable.grantWriteData(dynamoDbApiIntegrationRole);

    // Add an integration tool such as Dynamo integration or Lambda
    const integration = new DynamoDBPutItemIntegration({
      partitionKey: schedulerLib.partitionKeyValue,
      table: schedulerLib.schedulerTable,
      role: dynamoDbApiIntegrationRole,
    });

    const restApi = new RestApi(this, 'RestApi');

    const addScheduledEventModel = restApi.addModel('AddScheduledEventModel', {
        ...
    });

    restApi.root.addMethod('POST', integration, {
      methodResponses: [{ statusCode: '200' }],
      requestModels: {
        'application/json': addScheduledEventModel,
      },
      requestValidatorOptions: {
        validateRequestBody: true,
        validateRequestParameters: true,
      },
      // authorizer: new RequestAuthorizer(stack, 'MyAuthorizer', {}),
    });
  }
}

const app = new App();
new AppStack(app, 'AppStack');

For the scheduler to function properly the elements added to DynamoDB must have the following attributes:

Attribute Type Description
pk string The primary key is always the same. Its value is the attribute partitionKey available in the construct instance
sk string The secondary key should start with the timestamp at which you wish to publish the event. You can concatenate with a unique id to be sure you do not have duplicates if you separate them with a #. For instance: ✅ 1649434680000#d66727f2-9df7-41b7-b2f8-211eb5581640 is a correct secondary key. ❌ 20220422-16:47:00:00Z00#66727f2-9df7-41b7-b2f8-211eb5581640 will never be read.
payload map This an object, to the format of your need. This payload will be sent in the event once it's published. Use this to detail the action you want to execute a the scheduled time

cdk-scheduler's People

Contributors

adelego avatar guiyom-e avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.